Hi everyone
I'm trying to change what shows up in the contentpane depending on what button you press. Say you press a button labeled "Customer", it would add textfields to the content pane.
I thought I could just add the action int eh actionPerformed method but doesn't seem to work.
Thanks for your help!
I'm trying to change what shows up in the contentpane depending on what button you press. Say you press a button labeled "Customer", it would add textfields to the content pane.
I thought I could just add the action int eh actionPerformed method but doesn't seem to work.
Thanks for your help!
Code:
public class ButtonDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
private JTextField nameField;
public ButtonDemo()
{
setSize(WIDTH, HEIGHT);
WindowDestroyer listener = new WindowDestroyer();
addWindowListener(listener);
Container contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(new FlowLayout());
JButton customerButton = new JButton("Customer");
customerButton.addActionListener(this);
contentPane.add(customerButton);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
Container contentPane = getContentPane();
if (actionCommand.equals("Customer"))
{
nameField = new JTextField("Hi everyone");
nameField.addActionListener(this);
contentPane.add(nameField);
}
else
System.out.println("Error in button interface.");
}
}
Comment