JPanel Observer - java

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.

Related

Using many panesl in a class that extends JPanel

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

Java ActionListener in another Class

I am trying to build a small notepad application using the Java Swing library. I have a main function which calls a constructor of JFrame (NotepadClass). In this NotepadClass I have a MenuDesigner class something like this:
this.setJMenuBar(new MenuDesigner());
The MenuDesigner class extends JMenuBar which calls actionListener (MenuActionListener) which is written in another class.
Now my question is: If I click on "new" menuItem, the title which is in NotepadClass should change. How do I access a class that is two levels up?
Which concept of Java should I use to accomplish this?
use Swing Action instead of ActionListener, this API is designed for your purpose
post an SSCCE demonstrated your issue, just about JFrame with JMenuBar, JMenu and one, two JMenuItem(s), noting else
Without seeing your code it's difficult to give a definitive answer, but one of the reasons to write a separate class to build your menu is that you can pass instances to the class.
this.setJMenuBar(new MenuDesigner(notepadClass));
This is one reason why it's good to have a model class or classes when you're building a GUI.
You can pass an instance of the highest level model class to all of your GUI components, and each component can get or set the parts of the model class that they represent.
You could pass down the NotepadClass in the constructors and provide a method to change the title.
Another option is to build the ActionListener inside the NotepadClass or let NotepadClass itself implement the Actionlistener interface so you can access the variables or methods.
Why is your actionListener for your menu in another class?
You could create a new class that implements ActionListener in which you can add you own logic. This way you can reuse it in another file.
Also, you should probably de-couple your MenuDesigner class by moving it into its own file.

How can I pass "this" into an action listener

I have a MainWindow class that extends JFrame and is the only frame in my application. I have several JPanels to change its content though. I would like to pass the MainWindow around so I won't have to make a global copy of it.
Here is the problem. I have a fake JMenuBar in MainWindow that I use to go around and look for screens. In their JMenuItem listeners, I cannot pass MainWindow as this to the JPanels.
Is there a way I can do that, like maybe marking the class final or do I have to create a new MainWindow each time I have to pass in one. It doesn't carry around any data so its not crucial and the performance isn't a major concern for a small final project like this but I want to know if there is a better way?
Are you unable to pass this because it would reference the listener? If so, you should be able to use MainWindow.this.
Or create a reference to this outside the listener and pass that as the parameter.
Create the MainWindow class as the Outer class and extend this to JFrame which u already did, . Now in the MainWindow class, create as many inner classes as you want and let the handle the ActionListener method in different way..
eg:
MainWindow extends JFrame {
MainWindow(){
}
class panel1 extends JPanel implements ActionListener{
}
class panel2 extends JPanel implements ActionListener{
}
}
You can also implement Singleton Pattern, so that makes sure there is only one instance of MainWidow.
Try any of this approaches:
Make MainWindow singleton.
Mark as final a variable in the method when you create the listener.
Use MainWindow.this
For your case use the singleton approach, with that you can access freely from any place of your project.

How to implement my code in MVC Java?

I am making a game which will have three games implemented in it. I started off my code in a single class, now that I want to move it around into a MVC format I am having errors. I started off with writing all the code in GameView Class, I now want to move some of the code from this class to a SView Class. The problem is that I have intialised the JFrame in GameView class and it cannot find the variable frame when I move my code to SView class. So then I have been told to make it into a panel and then move it but it's just messing up my game.
You have to organize things a little differently. Some things to consider:
Is GameView your main window? Make it so GameView extends JFrame.
Is SView a panel? Make it so SView extends Panel.
Another thing to think about: SView is some panel that you want to add to your GameView, but it's not the responsibility of SView to add itself. Instead, simply instantiate and assemble these components somewhere outside both of these classes, f.e.:
GameView gameView = new GameView();
gameView.add(new SView());
You could also have GameView add a new SView() in its constructor code instead.
Anyway, the main point here is that a component should not concern itself with adding itself to its parent, just with creating its own content/children.
Good luck.
Some suggestions:
The main GUI would use a CardLayout so you can swap JPanels easily.
The main function of the select game menu will be to swap JPanels, so the game selected is visualized and then initialize the selected game.
Work on each game individually. Gear each individual game class towards produding a JPanel to hold the game. Give each game class a main method for testing that would create a JFrame, create a game class instance, place the instance's JPanel into the JFrame, initialize the game and display it.
Once the game is working on its own, try adding it to your larger GUI, the one that can show multiple games.
Consider having each game class implement a common interface for common functionality. For example you could give the interface an initialize() method that each game class would override.
Each game class would need to be MVC-based so that it has a model, a control class(es) to interact with the model and a view class that displays the state of the model on the JPanel as noted above.

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