Using many panesl in a class that extends JPanel - java

I have a main class which extends JFrame, and its content is contained in other classes which extend JPanel.
Now, in those other classes, I want to use several panels to group the content in a good order.
Is it possible to use many panels( by creating several JPanel objects in that class) in this class that extends JPanel?

JPanel extends JComponent, which extends Container, so JPanel is a container, so it contain other Component.
So, yes, you can do this and in fact, depending on the context and requirements, is actually a good idea.
You could take a look at this example and this example
mKorbel makes a valid point. It is generally not recommended to extend from top level containers like JFrame, instead, use something like JPanel as you primary application interface (adding other containers and components to it as you see fit) and adding this frame to an instance of JFrame which you create

Related

Is a JPanel represented as a class of its own in a Class Diagram?

I am developing a Java application that will have a JFrame with a tabbed pane inside it consisting of eight tabs. Each tab will have a JPanel in it.
If I am to draw a class diagram of this application, would the JPanels be represented as classes on their own under the JFrame's class or would I just include their attributes and behaviors in the JFrame's class?
No, each component/container is a class on its own, there isn't any relationship between them. However if you have a class that extends JFrame, then you would put that class as a child/subclass of JFrame.
I drew this up as an example for you:
See if you can get any useful information from this post: Representing swing components in UML class diagrams.
If not, just post your comments so that I can research more and help you.
If you're really drawing a class diagram, where one figure on the diagram represents one object/class, then of course you wouldn't put attributes of a JPanel within a JFrame figure, because that class does not have those attributes.
Class diagrams show relationships among classes, along with their attributes and sometimes their methods. A class diagram for a GUI application bears little resemblance to the GUI design of the application.

Get which Jframe opened another Jframe

Say I have a JFrame called childJframe.
If I create a new childJFrame from two different JFrames. How can I get which particular JFrame created the childJFrame.
Thus:
public class myPage1 extends javax.swing.JFrame{
// on a Button clicked
childJFrame cjf = new childJFrame();
cjf.setVisible(true);
}
And the Second class is
public class myPage2 extends javax.swing.JFrame{
// on a Button clicked
childJFrame cjf = new childJFrame();
cjf.setVisible(true);
}
How can I find out if cjf is an instance of myPage1 or myPage2?
The Window class, which JFrame descends from, has a getOwner() method that will return the "owner" Window for any child windows.
But having said that, child windows should be JDialogs, not JFrames as your application should have one and only one JFrame, and I believe that JFrames don't have owners, so that this method may return null. If you need to change "views" within the JFrame, use a CardLayout, and if you need to display child windows, use dialog windows such as JDialogs and JOptionPanes. Please read: The Use of Multiple JFrames, Good/Bad Practice?, for more on this.
But having said this, I do have to wonder if your question may in fact be an XY Problem where you ask "how do I fix this code" when the real solution is to use a different (read -- more "object oriented") approach entirely.
If your child windows must really be JFrame instances (I suppose ChildJFrame extends JFrame), I think the simplest solution consists in keeping track of the parent JFrame in the ChildJFrame instance. Since ChildJFrame is a custom class of yours, this is easy:
Add a JFrame (or Frame or Window) attribute to your ChildJFrame class;
Add to ChildJFrame a constructor that takes a parameter that will be assigned to the above attribute;
When you create a ChildJFrame instance from one of your JFrame-derived classes, just add this as a parameter.
Then you have everything you need to interact with the parent JFrame.

What does "extends JPanel" mean?

I have a class called UserInterface. In the code public class UserInterface extends JPanel, does "extends JPanel" mean that there is a JPanel within the JFrame (the JFrame is the application window) or does it mean something else?
Also, if it means that there is a JPanel within the JFrame, how can I access the JPanel (for example, set its background colour)?
extends in Java means is-a. So your class is a JPanel; it's inheritance; a fundamental part of OOP.
Since your class is a JPanel, if you want to set it's background color somewhere inside the class you'd just do setBackground(yourColor) from outside would be ui.setBackground(yourColor) assuming of course that you named the instance ui.
JFrames are not the same as JPanels if that's what you were getting at.

JPanel Observer

I created a game in a separate panel class from the GUI main class. I am trying to find a way to update the score and level text fields found inside the GUI main class, when a method inside the panel class is executed. I tried to use the observer design pattern but the panel class already extends JPanel and can only extend one item. Is there an alternative to this?
Implementation of observer pattern does not require that you extends your new JPanel class. You should to define the interface(s) and implement that/those interface(s) in your class.
I recommend to you take a look on this.

Basic Java GUI design

In the GUI book we use in class there are many examples of how graphical user interfaces are made in Java. So many examples, that I'm very confused regarding which one should be used when it comes down to a big application.
So I've seen examples
in which the main class extends JFrame
where the JFrame object is created inside the main method
where the main class extends JFrame AND implements ActionEvent interface
where Listener classes are declared inside the main class
Sure, I can work with all of these, but right now, as I don't have any kind of experience, I don't see the benefit of using any of them. Is actually one of them the correct way to do it or it depends on my sittuation?
Thank you!
"Is A" or "Has A"? This is the question that should be asked when considering extending a class. If the new class "Is A" frame, extend frame, but if the class just needs a reference to a frame, don't extend.
In fact, if a custom component is required, extend a JComponent or JPanel, then add that to a frame, ..applet, window, JInternalFrame, dialog, constraint of a layout, part of a split pane..
Listeners
As to the listeners. Rather than traverse a huge if/else structure in the single actionPerformed() method to determine the required action, it is more optimal to either:
Create a listener for each control that needs it.
Create an instance of an AbstractAction that might be used for multiple controls ('copy' button, menu item etc.).
Summary
So (generally) for the:
JFrame, don't extend.
Listeners, create and add as needed.
Honestly, it depends on the situation. One basic rule when coding is to "code to abstract classes or interfaces".
So, in a nutshell, have a class extending (or implementing) a JFrame (or whatever interface or class) and/or have one doing the same thing with ActionListener.
It is all about the maintainability, flexibility and cleanness of your code.
Standard approach: use EventQueue in method main, that creates main form. In that case all your operations will be asynchronous
in which the main class extends JFrame
the main calss doesn't have to extend JFrame. if it doesn't you should create a JFrame object like you do with any other class
where the JFrame object is created inside the main method
If the MainClass extend JFrame it created inside the c'tor (in the super() ).

Categories

Resources