1

How to create this:

JButton b = new JButton("text").addActionListener(e -> classX.addNewTest()));
buttons.add(b);

in one line? I tryed this:

panel.add(b = new JButton("text").addActionListener(e -> classX.addNewTest()));

But how could I do it whitout creating a "b"?

  • Do you need `b` somewhere else? If not, try `panel.add(new JButton("text").addActionListener(e -> classX.addNewTest()));` without declaring b. – ppasler Jan 28 '17 at 13:16
  • @ppasler `addActionListener()` returns `void` (up to Java 9), so this would not work - the same reason it did not work for the code posted in the question – user85421 Jan 28 '17 at 14:12
  • @CarlosHeuberger indeed you are right, my bad. – ppasler Jan 28 '17 at 14:16
  • @Tobiasz - just out of curiosity, why must it be one line? Three fundamental operations in one line: button creation, handling of action and adding to panel. Easier to understand and maintain if on different lines...just MHO – user85421 Jan 28 '17 at 14:44

3 Answers3

2

If you really want to do that in one line, you could extend the JButton class and add the listener in a instance initializer:

    panel.add(new JButton("text") {{ addActionListener(e -> classX.addNewTest()); }} );

I would not recommend doing that: it is very hard to understand, almost code obfuscation, and it is creating a subclass of JButton without really extending its functionality at all. See What is Double Brace initialization.

A better approach could be to write a method to create buttons - I do this for most components:

    panel.add(createJButton("test", e -> classX.addNewTest()));

...

private JButton createJButton(String text, ActionListener listener) {
    JButton button = new JButton(text);
    button.addActionListener(listener);
    // more customization if needed
    return button;
}
Community
  • 1
  • 1
user85421
  • 28,957
  • 10
  • 64
  • 87
  • 2
    To repeat and ephasisze what you said: **I would not recommend doing that**. It's really odd... – Marco13 Jan 28 '17 at 14:34
1

If i'm not wrong this is not possible with one line!

Why?

Because there are different type so for example : jButton.addActionListener(Action) don't return any thing a void so you could not add a void type to a List which take a JButton type.

and you can get this error : 'void' type not allowed here and type-mismatch-cannot convert typeX to typeY

What is "Type mismatch" and how do I fix it?

Hope this can help you.

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • I know that `jButton.addActionListener(Action)` returns void. I'm just looking for a clever way to do it (even in a diferent form) in one line. But thank you for answering, if there is no other option I will just use two lines ;/ – Tobiasz Rumian Jan 28 '17 at 11:25
0

You may also look into Action:

    JButton b;
    new JPanel().add(b=new JButton(new AbstractAction("Click Me"){
        @Override
        public void actionPerformed(ActionEvent e){
            //
        }
    }));