0

I have two jframe, one is a main frame used to call check box frame, and another one is a jframe containing checkbox.

MainFrame.java

enter image description here

How to remember previous checked checkbox after main frame call again the checkbox frame?

CheckBox.java

enter image description here

below is my button action code:

private void btn_callCheckBoxActionPerformed(
  java.awt.event.ActionEvent evt) {                                         

    //call checkbox window
    CheckBoxWindow cbw = new CheckBoxWindow();
    cbw.setVisible(true);
    cbw.setEnabled(true);

    this.setVisible(true);
    this.setEnabled(false);

}    

call mainframe from checkbox frame

private void btn_callMainFrameActionPerformed(
  java.awt.event.ActionEvent evt) {                                         

    //call main frame window
    MainPage mp = new MainPage();
    mp.setVisible(true);
    mp.setEnabled(true);
    this.setEnabled(false);
    this.setVisible(false);
}
Ridzuan Adris
  • 1,192
  • 2
  • 13
  • 32
  • surely there are better way rather than using database right? – Ridzuan Adris Jul 11 '14 at 04:07
  • In some class (or even the same class), save a `Map` or some relevant collection containing information about which buttons were clicked when the window with the checkboxes was navigated away from. – Rogue Jul 11 '14 at 04:12

3 Answers3

2

you have multiple options.

1- Do not create new windows every time you click the buttons. Save the windows in variables and use setVisible(true) and setVisible(false) to show/hide them. If you hide the checkbox frame and the you show it again, the checkbox will be in the state before closing.

This something like this

 public MainPage() {
     this.checkWindow = new CheckWindow()
 }

 private void btn_callCheckBoxActionPerformed(java.awt.event.ActionEvent evt)  {                                         
        // show window
        this.checkWindow.setVisible(true);
        this.checkWindow.setEnabled(true);

        this.setVisible(true);
        this.setEnabled(false);
  }  

2.- Save and retrieve the values of the checkbox using the preferences api of java

Alejandro Vera
  • 377
  • 2
  • 12
1

Here's a way to do what you want:

In your CheckBoxWindow create a method that will return the boolean value of your selection

private boolean chck1=false;
private boolean chck2=false;

public boolean getCheckBoxOneState(){
    return chck1;
}
private void btn_callMainFrameActionPerformed(java.awt.event.ActionEvent evt) {                                         
    chck1 = jCheckBox1.isSelected();
    chck2 = jCheckBox2.isSelected();
//call main frame window
MainPage mp = new MainPage();
mp.setVisible(true);
mp.setEnabled(true);
this.setEnabled(false);
this.setVisible(false);}

Then in your MainPage just instance the other frame an call the method that return the value of the checkbox

boolean chck1State = new CheckBoxWindow().getCheckBoxOne();
Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131
0

Thank you for the help. cant solve it without you guys. i have succeed. i just have to hide the window and avoid creating new window when button is clicked.

\*
 *From Class MainFrame
 */
public MainFrame(){

this.CheckBox = new CheckBox(this);
}

//button action to show checkbox frame
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){

CheckBox.setVisible(true);
}

\***********************************************************************\

\*
* From class CheckBox
\*

public CheckBox(JFrame mainFrame) {
this.MainFrame = MainFrame;
}

//button action to show MainFrame frame
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
MainFrame.setVisible();
}
Ridzuan Adris
  • 1,192
  • 2
  • 13
  • 32