Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

I have created two class and from one class I want to open a combobox using actionlisterner, but whenever I run the code, combobox are not appeared. Herewith I have attached my code:

public class Work extends JFrame {

// variables for JPanel

  private JPanel buttonPanel;

  private JButton timeButton;

   public Work() 

 {

       setLayout(new BorderLayout()); 

      buttonPanel = new JPanel();

  buttonPanel.setBackground(Color.RED);

  buttonPanel.setPreferredSize(new Dimension(400, 500));

      add(buttonPanel,BorderLayout.WEST);

      timeButton = new JButton("Time"); 

  buttonPanel.add(timeButton);

  buttontime clickTime = new buttontime(); // event created when time button is clicked

  timeButton.addActionListener(clickTime);

    Time timeObject = new Time();

  timeObject.SelectTime();

  buttontime2 selectDest = new buttontime2();

  timeObject.getAirportBox().addActionListener(selectDest);

   }

       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.EAST);

           timeObject.getTimePanel().setVisible(true); 

           timeObject.getTimePanel().revalidate() ;

           timeObject.getAirportBox().setVisible(true);

  }

  }

          public class buttontime2 implements ActionListener{

  public void actionPerformed(ActionEvent selectDest) {

   Time timeObject = new Time();

  timeObject.SelectTime();

  if(timeObject.getAirportBox().getSelectedIndex() == 1) {

  timeObject.getEastMidBox().setVisible(true);

  }

  else if(timeObject.getAirportBox().getSelectedIndex() == 2) {

  timeObject.getBirmBox().setVisible(true);

 }

  else if(timeObject.getAirportBox().getSelectedIndex() == 3) {

  timeObject.getMancbox().setVisible(true);

  }

  else if(timeObject.getAirportBox().getSelectedIndex() == 4) { 

 timeObject.getHeathBox().setVisible(true);

  }   

   }

  }

public static void main (String args[]) {

events mainmenu = new events(); //object is created

mainmenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainmenu.setSize(800,500);

mainmenu.setVisible(true);

mainmenu.setLayout(new BorderLayout());

mainmenu.setTitle("Learning how to use GUI");

mainmenu.setBackground(Color.BLUE);

mainmenu.setResizable(false);

}

}

Can anyone help me with this?

1 Answer

0 votes
by (26.7k points)

Basically, in able to run the code, you need to create a TimeObject only once and then store it as a member variable of another class, and then you can use it instead of recreating it which will look like this:

public class Work extends JFrame {

    // ...

    private Time timeObject;

    public Work() {

        // ...

        timeObject = new Time();

        timeObject.SelectTime();

        buttontime2 selectDest = new buttontime2();

        timeObject.getAirportBox().addActionListener(selectDest);

    }

    public class buttontime implements ActionListener {

        public void actionPerformed(ActionEvent clickTime)  {

            // use timeObject, don't create it and don't call SelectTime()

            // example:

            add(timeObject.getTimePanel(),BorderLayout.EAST);

            // ....

        }

    }

    public class buttontime2 implements ActionListener {

        public void actionPerformed(ActionEvent clickTime)  {

            // use timeObject, don't create it and don't call SelectTime()

        }

    }

}

I hope this will help.

Want to become a Java Expert? Join Java Training now!!

Related questions

0 votes
1 answer
0 votes
1 answer
asked Apr 1, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...