1

I have a JPanel in which I add a number of custom JButtons. When I put the JPanel into a showMessageDialog window, I don't manage to get any value from the pressing of one of the buttons. This is the window:

enter image description here

And this is the code:

public static void mainMenu() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
        JPanel panel = new JPanel(null);
        JButton button1 = new JButton();
        button1.setText("Conteggio Tweet"); button1.setSize(300, 80); button1.setLocation(100, 200); button1.setFont(new Font("Verdana", Font.ITALIC, 20));
        JButton button2 = new JButton();
        button2.setText("Top #Hashtag"); button2.setSize(300, 80); button2.setLocation(100, 300); button2.setFont(new Font("Verdana", Font.ITALIC, 20));
        JButton button3 = new JButton();
        button3.setText("Top Words"); button3.setSize(300, 80); button3.setLocation(450, 200); button3.setFont(new Font("Verdana", Font.ITALIC, 20));
        JButton button4 = new JButton();
        button4.setText("Top Utenti"); button4.setSize(300, 80); button4.setLocation(450, 300); button4.setFont(new Font("Verdana", Font.ITALIC, 20));
        JButton button5 = new JButton();
        button5.setText("Sentiment analysis"); button5.setSize(650, 80); button5.setLocation(100, 400); button5.setFont(new Font("Verdana", Font.ITALIC, 20));
        JLabel titolo = new JLabel();
        titolo.setText("Select an option:"); titolo.setSize(650, 80); titolo.setLocation(250, 70); titolo.setFont(new Font("Verdana", Font.BOLD, 30));

        panel.add(button2); panel.add(button1); panel.add(button3); panel.add(button4); panel.add(button5); panel.add(titolo);
        JOptionPane.showMessageDialog(null, panel, "Twitter", 0, icon);

    }

How can I retrieve a value from the buttons? Thank you.

sirdan13
  • 103
  • 6
  • What value do you want to get from the buttons? You can get its text, if you want. I believe Swing also has id fields, so you could also do that. – Compass Jun 19 '17 at 19:34
  • I need to know which button the user chose. So it could be anything, an int, the text, it's just enough something that tells me the choice. What would you advice me to do? – sirdan13 Jun 19 '17 at 19:35
  • You mean like an onClickListener? – Compass Jun 19 '17 at 19:36
  • I don't know like what. Any help is appreciated. – sirdan13 Jun 19 '17 at 19:36
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 19 '17 at 21:16

4 Answers4

2

You have to add ActionListener to JButtons to know which one is clicked. Do this before opening message dialog. Code:

ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton source = (JButton) e.getSource();
                String text = source.getText();
                System.out.println(text);
            }
        };
        button1.addActionListener(listener);
        button2.addActionListener(listener);
        button3.addActionListener(listener);
        button4.addActionListener(listener);
        button5.addActionListener(listener);

        JOptionPane.showMessageDialog(null, panel);

In ActionListener you can get clicked button by e.getSource method. Then you can check its text to see which button is clicked:

if(text.equals("Conteggio Tweet"){

} else if(text.equals("Top Words")) {

}

To close message dialog programmatically you can follow https://stackoverflow.com/a/9860799/6743203. Add to your listener :

Window window = SwingUtilities.getWindowAncestor(panel);
                window.dispose(); //first option
//              window.setVisible(false); //second option
Jay Smith
  • 2,331
  • 3
  • 16
  • 27
  • Thank you so much, this helps me a lot. What about if I wanted to perform an action, that actually closes the window? Like I've chosen an option and I want to move forward. – sirdan13 Jun 19 '17 at 19:54
  • Nevermind, I realized all what I have to do. Perfect answer, you even provided the code. Thank you mate. – sirdan13 Jun 19 '17 at 19:58
0

You need to use ActionListeners and the ActionPerformed Method. Something like this:

button1.addActionListener(this); //for every button
button1.setActionCommand("anything here");

and then you need

public void actionPerformed(ActionEvent e){}

where you define what shall happen if you press the button.

Important is also this line for your class:

public class ClassName extends JPanel implements ActionListener

Check out this Oracle Tutorial for help, they got some good examples too!

S.M.
  • 39
  • 1
  • 5
0

Simple: you need to read about using ActionListener objects.

There are events when buttons get clicked,you need to register for them to be able to react!

Beyond that, the real answer is: UI programming is not suited for trial and error learning!

There are many subtle details you need to know and understand in order to get to the desired results. Meaning: read and follow tutorials that explain all the steps required to solve a certain problem. And when you really understand all the elements, then start building your own programs. Anything else will dramatically increase the time to you need to get things done.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Instead of JOptionPane.showMessageDialog you can use JOptionPane.showOptionDialog. Here is an example how to do it: https://www.mkyong.com/swing/java-swing-joptionpane-showoptiondialog-example/

Balázs Nemes
  • 699
  • 4
  • 9