how do i fix this GUI Java code?

i'm trying to get the text fields to display the choice i chose from the combo boxes but it doesnt work when i chose a plate or dessert

and when i chose a drink

the drink name displays in all three text field boxes

the problem is here:

the JComboBox only displays the first JComboBox I have?

how can i change this

i tried to change each to c,c1,c2 but it doesn't work... some syntax error

public void actionPerformed(ActionEvent e) {

t.setText((String) ((JComboBox) e.getSource()).getSelectedItem());

t1.setText((String) ((JComboBox) e.getSource()).getSelectedItem());

t2.setText((String) ((JComboBox) e.getSource()).getSelectedItem());

}

here is my entire code:

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JApplet;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JTextField;

public class Menu extends JApplet {

private String[] drinks = { "water", "soda", "fruit juice"};

private String[] plates = { "sushi", "pasta", "salad" };

private String[] desserts = { "cake", "ice cream", "pastry"};

private JTextField t = new JTextField(15);

private JTextField t1 = new JTextField(15);

private JTextField t2 = new JTextField(15);

private JComboBox c = new JComboBox();

private JComboBox c1 = new JComboBox();

private JComboBox c2 = new JComboBox();

private JButton b = new JButton("Add items");

private int count = 0;

private int count1 = 0;

private int count2 = 0;

public void init() {

for (int i = 0; i < 3; i++)

c.addItem(drinks[count++]);

t.setEditable(false);

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (count < drinks.length)

c.addItem(drinks[count++]);

}

});

for (int j = 0; j < 3; j++)

c1.addItem(plates[count1++]);

t1.setEditable(false);

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (count < plates.length)

c1.addItem(plates[count1++]);

}

});

for (int k = 0; k < 3; k++)

c2.addItem(desserts[count2++]);

t2.setEditable(false);

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (count < desserts.length)

c2.addItem(desserts[count2++]);

}

});

c.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

t.setText((String) ((JComboBox) e.getSource()).getSelectedItem());

t1.setText((String) ((JComboBox) e.getSource()).getSelectedItem());

t2.setText((String) ((JComboBox) e.getSource()).getSelectedItem());

}

});

Container cp = getContentPane();

cp.setLayout(new FlowLayout());

cp.add(c);

cp.add(c1);

cp.add(c2);

cp.add(b);

cp.add(t);

cp.add(t1);

cp.add(t2);

}

public static void main(String[] args) {

run(new Menu(), 200, 125);

}

public static void run(JApplet applet, int width, int height) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(applet);

frame.setSize(width, height);

applet.init();

applet.start();

frame.setVisible(true);

}

}

Comments

Sign In or Register to comment.