When you say you want to add a new window into a JFrame, then that has
to be a panel, and that is what this solution uses.
I also plucked out the inner anonymous listener classes and made the
frame an action listener; why people want make a new inner anonymous
class for every little responsive thingie in a GUI beats me....
In this setup the panels are stored in a hashtable for reuse, e.g. when the
user selects 'connect' then the previous connect panel (if any) will show.
Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
class CenterPanel extends JPanel {
private JLabel lbl=new JLabel();
private JTextField tf=new JTextField(22);
public CenterPanel(String choice) {
lbl.setText(choice);
add(lbl);
add(tf);
}
public String getLabelText() {
return this.lbl.getText();
}
public boolean equals (Object ob) {
if (! (ob instanceof CenterPanel) ) return false;
CenterPanel p=(CenterPanel)ob;
return lbl.getText().equals(p.getLabelText());
}
}
public class MainFrame
extends javax.swing.JFrame implements ActionListener {
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem connectItem;
private JMenuItem testItem;
private JMenuItem exitItem;
private Connection connection;
private Hashtable panelHt=new Hashtable();
public MainFrame() {
super("Application");
getContentPane().setLayout(new BorderLayout());
menuBar = new JMenuBar();
fileMenu = new JMenu();
connectItem = new JMenuItem();
exitItem = new JMenuItem();
testItem = new JMenuItem();
fileMenu.setMnemonic('F');
fileMenu.setText("File");
connectItem.setMnemonic('L');
connectItem.setText("Connect");
testItem.setMnemonic('T');
testItem.setText("Test");
exitItem.setMnemonic('E');
exitItem.setText("Exit");
// anonymous inner listener classes sux....
connectItem.addActionListener(this);
testItem.addActionListener(this);
exitItem.addActionListener(this);
fileMenu.add(connectItem);
fileMenu.add(testItem);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar); // set menu bar for this application
} //enf of Public MainFrame
/**
* Handle the actions
* @param e
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Exit")) {
System.exit(0);
} else {
setCenterPanel(e.getActionCommand());
}
}
/**
* Panel swapping
* @param s
*/
private void setCenterPanel (String s) {
// first remove any old centerpanel
Component [] cmp=getContentPane().getComponents();
for (int i=0; i<cmp.length; i++) {
if (cmp[i] instanceof CenterPanel) {
getContentPane().remove(cmp[i]);
break;
}
}
// then check if we have made the panel already, lookup in hashtable.
CenterPanel aPanel=(CenterPanel)panelHt.get(s);
if (aPanel==null) {
aPanel=new CenterPanel(s); // nope, make a new one
panelHt.put(aPanel.getLabelText(),aPanel); // store in hashtable
}
getContentPane().add(aPanel, BorderLayout.CENTER); // show it
validate();
repaint();
}
public static void main (String [] args) {
MainFrame mf=new MainFrame();
mf.setBounds(20,20,400,300);
mf.setVisible(true);
}
} //end of class Main Frame }//end of class Main Frame
Bookmarks