1

First I'd like it to be known im VERY new to coding. I would like to have my code be able to produce a different JOptionPane Message for every button clicked

ive tried including t[1][1] = JOptionPane(null, "message") (the location of each button) however the error came up saying you cant convert Jbutton to string.

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class jep implements ActionListener{

    public  JButton[][] t = new JButton[6][6];

    public static void main(String[] args) {
        new jep();
    }
    static int n = 100;

    public jep()  {

        JFrame frame = new JFrame("Jeopardy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1920,1080);
        frame.setLayout(new GridLayout(6, 6));

        frame.setVisible(true);
        for (int r = 0; r < 5; r++) {
            for (int c = 0; c < 6; c++) {
                String vakue = String.valueOf(n);
                t[r][c] = new JButton(vakue);
                t[r][c].setBackground(Color.BLUE);
                t[r][c].setForeground(Color.YELLOW);
                t[r][c].addActionListener(this);
                frame.add(t[r][c]);
            }
            n = n +300;
        }
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        JOptionPane.showInputDialog(null,"What's 1+1?");
    }
}

I would like it so that every button clicked says something else... For example if you click the first button it says like "RED" and the second "BLUE" etc...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

1

Right now you are using the same ActionListener for each button with a static string as the message, hence, all the buttons will display the same message.

In the following code, I created a separate inner class called ButtonHandler to be the ActionListener which allows you to pass a message in the constructor. This will allow you to set a separate message for each button. The jep class no longer needs to implement ActionListener in this example since that's handled by a separate class now.

An easy solution to make each button display something unique would be to store the values in an array ahead of time. Here I populate a String array with different color names, and then I use those values to assign the messages to each button.

Also, I changed the condition in your outer for loop to be r<6 because otherwise it would not populate a 6x6 grid

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class jep {

    public JButton[][] t = new JButton[6][6];

    public static void main(String[] args) {
        new jep();
    }

    static int n = 100;

    public jep() {

        JFrame frame = new JFrame("Jeopardy");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1920, 1080);
        frame.setLayout(new GridLayout(6, 6));
        frame.setVisible(true);

        String[][] messages = new String[6][6];
        for(int r=0; r<6; r++) {
            for(int c=0; c<6; c++) {
                switch(c) {
                    case 0:
                        messages[r][c] = "RED";
                        break;
                    case 1:
                        messages[r][c] = "ORANGE";
                        break;
                    case 2:
                        messages[r][c] = "YELLOW";
                        break;
                    case 3:
                        messages[r][c] = "GREEN";
                        break;
                    case 4:
                        messages[r][c] = "BLUE";
                        break;
                    case 5:
                        messages[r][c] = "INDIGO";
                        break;
                }
            }
        }

        for (int r = 0; r < 6; r++) {
            for (int c = 0; c < 6; c++) {
                String vakue = String.valueOf(n);
                t[r][c] = new JButton(vakue);
                t[r][c].setBackground(Color.BLUE);
                t[r][c].setForeground(Color.YELLOW);
                t[r][c].addActionListener(new ButtonHandler(messages[r][c]));
                frame.add(t[r][c]);
            }
            n = n + 300;
        }
    }

    private class ButtonHandler implements ActionListener {
        private String message;

        public ButtonHandler(String message) {
            this.message = message;
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            JOptionPane.showInputDialog(null, message);
        }
    }
}

Hope this helps!

Edit: combined examples for conciseness...

  • WOW! this really helped me understand what I need to do! Thanks for the information! – Carl O'Neil Apr 01 '19 at 02:18
  • Awesome! I'm glad I could help you out. It looks like you're trying to make the game Jeopardy, so when you're ready to start creating questions, categories, answers, etc, I would recommend looking into how to read from a text file. Then you can store all that information in a separate text file or files which would be nicer than just hard coding it all in your Java code. It will also make it more configurable so that you can possibly have multiple separate boards and just store them in different text files/folders. Anyway, good luck! – Wesley Cook Apr 01 '19 at 12:47
1

Another alternative to this valid answer:
You can change the action listener to identify which button was clicked, and respond accordingly:

public void actionPerformed(ActionEvent e) {

    String value = e.getActionCommand();
    String message = "";

    switch(value){
        case "100":
            message = "RED";
            break;
        case "400":
            message = "BLUE";
            break;
        default:
            message = "Un recognized button pressed";
            break;
    }

    JOptionPane.showInputDialog(null,message);
}

Side notes: do not frame.setSize(1920, 1080); instead set preferred size and have

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

at the end of the constructor, after all components have been added.

c0der
  • 18,467
  • 6
  • 33
  • 65