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

2019-05-09 04:48发布

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?!

2条回答
Evening l夕情丶
2楼-- · 2019-05-09 05:22

@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
    }
}
查看更多
我命由我不由天
3楼-- · 2019-05-09 05:35

It's Better to Enter Text in JButton Constructor because JButton constructor has argument of Strings not String Variable.

JButton button = new JButton("Invisible"); 
查看更多
登录 后发表回答