Hi,
what my expericence says u must be using GBC layout somewhere in ur this module.
GBC is powerful layout ,but if u donot use it correctly it can create blunder for u.
i am sending u a main class from which another class opens contiainig requied panels i have used BorderLayout for this.
go through this,hope it will help u out.
...........Main class............
package abc;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class MainClass
extends JFrame {
private JButton btn1 = new JButton("Option 1");
private JButton btn2 = new JButton("Option 2");
private RanchWork ranchWork = new RanchWork();
public MainClass() {
getContentPane().setLayout(new FlowLayout());
getContentPane().add(btn1);
getContentPane().add(btn2);
setSize(200, 70);
setVisible(true);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ranchWork.addMyPanel();
ranchWork.pack();
ranchWork.setSize(500, 500);
ranchWork.setVisible(true);
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ranchWork.removeMyPanel();
ranchWork.pack();
ranchWork.setSize(500, 500);
ranchWork.setVisible(true);
}
});
}
public static void main(
String[] args) {
MainClass mainClass1 = new MainClass();
}
}
...........Dialog class..........
package abc;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class RanchWork
extends JDialog {
private JPanel panel3 = null;
private JSplitPane splitPane = null;
public RanchWork() {
init();
}
private void init() {
getContentPane().setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.add(new JLabel("Panel 1"));
JPanel panel2 = new JPanel();
panel2.add(new JLabel("Panel 2"));
panel3 = new JPanel();
panel3.add(new JLabel("Panel 3"));
JPanel panel4 = new JPanel();
panel4.add(new JLabel("Panel 4"));
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
panel2, panel3);
getContentPane().add(panel1, BorderLayout.NORTH);
getContentPane().add(splitPane, BorderLayout.CENTER);
getContentPane().add(panel4, BorderLayout.SOUTH);
}
protected void addMyPanel() {
if (splitPane.getRightComponent() == null) {
this.splitPane.add(panel3);
}
//
}
protected void removeMyPanel() {
if (splitPane.getRightComponent() != null) {
splitPane.remove(panel3);
}
}
}