Results 1 to 3 of 3
  1. #1
    beginnerprog is offline Member
    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    beginnerprog is offline Member
    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    Default Re: When i click on a JButton i want to display JCombobox on JFrame

    Quote Originally Posted by Fubarable View Post
    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.
    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 02:32 AM.

Similar Threads

  1. Click in JFrame Titlebar sets the JFrame Opaque
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 02-02-2013, 08:29 AM
  2. JButton after a click
    By tnrh1 in forum AWT / Swing
    Replies: 9
    Last Post: 12-09-2011, 01:45 AM
  3. JButton click
    By mine0926 in forum New To Java
    Replies: 0
    Last Post: 07-30-2010, 05:50 AM
  4. JButton click problem
    By mine0926 in forum NetBeans
    Replies: 7
    Last Post: 06-11-2010, 04:00 AM
  5. Replies: 8
    Last Post: 12-29-2008, 08:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •