Results 1 to 3 of 3
- 03-11-2013, 03:23 AM #1
Member
- Join Date
- Mar 2013
- Posts
- 22
- Rep Power
- 0
When i click on a JButton i want to display JCombobox on JFrame
Hi, heres the problem, im trying to make it so when i click on the button time, i get a combobox appearing on the frame.
Java Code:import java.awt.*; import javax.swing.JFrame; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; public class TravelAgentSystemGUI{ public static void main (String args[]) { JFrame myJFrame = new JFrame(); myJFrame.setSize(600,400); //500 wide, 300 tall myJFrame.setLocation(150,250); //150 across, 250 down myJFrame.setTitle("TravelAgentSystem"); myJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); myJFrame.setResizable(true); //allows user to resize the frame myJFrame.setVisible(true); myJFrame.setLayout(new BorderLayout()); JPanel testPanel = new JPanel(); testPanel.setLayout(new GridLayout(2,0)); JButton moneyButton, timeButton, hotelButton, exitButton; moneyButton = new JButton("Money"); timeButton = new JButton("Time"); hotelButton = new JButton("Hotel"); exitButton = new JButton("Exit"); testPanel.add(timeButton); testPanel.add(moneyButton); testPanel.add(hotelButton); testPanel.add(exitButton); testPanel.setBorder( new TitledBorder( new EtchedBorder(),"Main Menu")); testPanel.setBackground(Color.RED); myJFrame.add(testPanel, BorderLayout.WEST); TimeGUI handler = new TimeGUI( ); timeButton.addActionListener(handler); myJFrame.add(airports); } }
Java Code:import java.awt.*; import java.awt.event.*; public class TimeGUI implements ActionListener { public void actionPerformed(ActionEvent event) { JComboBox airports = new JComboBox(); airports.addItem("East Midlands"); airports.addItem("Birmingham"); airports.addItem("Heathrow"); airports.addItem("Albertus"); } }
i want it so when i click on the button Jcombo appears with those options but im struggling to do this, can anyone help?
-
Re: When i click on a JButton i want to display JCombobox on JFrame
You need to tell us what is wrong with the code you've posted. Exactly what problems are you having?
Also, consider replying to replies in your last thread on this forum, if only to let me know that you've read the replies. We're volunteers, and that's how were "paid", by your thanks.
- 03-12-2013, 01:26 AM #3
Member
- Join Date
- Mar 2013
- Posts
- 22
- Rep Power
- 0
Re: When i click on a JButton i want to display JCombobox on JFrame
apologies you guys have been very helpful and are helping me gain a better understanding of the language. I really appreciate it and thank you for your time. the code below works. When i click on the TimeButton A Jlabel appears. But instead of this JLabel i want to display a JComboBox with a list of string. Been working at this for 4 hours and still no luck can someone give me an idea please.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class events extends JFrame { private JLabel label; private JButton timeButton; private JButton moneyButton; private JButton hotelButton; private JButton exitButton; private JPanel testPanel; public events() { setLayout(new BorderLayout()); JPanel testPanel = new JPanel(); add(testPanel,BorderLayout.WEST); testPanel.setBackground(Color.RED); timeButton = new JButton("Time"); testPanel.add(timeButton); moneyButton = new JButton("Price"); testPanel.add(moneyButton); hotelButton = new JButton("Price"); testPanel.add(hotelButton); exitButton = new JButton("Exit"); testPanel.add(exitButton); label = new JLabel(""); add(label); event e = new event(); timeButton.addActionListener(e); } public class event implements ActionListener { public void actionPerformed(ActionEvent e) { label.setText("Now yoy can see words"); } } public static void main (String args[]) { events gui = new events(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(600,400); gui.setVisible(true); gui.setLayout(new BorderLayout()); gui.setTitle("Travel Agent System"); } }Last edited by beginnerprog; 03-12-2013 at 01:32 AM.
Similar Threads
-
Click in JFrame Titlebar sets the JFrame Opaque
By tim8w in forum AWT / SwingReplies: 1Last Post: 02-02-2013, 07:29 AM -
JButton after a click
By tnrh1 in forum AWT / SwingReplies: 9Last Post: 12-09-2011, 12:45 AM -
JButton click
By mine0926 in forum New To JavaReplies: 0Last Post: 07-30-2010, 04:50 AM -
JButton click problem
By mine0926 in forum NetBeansReplies: 7Last Post: 06-11-2010, 03:00 AM -
How to display a list of items and on click display subitems?
By mandyj in forum New To JavaReplies: 8Last Post: 12-29-2008, 07:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks