Search code examples
javaswingjbutton

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.


Solution

  • 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