sirdan13
sirdan13

Reputation: 103

Returning value from custom JButtons on JOptionPane.showMessageDialog

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.

Upvotes: 1

Views: 704

Answers (4)

Jay Smith
Jay Smith

Reputation: 2490

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

Upvotes: 2

Balázs Nemes
Balázs Nemes

Reputation: 729

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/

Upvotes: 0

GhostCat
GhostCat

Reputation: 140525

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.

Upvotes: 0

S.M.
S.M.

Reputation: 39

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!

Upvotes: 0

Related Questions