java passing JFrame acceptable? - java

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?

Related

How to call a method from my panel class through the button-actionPerformed in the dialog-class, that get´s it´s instance in the panel-class?

I have a frame-class, that creates an instance of a panel-class.
and the panel-class creates an instance of a dialog class.
In the dielog class i have this:
public void actionPerformed(ActionEvent evt)
{
panel.changeLevel((int)levelList.getSelectedItem());
setVisible(false);
}
How must i start the method-call to use the method of the already existing instance of panel, that is created in the frame-class (not create a new instance)?
i want to sent a number back to the instance of panel, that has called the dialog box, and use it there to run a method
Since your actionPerformed method is in the dialog class, I'm assuming that you are using the dialog class as your listener.
If you make a controller class OR set the panel class to be your listener, you can place the ActionPerformed event in the controller/panel class, which will then have access to the methods/fields that it needs to be able to reach
(Using the panel is the easiest fix, using a controller or a custom listener class is the better design).

Disposing a JFrame in another Class [duplicate]

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.

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.

Retrieve object from a Java Swing component

I've been working on a Java Swing project where I need to retrieve the object/instance that created a panel in order to call a simple save method particular to that instance.
You have a JFrame with a JTabbedPane that has tabs created by instancing a class which builds a JPanel and adds it to the JTabbedPane, I need to find the specific instance from the selected JPanel/tab on the JTabbedPane to then call it's save method.
Any ideas?
Thanks for your time!
public class frame extends JFrame implements ActionListener{
Builds a frame dubbed "frame" that is static.
Builds a static JTabbedPane dubbed "pane"and adds it to the frame.
Creates a button that creates a new instance of sheet.
public void actionPerformed(MAGIC!){
See if a button on the panel has been pressed and uses the currently selected tab to locate the correct instance of sheet to run it's save method.
}
}
public class sheet extends JPanel{
In constructor makes a JPanel and adds it to "pane"
Describes a save method that outputs a variable unique to the instance.
}
I figured out all I needed to do was store new tab objects in an ArrayList derp. Thanks for your attempts though guys!
Rather than just connecting back to the original creator, my approach to this was to create / use an interface that expicitly supports saving. I created something for this in TUS, my sourceforge project
http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/io/filepersist/
Check out Persistable and Persistable2. Of course anything can be a Persistable, but the abstraction let's you get away from explicit ties back to the creator class
You can add a field in the new JPanels that point to the instance of the creator. I don't think there is any such method to point back to parent class in the API.
--EDIT--
You may want to check
http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
getSelectedIndex() may be what you are looking for.

design question for java SWING app

Note: This is for a SWING course I am taking.
I have an assignment to make a simple graphics package (draw circles, squares, etc).
I was thinking of having multiple dialog boxes for entering the shape parameters, i.e:
Point has x,y
Circle has x,y,radius
Rectangle has x,y,width,height
etc.
I was thinking of creating a super dialog class with X,Y and extending it to allow for Width,Height or Radius etc.
For example, the rectangleDialog would invoke the super constructor with the additional parameters required:
public abstract class XYDialog extends JFrame {
public XYDialog(PARAMETERS ... params) {
// build the dialog by iterating through PARAMETERS
}
}
public class RectangleDialog extends XYDialog {
public RectangleDialog() {
super(PARAMETERS.WIDTH, PARAMETERS.HEIGHT);
}
}
then the super class is responsible for building the GUI
Does this seem like a reasonable approach? Does this make sense?
Thanks
Yes, I think it's a good solution. But, as stated before, reconsider the naming of your classes. If you extend a JFrame, call it SomethingFrame. If PARAMETERS is a normal class, it should not be in capitals.
I would also suggest extending JPanel instead of JFrame, and let the one instatiating these classes determine if to put them in a JFrame or a JDialog. A JFrame creates a whole new window, and you normally only have one main window for your application, whereas dialogs and panels are created on the fly.

Categories