0
votes

In my Swing application I have 2 JFrame A and B. When I click button on JFrame A, It opens JFrame B and hide itself(I managed to do that part)

On JFrame B i have 4 JPanels Placed on JTabbedPane. Each JPanel has 2 JButtons.

I'm trying to hide JFrame B when i click Jbutton on JPanels and show Jframe A again.

How do I do this?

// JPanel Class

public class AddItemPanel extends javax.swing.JPanel {

  public AddItemPanel() {
        initComponents();
  }

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

      if(evt.getSource() == btnCancel)
      {
        ItemFrame d = new ItemFrame();      
             d.setVisible(false);// not working
             this.setVisible(false);// not working
      }
}

}

// JFrame Class

public class ItemFrame extends javax.swing.JFrame {

  public ItemFrame() {

        initComponents();
        jTabbedPane1.add("Add Items",new AddItemPanel());
        jTabbedPane1.add("Delete Items",new DeleteItemPanel());
        jTabbedPane1.add("Update Items",new UpdateItemPanel());
        jTabbedPane1.add("Search Items",new SearchItemPanel());
 }

}

1
use 'setVisible()' method for hiding and showing framesAzad
See The Use of Multiple JFrames, Good/Bad Practice? Likely frame B should be a (possibly modal) dialog and frame A should be the main app. and never disappear.Andrew Thompson

1 Answers

1
votes

try this example hope it's useful for you

  import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;

public class JframeTest implements ActionListener
{
    JButton b1;
    JButton b2;
    JFrame f1 ;
    JFrame f2;

public void init()
{
 f1 = new JFrame("Frame one");
 f2 = new JFrame("Frame two");

 f1.setSize(400,400);
 f2.setSize(400,400);

 f1.setLayout(new FlowLayout());
 f2.setLayout(new FlowLayout());

 b1 = new JButton("Open Frame two");
 b2= new JButton("Open Fram one");
 JPanel p1 = new JPanel();
 JPanel p2 = new JPanel();

 p1.setBackground(Color.white);
 p2.setBackground(Color.white);
 p1.add(b1);
 p2.add(b2);

f1.getContentPane().add(p1);
f2.getContentPane().add(p2);

f1.setVisible(true);
f2.setVisible(false);
f1.setDefaultCloseOperation(3);
f2.setDefaultCloseOperation(3);

b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
    if(evt.getSource() == b1)
    {
        f1.setVisible(false);
        f2.setVisible(true);
    }else if(evt.getSource()==b2)
    {
        f1.setVisible(true);
        f2.setVisible(false);
    }

}

public JframeTest()
{
    this.init();
}
public static void main(String...argS)
{
    new JframeTest();
}
}