Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, Im trying to only have one value disable a combobox. It works at first but i

ID: 3907820 • Letter: H

Question

Hi,

Im trying to only have one value disable a combobox. It works at first but if I go back and forth between frames it stops working. Here is the bug.

1. Select value Test2 in the combobox to disable the other comboxbox on the other frame.

2. Press the continue button to go to the next frame.

3. Press Back button on the other frame to go back to the original frame

4. Test1 is selected in the combobox. If I press the continue button again the other combobox will still be disable. I would need to reselect the Test1 button to do that. Is there it can automatically select that button when I go back. I tried setSelectedItem but it didnt work,

package jframe;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.WindowConstants;

import javax.swing.border.EmptyBorder;

import javax.swing.JComboBox;

import javax.swing.DefaultComboBoxModel;

//create class and extend with JFrame

public class jrrame1 extends JFrame

{

   //declare variable

   private JPanel contentPane;

   private static int flag = 1;

   /**

   * Launch the application.

   */

   //main method

   public static void main(String[] args)

   {

       /* It posts an event (Runnable)at the end of Swings event list and is

       processed after all other GUI events are processed.*/

       EventQueue.invokeLater(new Runnable()

       {

           public void run()

           {

               //try - catch block

               try

               {

                   //Create object of OldWindow

                   jrrame1 frame = new jrrame1();

                   //set frame visible true

                   frame.setVisible(true);                  

               }

               catch (Exception e)

               {

                   e.printStackTrace();

               }

           }

       });

   }

   /**

   * Create the frame.

   */

   public jrrame1()//constructor

   {

      

       //set default close operation

       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

       //set bounds of the frame

       setBounds(100, 100, 450, 300);

       //create object of JPanel

       contentPane = new JPanel();

       //set border

       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

       //set ContentPane

       setContentPane(contentPane);

       //set null

       contentPane.setLayout(null);

      

       //create object of JButton and set label on it

       JButton btnNewFrame = new JButton("Continue");

       //add actionListener

       btnNewFrame.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent arg0)

           {      

               //call the object of NewWindow and set visible true

               jframe2 frame = new jframe2();  

               frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

dispose();

               frame.setVisible(true);

           }

       });

       //set font of the Button

               btnNewFrame.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));

               //set bounds of the Button

               btnNewFrame.setBounds(143, 195, 116, 25);

               //add Button into contentPane

               contentPane.add(btnNewFrame);

      

              

       JComboBox comboBox = new JComboBox();

       comboBox.setModel(new DefaultComboBoxModel(new String[] {"Test1", "Test2"}));

       comboBox.setBounds(93, 106, 248, 27);

      

          comboBox.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent event) {

      JComboBox DeviceCB = (JComboBox) event.getSource();

      Object selectDevice = DeviceCB.getSelectedItem();

  

  

  

      //Having issues with this.

      if(selectDevice.toString().equals("Test2"))

      {      

      

       flag =2;  

      }

      else if(selectDevice.toString().equals("Test1"))

      {

         flag =1;

           

      }  

      }

      });      

      

          contentPane.add(comboBox);

      

      

   }

  

   public static int getFlagValue()

   {

       return flag;

   }

}

-- - - - --- -

package jframe;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import jframe.jrrame1;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.WindowConstants;

import javax.swing.border.EmptyBorder;

import javax.swing.JComboBox;

import javax.swing.DefaultComboBoxModel;

//create class and extend with JFrame

public class jframe2 extends jrrame1

{

   //declare variable

   private JPanel contentPane;

   /**

   * Launch the application.

   */

   //main method

   /**

   * Create the frame.

   */

   public jframe2() //constructor

   {

       //set default close operation

       jrrame1 test = new jrrame1();

      

       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

       //set bounds of the frame

       setBounds(100, 100, 450, 300);

      

       //create object of JPanel

       contentPane = new JPanel();

       //set border

       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

       //set ContentPane

       setContentPane(contentPane);

       //set null

       contentPane.setLayout(null);

      

      

       //create object of JButton and set label on it

       JButton btnNewFrame = new JButton("Back");

       //add actionListener

       btnNewFrame.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent arg0)

           {

              

               jrrame1 frame = new jrrame1();  

               frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

dispose();

               frame.setVisible(true);

              

              

          

               //set default close operation

              

           }

       });

       //set font of the Button

               btnNewFrame.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));

               //set bounds of the Button

               btnNewFrame.setBounds(143, 195, 116, 25);

               //add Button into contentPane

               contentPane.add(btnNewFrame);

      

       JComboBox comboBox1 = new JComboBox();

       comboBox1.setModel(new DefaultComboBoxModel(new String[] {"Test3", "Test4"}));

       comboBox1.setBounds(77, 61, 230, 27);

       contentPane.add(comboBox1);

      

       if(jrrame1.getFlagValue() ==2)

           comboBox1.setEnabled(false);

      

             

       else

       {

           comboBox1.setEnabled(true);

       }

      

  

       comboBox1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {

JComboBox DeviceCB = (JComboBox) event.getSource();

Object selectDevice = DeviceCB.getSelectedItem();

}

});  

   }

  

}

