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

    Default action Listener when i click on a button a panel from another class appears

    When I click on the timeButton, i want to make a panel (timePanel) from the other class appear in the JFrame. I have put in the code but im getting a weird error.

    Java Code:
    import javax.swing.JOptionPane;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class events extends JFrame {
    // variables for JPanel
      private JPanel timePanel;
      private JButton timeButton;
      private JButton moneyButton;
      private JButton hotelButton;
      private JButton exitButton;
      
      public events() 
      {
      
    
      
      
      setLayout(new BorderLayout()); 
     
       JPanel buttonPanel = new JPanel();
    	  buttonPanel.setBackground(Color.RED);
    	  add(buttonPanel,BorderLayout.WEST);
    
    	  timeButton = new JButton("Time"); 
    	  buttonPanel.add(timeButton);
    	  
    	  moneyButton = new JButton("Price");
    	  buttonPanel.add(moneyButton);
    	  
    	  hotelButton = new JButton("Hotel");
    	  buttonPanel.add(hotelButton);
    	  
    	  exitButton = new JButton("Exit");
    	  buttonPanel.add(exitButton);
    	  
    	  
    	  
    	  
    	  buttontime clickTime = new buttontime();
    	  timeButton.addActionListener(clickTime);
    	  
    	   }
    
    	   
    	   
    	   public class buttontime implements ActionListener { //creating actionlistener for clicking on timebutton to bring up a combobox
    	  public void actionPerformed(ActionEvent clickTime)  {
    	  
    	      Time timeObject = new Time();
              timeObject.SelectTime();
      
              add(timeObject.gettimePanel(),BorderLayout.WEST);
    		  timeObject.gettimePanel().setVisible(true);
    	 
    	  
    	  
    	 
    	  }
    	  }
    	   
    	   
    	   
    	   
    	   public static void main (String args[]) {
    	events mainmenu = new events(); //object is created
    	mainmenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	mainmenu.setSize(600,400);
    	mainmenu.setVisible(true);
    	mainmenu.setLayout(new BorderLayout());
    	mainmenu.setTitle("Travel Agent System");
    	mainmenu.setBackground(Color.BLUE);
    
    	}
    	}

    Java Code:
    import javax.swing.JOptionPane;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
     class Time
    {
      private JPanel timePanel;
      private JComboBox	timeairportbox;//comboboxes declared
      private String[] Airport = {"","East Midlands", "Birmingham", "Manchester", "Heathrow"};//array of airports declared
    
      
    public void SelectTime() {
    
    //combobox objects created
    	  timeairportbox = new JComboBox(Airport); //array is inserted into the JComboBox
    	  timePanel.add(timeairportbox);  // Combobox is placed in the timePanel
    
    	  
    	
    	  
    }
    
     public JComponent gettimePanel() {
            return timePanel;
    		}
    		
    		}

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: action Listener when i click on a button a panel from another class appears

    And what is this weird error? Any exception messages mabybe?

  3. #3
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: action Listener when i click on a button a panel from another class appears

    you don't initialize the panel in class Time that is

    you replace this into
    Java Code:
    private JPanel timePanel;
    this
    Java Code:
    JPanel timePanel=new JPanel();
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

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

    Default Re: action Listener when i click on a button a panel from another class appears

    Quote Originally Posted by tamilarasi View Post
    you don't initialize the panel in class Time that is

    you replace this into
    Java Code:
    private JPanel timePanel;
    this
    Java Code:
    JPanel timePanel=new JPanel();

    Hi thanks for reply, but yes im getting an exception error

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: action Listener when i click on a button a panel from another class appears

    Quote Originally Posted by beginnerprog View Post
    Hi thanks for reply, but yes im getting an exception error

    can you post here your exception ?
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2012, 04:56 AM
  2. Replies: 2
    Last Post: 12-11-2011, 11:44 PM
  3. Action Listener listen to another class/ program
    By kevinn205 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 11:59 PM
  4. Table cells change to editable by button click action
    By Zamioculcas in forum New To Java
    Replies: 2
    Last Post: 11-24-2011, 10:50 PM
  5. Replies: 4
    Last Post: 08-11-2010, 01:47 PM

Posting Permissions

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