Combo Boxes

Reply

Join Date: Sep 2008
Posts: 38
Reputation: cproud21 has a little shameless behaviour in the past 
Solved Threads: 0
cproud21 cproud21 is offline Offline
Light Poster

Combo Boxes

 
0
  #1
Nov 10th, 2008
I created a combo box for the selection of a dorm....

I need to create another combo box for the selection of a meal plan...

Should I write that in another class, or can I add it to this class?

I need to display the total of the costs of both at the same time..

I appreciate any help...

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Main extends JFrame
{
private JPanel dormPanel;
private JPanel selectedDormPanel;
private JComboBox dormBox;
private JLabel label;
private JTextField selectedDorm;

private String[] dorm = { "Allen Hall: $1500/semester", "Pike Hall: $1600/semester", "Farthing Hall: $1200/semester", "University Suites: $1800/semester"};


public Main()
{
super("Dorm Selection");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout());

buildDormPanel();
buildSelectedDormPanel();

add(dormPanel, BorderLayout.CENTER);
add(selectedDormPanel, BorderLayout.SOUTH);

pack();
setVisible(true);


}

private void buildDormPanel()
{
dormPanel = new JPanel();
dormBox = new JComboBox(dorm);

dormBox.addActionListener(new ComboBoxListener());

dormPanel.add(dormBox);
}

private void buildSelectedDormPanel()
{
selectedDormPanel = new JPanel();

label = new JLabel("Dorm: ");
selectedDorm = new JTextField (25);
selectedDorm.setEditable(false);

selectedDormPanel.add(label);
selectedDormPanel.add(selectedDorm);
}

private class ComboBoxListener implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
String selection = (String) dormBox.getSelectedItem();
selectedDorm.setText(selection);
}
}

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










}
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Combo Boxes

 
0
  #2
Nov 10th, 2008
would be a crappy programming language if you couldn't fit two combo boxes into one frame, wouldn't you think?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Combo Boxes

 
0
  #3
Nov 10th, 2008
Originally Posted by cproud21 View Post
I created a combo box for the selection of a dorm....

I need to create another combo box for the selection of a meal plan...

Should I write that in another class, or can I add it to this class?

I need to display the total of the costs of both at the same time..

I appreciate any help...

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Main extends JFrame
{
private JPanel dormPanel;
private JPanel selectedDormPanel;
private JComboBox dormBox;
private JLabel label;
private JTextField selectedDorm;

private String[] dorm = { "Allen Hall: $1500/semester", "Pike Hall: $1600/semester", "Farthing Hall: $1200/semester", "University Suites: $1800/semester"};


public Main()
{
super("Dorm Selection");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout());

buildDormPanel();
buildSelectedDormPanel();

add(dormPanel, BorderLayout.CENTER);
add(selectedDormPanel, BorderLayout.SOUTH);

pack();
setVisible(true);


}

private void buildDormPanel()
{
dormPanel = new JPanel();
dormBox = new JComboBox(dorm);

dormBox.addActionListener(new ComboBoxListener());

dormPanel.add(dormBox);
}

private void buildSelectedDormPanel()
{
selectedDormPanel = new JPanel();

label = new JLabel("Dorm: ");
selectedDorm = new JTextField (25);
selectedDorm.setEditable(false);

selectedDormPanel.add(label);
selectedDormPanel.add(selectedDorm);
}

private class ComboBoxListener implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
String selection = (String) dormBox.getSelectedItem();
selectedDorm.setText(selection);
}
}

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










}
You can create another object of JComboBox in same way you created 'dormBox'.

Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC