0

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());
 }

}

amal
  • 3,470
  • 10
  • 29
  • 43
  • use 'setVisible()' method for hiding and showing frames – Azad Feb 24 '13 at 11:32
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) Likely frame B should be a (possibly modal) dialog and frame A should be the main app. and never disappear. – Andrew Thompson Feb 24 '13 at 11:36

1 Answers1

1

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();
}
}
Azad
  • 5,047
  • 20
  • 38
  • 3
    `public class Java extends JFrame implements ActionListener` Poorly named class, don't extend frame unless adding new functionality, best to create anonymous listeners or actions.. I would not be recommending this code for study. Also, use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Feb 24 '13 at 11:47
  • Thank you for your response.I managed to run the program this way. But when I placed Jbutton on JPanel, i can not use setVisible() method to hide or show first JFrame. – amal Feb 24 '13 at 11:54
  • @AndrewThompson Thank you for your comment. I will try to be better in future – Azad Feb 24 '13 at 12:04
  • @Azad Eng Hi.I'm sorry i forgot to mention i use 4 extended JPanels as separate classes. On my JFrame B, I have Jtabpane. I have added these 4 JPanels like this. `code`public ItemFrame() { jTabbedPane1.add("Add Items",new AddItemPanel()); jTabbedPane1.add("Delete Items",new DeleteItemPanel());; }`code` Each Panel has 2 Jbutton. What I want to do is, When i click on Cancel button on each Jpanel, I want to hide current JFrame and show First Frame again. – amal Feb 24 '13 at 12:37
  • So you have only just one frame have a JTabedPane of 4 panels ? – Azad Feb 24 '13 at 12:44
  • yes. i have 2 Jframes. when i click button on Jframe A, it opens Jframe B. Inside Jframe B, i have Jtabpane. – amal Feb 24 '13 at 12:52
  • @AzadEng, Sorry about confusion. Im new to this site. Could you please tell me how can i post part of my code here. That will give better idea. I already tried to putting code between `code`--`code` blocks. – amal Feb 24 '13 at 13:05
  • 1
    @user2033382: You can edit your question to include new code, just as Azad Eng has update his answer in response to Andrew's helpful comments' +1. See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html), which applies to applets, too. – trashgod Feb 24 '13 at 13:36