If this is a Swing application (you still haven't told us), then you could solve your direct question, by placing in a JButton's ActionListener code that calls setVisible(false)
on the current window if you will use it again or dispose()
on it if you won't be using it again, and then calls setVisible(true)
on the next window object that you wish displayed. This could be as simple as:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ThisGuiClass.this.setVisible(false);
newGUI.setVisible(true);
}
});
but again unanswered questions remain.
- Again, is this a Swing GUI? Specific solutions will depend on the GUI library that you're using.
- The specifics of a code solution will also depend completely on the specific code and program structure that you're using, something you've not shown us. Please show us code if you want a better answer.
- Again, I stand by my recommendation to minimize the windows that are thrown at a user, to favor use of say CardLayout to swap views on a single window, if possible, and to limit 2nd window use to modal JDialogs (as much as possible). A great reference on this remains the link, The Use of Multiple JFrames, Good/Bad Practice?
Edit
OK, I've seen that you've added a Swing and AWT tag, thanks, but I still think that my answer is as yet very incomplete because the question is still incomplete.
Edit 2
Based on your code, I would stand by my original recommendations and would use a CardLayout to swap JPanel "views". For example, run this:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class NumberConverterWithCardLayout extends JPanel {
private CardLayout cardlayout = new CardLayout();
private JPanel cardPanel = new JPanel(cardlayout);
private BinaryPanel binaryPanel = new BinaryPanel();
private OctalPanel octalPanel = new OctalPanel();
private HexadecimalPanel hexadecimalPanel = new HexadecimalPanel();
private DecimalPanel decimalPanel = new DecimalPanel();
private JPanel[] allPanels = { binaryPanel, octalPanel, hexadecimalPanel,
decimalPanel };
public NumberConverterWithCardLayout() {
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
setLayout(new BorderLayout());
add(cardPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
cardPanel.add(createIntroPanel(), "intro");
for (final JPanel jPanel : allPanels) {
cardPanel.add(jPanel, jPanel.getName());
buttonPanel.add(new JButton(new AbstractAction(jPanel.getName()) {
@Override
public void actionPerformed(ActionEvent evt) {
cardlayout.show(cardPanel, jPanel.getName());
}
}));
}
}
private JPanel createIntroPanel() {
JPanel introPanel = new JPanel(new GridBagLayout());
introPanel.add(new JLabel("Introductory panel which introduces me and the program"));
return introPanel;
}
private static void createAndShowGui() {
NumberConverterWithCardLayout mainPanel = new NumberConverterWithCardLayout();
JFrame frame = new JFrame("Number Converter Application");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class BinaryPanel extends JPanel {
public static final String NAME = "Binary";
private static final int PREF_W = 500;
private static final int PREF_H = 400;
public BinaryPanel() {
setName(NAME);
add(new JLabel("A complex GUI that does binary calculations will go here"));
}
@Override
// just to give it some size
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}
class OctalPanel extends JPanel {
public static final String NAME = "Octal";
public OctalPanel() {
setName(NAME);
add(new JLabel("A GUI that does octal calculations will go here"));
}
}
class HexadecimalPanel extends JPanel {
public static final String NAME = "Hexadecimal";
public HexadecimalPanel() {
setName(NAME);
add(new JLabel("A GUI that does hexadecimal calculations will go here"));
}
}
class DecimalPanel extends JPanel {
public static final String NAME = "Decimal";
public DecimalPanel() {
setName(NAME);
add(new JLabel("A GUI that does decimal calculations will go here"));
}
}
Of course the calculation JPanels would be much more involved, holding nested JPanels each with its own layout manager, JTextFields, JButtons, and whatever else would be needed to implement their functionality.
Edit 3
Actually, on thinking this over, even that is overkill. Your far better off using a single JPanel with two text fields, one for number entry and one for result display, that has a bunch of buttons at the bottom. Then the display will change depending on which button (which type of conversion) is pressed. KISS principle.