Explanation / Answer

Ans.

Hello folks. I think you are doing great. You are doing the best thing that you are trying to build something of your interest. Now coming to the solution:-

As your problem is quite simple. You have done everything correct. What you have to do extra is you have to set the value of flag on continue button also because from continue button you are trying to open a new frame. Value of flag is only being changed when any action is being performed on combobox. First time when you change the value of combobox from test1 to test2 then flag value is being changed and that is reflected on your second frame that your combobox is disabled. But when you come back to first frame flag value is as it is and without perfoming any action on combobox you press on continue, that's why flag value doesn't changed and it behaved as previous one but when you changed the value of flag by clicking on combobox then it will behave as per your expection. I think you have known your mistake and if anythin you are not able to understand kindly comment on the solution. I am pasting the solution below:-

package jframe;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.WindowConstants;

import javax.swing.border.EmptyBorder;

import javax.swing.JComboBox;

import javax.swing.DefaultComboBoxModel;

//create class and extend with JFrame

public class jrrame1 extends JFrame

{

//declare variable

private JPanel contentPane;

private static int flag = 1;

/**

* Launch the application.

*/

//main method

public static void main(String[] args)

{

/* It posts an event (Runnable)at the end of Swings event list and is

processed after all other GUI events are processed.*/

EventQueue.invokeLater(new Runnable()

{

public void run()

{

//try - catch block

try

{

//Create object of OldWindow

jrrame1 frame = new jrrame1();

//set frame visible true

frame.setVisible(true);   

}

catch (Exception e)

{

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public jrrame1()//constructor

{

  

//set default close operation

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//set bounds of the frame

setBounds(100, 100, 450, 300);

//create object of JPanel

contentPane = new JPanel();

//set border

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

//set ContentPane

setContentPane(contentPane);

//set null

contentPane.setLayout(null);

JComboBox comboBox = new JComboBox();

comboBox.setModel(new DefaultComboBoxModel(new String[] {"Test1", "Test2"}));

comboBox.setBounds(93, 106, 248, 27);   

//create object of JButton and set label on it

JButton btnNewFrame = new JButton("Continue");

//add actionListener

btnNewFrame.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent arg0)

{   

//call the object of NewWindow and set visible true

Object selectDevice = comboBox.getSelectedItem();

//Having issues with this.

if(selectDevice.toString().equals("Test2"))

{   

flag =2;  

}

else if(selectDevice.toString().equals("Test1"))

{

flag =1;

}  

jframe2 frame = new jframe2();  

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

dispose();

frame.setVisible(true);

}

});

//set font of the Button

btnNewFrame.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));

//set bounds of the Button

btnNewFrame.setBounds(143, 195, 116, 25);

//add Button into contentPane

contentPane.add(btnNewFrame);


  

comboBox.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {

JComboBox DeviceCB = (JComboBox) event.getSource();

Object selectDevice = DeviceCB.getSelectedItem();

  

  

  

//Having issues with this.

if(selectDevice.toString().equals("Test2"))

{   

  

flag =2;  

}

else if(selectDevice.toString().equals("Test1"))

{

flag =1;

}  

}

});   

  

contentPane.add(comboBox);

  

  

}

  

public static int getFlagValue()

{

return flag;

}

}

-------------------------------------

package jframe;

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import jframe.jrrame1;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.WindowConstants;

import javax.swing.border.EmptyBorder;

import javax.swing.JComboBox;

import javax.swing.DefaultComboBoxModel;

//create class and extend with JFrame

public class jframe2 extends jrrame1

{

//declare variable

private JPanel contentPane;

/**

* Launch the application.

*/

//main method

/**

* Create the frame.

*/

public jframe2() //constructor

{

//set default close operation

jrrame1 test = new jrrame1();

  

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

//set bounds of the frame

setBounds(100, 100, 450, 300);

  

//create object of JPanel

contentPane = new JPanel();

//set border

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

//set ContentPane

setContentPane(contentPane);

//set null

contentPane.setLayout(null);

  

  

//create object of JButton and set label on it

JButton btnNewFrame = new JButton("Back");

//add actionListener

btnNewFrame.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent arg0)

{

  

jrrame1 frame = new jrrame1();  

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

dispose();

frame.setVisible(true);

  

  

  

//set default close operation

  

}

});

//set font of the Button

btnNewFrame.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));

//set bounds of the Button

btnNewFrame.setBounds(143, 195, 116, 25);

//add Button into contentPane

contentPane.add(btnNewFrame);

  

JComboBox comboBox1 = new JComboBox();

comboBox1.setModel(new DefaultComboBoxModel(new String[] {"Test3", "Test4"}));

comboBox1.setBounds(77, 61, 230, 27);

contentPane.add(comboBox1);

  

if(jrrame1.getFlagValue() ==2)

comboBox1.setEnabled(false);

  

else

{

comboBox1.setEnabled(true);

}

  

  

comboBox1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {

JComboBox DeviceCB = (JComboBox) event.getSource();

Object selectDevice = DeviceCB.getSelectedItem();

}

});  

}

  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote