I'm still trying to work out my JComboBox and I'm having trouble with my ActionListener. I would like to have some JLabels change when I make a selection in my box.
Code:
public void admitPane(){
JLabel labelAdmit = new JLabel ("Admit");
JPanel admitCard = new JPanel();
You are adding the panel to the combobox's actionlisteners, then you
respond to selections (actions) in that combo by setting the texts in some JLabels.
If that doesn't work you must have missed out on something.
public class SetLabelText extends JFrame implements ActionListener {
JPanel centerPanel = new JPanel();
JLabel lbl1 = new JLabel();
JLabel lbl2 = new JLabel();
JLabel lbl3 = new JLabel();
JComboBox combo = new JComboBox();
private void fillCombo() {
combo.addItem("Test1");
combo.addItem("Test2");
combo.addItem("Test3");
combo.addActionListener(this);
}
public SetLabelText(String title) {
super(title);
jbInit();
fillCombo();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==combo) {
String s=(String)combo.getSelectedItem();
lbl2.setText(s);
}
}
public class ApplicationCentre extends JApplet implements ActionListener
{
Student[] arrayOfStudents;
Object[] jListArray;
String[] nameArray = new String[100];
private JPanel main, deck;
private JLabel labelName, labelAvg, counter, label1, label2, label3;
private JButton input, admit, display, submit, admitSubmit;
private Container container;
private CardLayout cardManager;
private JTextField name, avg;
private JList schools;
private int studentIndex;
private int count=0;
private int i = 0;
private int mark = 0;
private JComboBox studentBox;
private String nameOfStudent;
private JRadioButton a1, a2, a3, r1, r2, r3;
private String school1, school2, school3;
private JTextArea displayText;
private String textHolder=("");
// End of variables declaration
public void init ()
{
initComponents();
setSize(400, 210);
}
public void initComponents()
{
container = getContentPane();
container.setLayout( new BorderLayout() );
// Creating instances of each item
main = new JPanel();
input = new JButton("Input");
admit = new JButton("Admit");
display = new JButton("Display");
arrayOfStudents = new Student[100];
school1 = ("");
school2 = ("");
school3 = ("");
// End Creation
//set up main menu
main.setLayout (new GridLayout (3,1));
main.add(input);
main.add (admit);
main.add (display);
//set up deck
cardManager = new CardLayout();
deck = new JPanel(cardManager);
//Input Card
inputPane();
//Display Card
JLabel labelDisplay = new JLabel ("Display");
JPanel displayCard = new JPanel();
displayCard.setLayout (new BoxLayout (displayCard, BoxLayout.PAGE_AXIS));
JTextArea displayText = new JTextArea ("This is a JTextArea");
//listener for input
submit.addActionListener (this);
//add components to screen
container.add( main, BorderLayout.WEST);
container.add(deck, BorderLayout.CENTER);
}//end applicationCentre
//INPUT PANE
public void inputPane(){
labelName = new JLabel ("Name");
name = new JTextField (10);
labelAvg = new JLabel ("Average");
avg = new JTextField (3);
submit = new JButton ("Submit");
counter = new JLabel ();