Disposing a JFrame in another Class [duplicate] - java

This question already has an answer here:
Dispose JFrame from another class
(1 answer)
Closed 7 years ago.
I have a quick question regarding JFrames and disposing them properly. I have a game that has multiple levels, I wish to dispose of the frame in use when another is created with a new level.
The program I am currently working on extends a JFrame which has always confused me as I don't know what that JFrame is called.
Anyway, I have another class that extends a JPanel. In this class I have a method that, when the game state is completed, removes all instances and closes the JFrame. Yet this does not work because I cannot get the frame of the frame, instead I get multiple instances of the same JFrame.
So my set up looks like this:
Class 1 extends JFrame
....
....
....
Class 2 extends JPanel
...
...
method(clears everything + gets new JFrame for new level)
...
Sorry if that is vague, I don't want to post hundreds of lines of code for a short question. I know others have asked this question but I can never seem to get it to work for me.
So once again, my question is simply how do you close a JFrame in another class method.
(Please note everything works perfectly, I just can't close the frame without it breaking completely on me)

dispose() is an insance-level method. If you have object o, which is of JFrame or an inherited class, then o.dispose() should dispose it. If you are not sure that o is initialized when you want to dispose it, then
if (o != null) {
o.dispose();
}
If you simply call dispose() from somewhere, you will get an exception if that object/class does not have a dispose object. So, if you want to dispose o from class A, then you should call o.dispose() in one of the methods, but make sure that you initialize o correctly before that.

Related

Use main method in JFrame class or just call the JFrame? [duplicate]

This question already has answers here:
Java/Swing GUI best practices (from a code standpoint) [closed]
(2 answers)
Closed 4 years ago.
So what's the best practice, use a JFrame as the one that contains the main method, or just create a main class, and call the JFrame?
I'm not sure it matters at all, but i'm wondering if there are any advantages using the main in JFrame class or not?
I personally try to keep the main method in its own class because maybe you want to do more than just start the JFrame at startup.
But that really depends on what you want to do, if you only want to start the JFrame then the main method in the JFrame is also good.
You may also want to have a look at this post

Minesweeper Java Inheritance

I have 5 classes for this project, Button Minefield GUI MenuBar Game.
Button is an individual mine cell.
Minefield contains a 2d array of buttons, and operations that concern all of the buttons.
GUI is the portion above the MineField grid, displaying the reset button, time lapsed and how many flags remain,
Game puts all of the other classes in Panels and places them in the applet. No game logic here.
Here is a screenshot of my applet
I'm having problems when it comes to using inheritance. If I can solve this issue, I can do the other problems too. It has to do with invoking a method in the super class.
One of my problems:
When a Button mine cell is right-clicked, the mouse listener picks it up and changes the text of the JButton to "F" for flag. However, I want to update the counter of how many flags are available, which is a variable in the Minefield class. Finally, I want the GUI class to listen for changes to that variable and update the JLabel.
In the MouseListener for Button:
if (e.getButton() == MouseEvent.BUTTON3)
{
advanceCurrentState();
if (currentState == "FLAG")
super.setNumFlagsRemaining(-1); //update the Minefields variable
}
Back in Minefield:
public void setNumFlagsRemaining(int delta)
{
numFlagsRemaining += delta;
}
But this doesn't work. When I am debugging, super.setNumFlagsRemaining(-1) is creating a new instance of Minefield and I cannot update the variable inside the super class. I thought that it would update the existing object of Minefield, but I read that Java does not pass by reference, so I was confused as how to do it. If you need to see additional code please say so, I tried to not clutter this post with 5 whole classes and say, "fix it".
You do not provide enough code so I can only guess here.
I think you are confused with key word super
It actually mean to call method from the super class which you had inherited.
I doubt that JButton inherits anything from your class Minefield. Usually it is in opposite way.
Also count of the fields is stored and displayed in the JLabel. So you, actually, have to call method of the class which has this JLabel and takes care of total count.

Choosing between inheritance vs composition in swing GUIs [duplicate]

This question already has answers here:
Why shouldn't you extend JFrame and other components? [duplicate]
(5 answers)
Closed 7 years ago.
I was used to do java swing programming with netbeans drag and drop, and never really cared too much about the code it generated. Now I am in the process of learning to code GUIs without drag and drops.
The fundamental problem occurred to me was, whether the window I am going to make IS_A JFrame or HAS_A JFrame. i.e whether to use inheritance or composition.
if MyWindow is a JFrame
public class MyWindow extends JFrame{
}
if MyWindow has a JFrame
public class MyWindow{
private JFrame frame;
}
Both seems fine to me. But I guess there should be a right way to do it out of these two. What is the correct way, and why?
If you want your class to behave as a window, then, you should extend it from JFrame (at least in my opinion). To my knowledge, this is how you should go about it.
If on the other hand, you want a class which has access to a window then you would go with the second option. That being said, you would still, at some point need to initialize a class which extends from JFrame.
EDIT: The answer to the question does say that, however it also says that it depends on what you are after. If I am understanding the answer correctly (maybe others could comment on this), if you have a scenario where you need a frame to print a list to a table, you could have a class which extends Frame and provides a utility method which takes in a list and prints it to a table. Your logic would then instantiate this class instead of the actual JFrame and use it to show the data.
My approach is that usually, I have a class which extends JFrame and provides a series of methods which make printing data easy. I would then have another class, which links logic and view layers. This class will have a reference to the JFrame extending class.

Button opens new JFrame multiple times. How do I stop this?

I am using to different classes: one holding a main JFrame with a button, and one holding a new JFrame that is called upon at a button press.
if( event.getSource() == noteBtn ) { MiniPad.pad(); return;}
(MiniPad.pad() references the class and pad() method on the new JFrame)
When I removeAll() on the JPanel that hosts the button, and then revalidate() and repaint(), the button opens the JFrame multiple times, which isn't what I want it to do at all.
Is there a way to tell the MiniPad class that you can't have more than one copy of the JFrame open at any one time? I extend the JFrame by the way, in case that's any help.
Edit: Everything below is valid programming knowledge, but you might also want to consider having MiniPad extend the JDialog class instead. I haven't used it before, but its implementation looks a lot like JFrame. You might not actually have to change much in your MiniPad class. The documentation is here: http://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.html
If you're wondering why, check out Andrew Thompson's post here.
--
From what I understood of your question, MiniPad extends JFrame, and the pad() method creates a new instance of the MiniPad class. The simplest solution would be to turn the MiniPad class (at least through the pad() method) into a singleton. A singleton is a type of class where only one instance (or object) can exist at any given time. By calling a static method (in this case pad()) you check to see if an instance of the object already exists; if it does, simply use that existing object:
public class MiniPad extends JFrame {
//whatever code you have
private static MiniPad padInstance = null; //the singleton instance of your MiniPad
public static MiniPad pad() {
if(padInstance == null)
padInstance = new MiniPad();
//If you want to reset the object every time you call the method for whatever reason, do it here
pad.setVisible(true); // I believe this is all you want to do
}
}
This should do what you want. By calling the pad() method, only one MiniPad will ever show up.
However, if I read your question wrong, let me know and I will revise my answer.
Info on singletons:
http://en.wikipedia.org/wiki/Singleton_pattern
The best solution is to open a modal dialog instead of the frame. See The Use of Multiple JFrames, Good/Bad Practice? for more.

java passing JFrame acceptable?

I've got a class that extends JFrame...
I then assign it to a variable
JFrame frame = this;
I now need to pass this frame into an actionListener class called LoginListener but to do that I need to pass it to a class AppLoginComponents first that extends JPanel and this class has an instance of LoginListener(information,signin, frame)
So it's like this
Class extending Frame -> AppLoginComponents -> LoginListener
The reason i'm doing this is that the LoginListener needs to quit the application if the user hasn't logged in within 2 minutes. I am planning to dispose the frame. I don't think a static variable would be ideal would it?
No, don't use a static variable. Yes, it's fine to pass references around. You can also get a reference to the containing top level window via a SwingUtility method, called getWindowAncestor(Component c).
But this begs a question -- why have a class extend JFrame? Are you overriding any of JFrame's methods?

Categories