Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using setVisible(false) for another JFrame in a nested class.

Status
Not open for further replies.

andypara

Programmer
Apr 25, 2002
32
GB
Here's the code in it's most simplistic way:-

//v 1.3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;

// This example demonstrates the use of JButton, JTextField
// and JLabel.
public class LunarPhases implements ActionListener {

JPanel mainPanel, selectPanel, displayPanel, buttonPanel;

JComboBox phaseChoices = null;
JLabel phaseIconLabel = null;
JTextField tf = new JTextField("");
JButton myButton = new JButton("Andy");


// Constructor
public LunarPhases() {
// Create the phase selection and display panels.
selectPanel = new JPanel();
displayPanel = new JPanel();
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());

// Add various widgets to the sub panels.
addWidgets();

// Create the main panel to contain the two sub panels.
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3,1,5,5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

// Add the select and display panels to the main panel.
mainPanel.add(selectPanel);
mainPanel.add(displayPanel);
mainPanel.add(buttonPanel);
myButton.setToolTipText("Click to exit");
}
buttonPanel.add(myButton);
tf.setColumns(20);
buttonPanel.add(tf);

ButtonHandler handler = new ButtonHandler();
myButton.addActionListener(handler);


// Listen to events from combo box.
phaseChoices.addActionListener(this);
}


// main method
public static void main(String[] args) {
// create a new instance of LunarPhases
LunarPhases phases = new LunarPhases();

// Create a frame and container for the panels.
JFrame lunarPhasesFrame = new JFrame("Lunar Phases");

// Set the look and feel.
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}

lunarPhasesFrame.setContentPane(phases.mainPanel);

// Exit when the window is closed.
lunarPhasesFrame.setDefaultCloseOperation Frame.HIDE_ON_CLOSE);

// Show the converter.
lunarPhasesFrame.pack();
lunarPhasesFrame.setVisible(true);
}


class ButtonHandler implements ActionListener

{
public void actionPerformed(ActionEvent event)
{
Assessment3 ass = new Assessment3();

JFrame newFrame = new JFrame("Andy");

newFrame.setContentPane(ass.mainPanel);
lunarPhasesFrame.setVisible(false);
newFrame.pack();
newFrame.setVisible(true);

}
}
}
 
I'm guessing that you have a problem with setVisible working on a JFrame? Try using show() and hide() instead. JFrame inherits setVisible from Component, but I think it's ignored in favour of the more specialized show() and hide().
 
Still doesn't compile.

M:\java\JAVASW~1\LunarPhases.java:150: cannot resolve symbol
symbol : variable lunarPhasesFrame
location: class LunarPhases.ButtonHandler
lunarPhasesFrame.hide();
^
1 error
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top