Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JButton.setAction(...) null's button text

The following code renders a JButton without text:

public abstract class Test {

    public static void main(String... args) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();

            String text = "Invisible";
            JButton button = new JButton(text); // blank button rendered ???

            System.out.println(button.getText()); // prints Invisible

            button.setAction(new AbstractAction() {     
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // do nothing
                }
            });

            System.out.println(button.getText()); // prints null ???

            button.setFocusable(false);

            button.setPreferredSize(new Dimension(100, 40));

            panel.add(button);

            frame.setResizable(false);
            frame.getContentPane().add(panel);

            frame.pack();
            frame.setVisible(true);

        });

    }

}

If i remove the call to button.setAction(...) it renders the button including the text.

Alternatively:

public abstract class Test {

    public static void main(String... args) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();

            String text = "Invisible";
            JButton button = null;
            button = new JButton(new AbstractAction() {     
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // do nothing
                }
            });

            button.setText(text); // renders the button with text
            System.out.println(button.getText()); // prints Invisible obviously

            button.setFocusable(false);

            button.setPreferredSize(new Dimension(100, 40));

            panel.add(button);

            frame.setResizable(false);
            frame.getContentPane().add(panel);

            frame.pack();
            frame.setVisible(true);

        });

    }

}

Works fine but has some nasty implications like not being able to change the buttons action without resetting its text after.

Why?!

like image 806
Dima Maligin Avatar asked Sep 28 '22 14:09

Dima Maligin


1 Answers

@Dima Maligin

  • not an answer, will be deleted

  • Swing Action should be declared (override isEnabled too, it can be importnant)

.

 private class SwingAction extends AbstractAction {

   //or public Action SwingAction() {
   //       return new AbstractAction("Invisible") {
   //           here to override AbstractAction   
   //       } 
   //    } 

    public SwingAction() {
        putValue(NAME, "Invisible"); // bounds properties
        putValue(SHORT_DESCRIPTION, "Invisible");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // do nothing
    }
}
like image 91
mKorbel Avatar answered Nov 02 '22 05:11

mKorbel