I have the main application frame. If user clicks the button he gets a new frame which has some chart in it. Now if I want to close that chart both chart and main application closes. How can I distinguish those two closings. Certainly I don't want my application to be closed after closing the frame in which I put chart.
Here's the code of the chart frame.
chartBttn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final ShowChart sc = new ShowChart("Reserve Selection", getUtilExperiments() );
sc.pack();
RefineryUtilities.centerFrameOnScreen(sc);
sc.setVisible(true);
sc.setDefaultCloseOperation(ShowChart.DISPOSE_ON_CLOSE);
}
});
You can use the dispose() method. Or you can call setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); on the JFrame.
If I understood you correctly on your new JFrame build a method for your close button or X window button with:
setVisible(false);
dispose();
Otherwise please post your code on creating the new JFrame etc.
Certainly I don't want my application to be closed after closing the frame in which I put chart.
1) don't create lots of JFrames on the fly, create JFrame only once and re-use that for next usage(s), then
call only for visibility setVisible(false/true) with setDefaultCloseOperation(JFrame.NOTHING_ON_CLOSE)
or very simple workaround
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
2) or JDialog with setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE)
3) NOTICE: but in this case isn't possible close the current JVM instance, you have to add JButton or JMenu/JMenuItem which accelerate for System.exit(1)
simply use super.dispose(); for closing previous jframe
Related
I am facing a weird issue that I can't see to understand why. I have a frame with an Internal JFrame that opens upon a menu click. This internal frame is supposed to be always maximized. When I run the program and click on the menu then it does what is expected. If I close the internal jframe and click the button again, the internal jframe is minimized. If I close it and click the button then it is maximimzed!! Why is it alternating like that. Here is the code that open the internal jframe. This method is inside the JFrame class
private void onButtonClick(){
InternalFrameProp intFrame = new InternalFrameProp ();
intFrame.setVisible(true);
jDesktopPane1.add(intFrame );
try {
intFrame .setMaximum(true);
} catch (PropertyVetoException ex) {
Logger.getLogger(MainHomePage.class.getName()).log(Level.SEVERE, null, ex);
}
}
As I was trying to put a runnable example, I noticed that this behaviour only happen in the Windows look and feel. To get around I did
jDesktopPane1.setDesktopManager(new DefaultDesktopManager());
That seemed to fix the problem! I have no idea why but it worked!
I am trying to create a JPanel to display when a user clicks a button within my main JFrame. In Netbeans I first used the wizard to add a new JPanel to my project, I then used the GUI creator to fill in all the content. I am not trying to display the JPanel with the following code
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt)
{
Account selAcc = getSelectedAccount();
if(selAcc != null)
{
AccountView accPanel = new AccountView(Account.getDeepCopy(selAcc));
accPanel.setVisible(true);
}
else
ShowMessage("Please select an account to view");
}
But nothing happens, no error is thrown and the JPanel is not shown. So I then changed the JPanel to a JFrame (Netbeans didn't complain). When I try again with the same code I receive the error GroupLayout can only be used with one Container at a time.
How can I display my JPanel/JFrame?
To change views within a Swing GUI, use a CardLayout as this is a much more robust and reliable way to do this.
Don't try to blindly "change a JPanel to a JFrame". It looks like you're just guessing here.
GroupLayout can't be reused as the error message is telling you. Likely this error comes from the point above. If you avoid trying to make a JFrame out of a JPanel, the error message will likely go away. As an aside, GroupLayout is not easily used manually, especially if you're trying to add components to an already rendered GUI.
So for instance, if your program had a JPanel say called cardHolderPanel, that used a CardLayout, this held by a variable say called cardLayout, and you've already added a "card" JPanel to this holder for accounts, say called accPanel, and if the accPanel had a method to set its currently displayed account, say setAccount(Accoint a), you could easily swap views by calling the CardLayout show(...) method, something like:
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt) {
Account selAcc = getSelectedAccount();
if(selAcc != null) {
accPanel.setAccount(Account.getDeepCopy(selAcc));
cardLayout.show(cardHolderPanel, "Account View");
}
else {
showErrorMessage("Please select an account to view");
}
}
simple question here but can't find any resources to help me.
In an ActionListener I've made I bring up a new window. I've guessed you keep the main window visibility to false. but how do I close the newly opened window and set the visibility of the old window back to true?
here is my main window which brings up the new window.
JFrame window = new JFrame();
public void actionPerformed(ActionEvent e) {
window.setVisible(false);
Clock clock = new Clock(initialSize);
while(clock.isVisible()){
window.setVisible(false);
}
window.setVisible(true);
}
});
and here is my new windows ActonListener which closes that one and opens old window.
public void actionPerformed(ActionEvent ea){
jframe.setVisible(false);
}
I'm new to this, so it may be an obvious answer here which I am unaware of. the current code here is a desperate attempt. Thank you.
but how do I close the newly opened window and set the visibility of
the old window back to true?
add WindowListener to JFrame/JDialog
override windowClosing event, here you can to call setVisible(false) for JDialog and setVisible(true) for JFrame
change default close operation for JFrame/JDialog to HIDE or NOTHING_ON_CLOSE
setParent for JDialog
I decided to put "close" buttons on my JFrames. If I test just that JFrame by itself, it works fine, but when I try to close it after opening it through another class, it won't close.
Here is the code:
JButton btnClose = new JButton("Close");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
btnClose.setBounds(282, 666, 96, 50);
contentPane.add(btnClose);
I have tried using frame.dispose() and frame.close() and super.dispose() but the only one that works is system.exit(0); but then that exits the whole program.
The issue:
If I test the JFrame by itself, the frame closes fine.
If I open the program and navigate to that specific JFrame, the close button does nothing.
Please advise.
Did you add a display statement in the ActionListener to make sure the code is being executed?
If the code is being executed, then the problem is probably that the frame variable has in invalid reference.
You don't need to keep a reference to the frame. Instead use something like:
Window window = SwingUtilities.windowForComponent( e.getSource() );
window.dispose();
Use setVisible(false); on the frame. Then call dispose.
I wrote a simple application and I want show delay of it with JProgressBar Plese help me ;
I want show JProgressBar with Joptionpane , with a cancel button and it should be modal
this is my source code :
class CustomFrame extends JFrame {
private JProgressBar progressBar;
public CustomFrame() {
long start = System.currentTimeMillis();
myMethod();
this.getContentPane().setLayout(null);
this.setSize(200, 200);
//JOptionPane. ?????
this.setTitle("JFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
long end = System.currentTimeMillis();
System.out.print("\nTime: " + (end - start));
}
public void myMethod(){
try {
java.io.File file = new java.io.File("i://m.txt");
BufferedReader input =
new BufferedReader(new FileReader(file));
String line;
while ((line = input.readLine()) != null) {
if (line.indexOf("CREATE KGCGI=") != -1 ){
System.out.println(line);
}
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
}
Thanks ...
There are a couple things that you will need to do to get this to work:
You should be aware of threading issues in Swing. Your GUI painting should be done on the EventDispatchThread and disk I/O should be done in a worker thread. See this tutorial, the SwingWorker JavaDoc, and SwingUtilities.invokeLater for more detail
You will then want to get the size of your file (file.length())to determine how to scope your progress bar (myProgressBar.setMaximum(length))
When you iterate over the lines in your file, you will want to trigger an update to your progress bar (myProgressBar.setValue(myProgressBar.getValue()+lineLength)).
A couple points by way of critique:
your constructor shouldn't go off and do all of your work (ie load your file and pop up an option pane with the ability to cancel. the constructor should just do the work needed to create the object. you might want to consider having your constructor create your class, and then have the work that needs to be done to be called separately, or within something like an init() method.
It isn't clear what you are doing with the JFrame as superclass. JOptionPane is a class that will pop up a very basic modal dialog with some text, maybe an icon or input field. It isnt a panel that is embedded in a dialog.
As JOptionPane is a very basic construct for creating a basic message dialog, it might be easier to use a JDialog, which can also be made modal. JDialog will allow you to add buttons as you please, where as a standalone JOptionPane will require you to use Yes/No, or Yes/No/Cancel or OK/Cancel etc.
If you still want to use JOptionPane, and only show a cancel button, you can instantiate a JOptionPane (as opposed to using the utility show* methods), with the progressbar as the message, and the JOptionPane.CANCEL_OPTION as the optionType param. You will still need to put this into a JDialog to make it visible. See this tutorial for more details:
JOptionPane (constructor)
Creates a JOptionPane with the specified buttons, icons, message, title, and so on. You must then add the option pane to a JDialog, register a property-change listener on the option pane, and show the dialog. See Stopping Automatic Dialog Closing for details.