So i have a menuing system. I create each window in a new class, but the problem is to detect which button is pressed in one of the provider classes by the driver class. I want the driver class to detect i have pressed for instance "list" to run the list class. Here i thought parameterized constructors would work best to get the variables across, but for some reason it is not working. Here is the code: (The gui works and detects the button is pushed etc.)
Provider class:
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
public class MainMenu implements ActionListener
{
public String choice = "";
public MainMenu(String c)
{
choice = c;
}
JFrame jf = new JFrame("Main Menu");
Toolkit tk = Toolkit.getDefaultToolkit ();
int x = (int) tk.getScreenSize ().getWidth ();
int y = (int) tk.getScreenSize ().getHeight ();
ImageIcon ev = new ImageIcon("event.png");
ImageIcon lb = new ImageIcon("list.png");
ImageIcon cv = new ImageIcon("vote.png");
ImageIcon bb = new ImageIcon("bio.png");
Font f1 = new Font ("Tahoma", Font.BOLD, 25);
Font f2 = new Font ("Chiller", Font.BOLD, 100);
JLabel lbl = new JLabel("MAIN MENU");
JLabel space = new JLabel ("");
JButton jb1 = new JButton("Events", ev);
JButton jb2 = new JButton("Leader Board", lb);
JButton jb3 = new JButton("Cast Vote", cv);
JButton jb4 = new JButton("Band Biographies", bb);
GridLayout grid = new GridLayout(10,10,1,1);
JPanel jp0 = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
public MainMenu()
{
lbl.setFont(f2);
jf.setBounds(0,0, x,y);
jf.setVisible(true);
jf.setResizable(false);
jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb1.setFont(f1);
jb2.setFont(f1);
jb3.setFont(f1);
jb4.setFont(f1);
jp0.add(lbl);
jp1.add(jb1);
jp2.add(jb2);
jp3.add(jb3);
jp4.add(jb4);
jf.setLayout(grid);
jf.add(space);
jf.add(space);
jf.add(jp0);
jf.add(space);
jf.add(jp1);
jf.add(jp2);
jf.add(jp3);
jf.add(jp4);
jf.repaint();
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == jb1)
{
choice = "events";
System.out.println(choice);//Just to test whether the buttons work...
}
else if (e.getSource () == jb2)
{
choice = "list";
System.out.println(choice);
}
else if (e.getSource () == jb3)
{
choice = "vote";
System.out.println(choice);
}
else if (e.getSource () == jb4)
{
choice = "bio";
System.out.println(choice);
}
}
public String getChoice()
{
return choice;
}
}
and here is the driver class which doesnt read the change in the choice variable:
public class PAT
{
public static void main(String[] args) //implements ActionListener
{
String choice = "";
MainMenu mm = new MainMenu(choice);
new MainMenu();
while (choice.equals(""))
{
if (choice.equals("list"))
{
System.out.println("Parameterized constructor works");
}
}
}
}
Any help or advice would be greatly appreciated. Thanks in advance.