0

What's the name of the JFrame when the class extends JFrame ie:

public class FrameClass extends JFrame{
    public FrameClass (){
        super("name")
    }

Basically, I want to use it in an ItemListener for a JRadioButton so I have an internal method. Would I just have to not extend the JFrame class and make a JFrame object to use it inside the internal method, or is there a name I could reference it by, such as FrameClass.

EDIT: I guess there is a slight confusion towards what I put in text above. This is how I'm trying to use it:

public void itemStateChanged(ItemEvent e) {
setUndecorated(true);
setResizable(false);
vc.setFullScreenWindow();
}

I cannot use this because that uses ItemEvent e.

Nicholas Eason
  • 290
  • 4
  • 13
  • If all you want is to be able to refer to this "frame" (maybe `Component` is a better term) by name, is very easy to do. When you pass a `String` to the `JFrame` constructor, the attribute being set is the `title` attribute which is inherited from `java.awt.Frame`. The use of multiple `JFrame` windows in a single application is strongly discouraged. If all you want is to change the "view" being shown in the window, you can use a `CardLayout`. This is very easy to do and Oracle provides a very nice and simple [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). – hfontanez Nov 12 '14 at 20:26
  • 2
    @hfontanez Specifically I needed to use the JFrame for a parameter within the `itemStateChanged` method, causing `this` to return ItemEvent e. In the selected answer, he noted that I could just use `FrameClass.this` to reference the JFrame. The `setFullScreenWindow` method requires a `Window` as a parameter, so if anything `Window` is a better term. But nonetheless, the question was answered. – Nicholas Eason Nov 12 '14 at 20:29

3 Answers3

0

If your class extends JFrame then it is a JFrame, so you can treat it as a normal JFrame object.
You can invoke JFrame methods and use it as parameter for functions that take JFrame as an argument.

public class MyFrame extends JFrame { //it's still an instance of JFrame
    ...
}
misko321
  • 493
  • 7
  • 14
  • I want to use it as a parameter, but since it is inside of the `ItemListener` method, I can't use `this` because that calls `ItemEvent e` – Nicholas Eason Nov 12 '14 at 20:17
0

By extending JFrame class you can add design your Frame in the contructor or methods in the class.

public class FrameClass extends JFrame{
    public class(){
        super("name")

    }
}

Now create an object for your Frame class and access it.

public class TestFrame2
{
  public static void main ( String[] args )
  {
    FrameClass frame = new MyFrame("Hello"); // construct a MyFrame object
    frame.setVisible( true );             // ask it to become visible
  }
}
BDRSuite
  • 1,594
  • 1
  • 10
  • 15
  • But! don't forget about [The use of multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657). Be sure to read it. – Frakcool Nov 12 '14 at 20:10
  • This will create more than 1 JFrame though, and I'm wanting to use the one "in the example" within the ItemListener method. The question is asking if I need to create another JFrame, or if I can use the one that I made by extending the class. – Nicholas Eason Nov 12 '14 at 20:16
0

I'm not exactly sure what you mean by the name of the JFrame. If you just want to pass a reference to your apps JFrame somewhere then well if you extend JFrame your FrameClass class is the JFrame. In this case /this/ is the right "name".

Christian Frommeyer
  • 1,390
  • 1
  • 12
  • 20