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.
Related
So I'm doing a project for school where I'm supposed to simulate a supermarket. I'm pretty far into it, but I came across a problem I haven't had before. Long story short, (among others) I have a controller class and a JFrame class.
In the main class and method, I have an object of the Controller class. In the controller cass I have an object of the JFrame class. When I run the project, the main makes a controller, which makes a JFrame.
Now, I want to add a button into the JFrame that signals the controller I want to 'move on' the simulation. I'm kind of stumped as to how to accomplish this. Basically, I want to add a button into the JFrame that will call a method in the controller class when clicked.
I'm happy to post code if necessary, but it's more a problem of 'how do I do this?' than fixing something that is broken.
Extend the JFrame with your own JFrame, which has an overloaded constructor that takes an instance of the Controller. Then when you create an instance of the JFrame from the Controller you pass it a reference to itself using the "this" keyword. When the button inside the JFrame needs to notify the controller it should be able to call some getController() method. Of course, this is all conjecture without seeing some actual code :)
So I have the classes;
ControllerOne.java
and
ControllerTwo.java
They both implement MyWindowListener interface with the method void actionHappend();
In my View class implementing ActionListener I have added the windowListener to a JButton. And when the actionPerformed() is run in the view, it will check the source of the event and check if it was the button for example.
If it was the button, then the windowListener.actionHappend(); will run, and some code in the controller will run.
The problem is now, only the first Controller "ControllerOne" that is implementing this interface seems to run the method actionHappend();
Question:
Cant multiple classes that implement a the same custom Listener be started in the same event?
Cant multiple classes that implement a the same custom Listener be runned in the same event?
Answer, yes they can. A single event can trigger multiples of the same listener type.
Now if you want to make my work more challenging, and my answer quality improved, ramp up the quality of your question with more detail and code.
As an aside:
The WindowListener interface is already present in core Java, and as suggested by its name, it is built to listen for state changes of Swing and AWT windows such as JFrames and JDialogs.
This interface does not have an actionHappend() method declared.
I can't picture when anyone would ever add a WindowListener to a JButton. An ActionListener yes, but WindowListener, never.
In fact, a JButton doesn't even have a an addWindowListener(WindowListener wListener) method.
You state, "in my View class implementing ActionListener..." -- note that as a general rule, view classes should not implement listener interfaces, except in very simplistic toy programs.
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.
I have a JFrame that houses my many JPanels that represent different parts of my application in which you can do certain calculations.
I have a JDialog that is created in my Main.java class (extends the JFrame) and is designed to be used as an output window (i.e. whatever calculations are performed in different JPanel classes, the result should be appended to this output windows JTextArea).
My question is, how do I access this JDialog from my other classes? I don't want to instanciate another Jdialog but use the existing window... I have getters and setters for the JDialog but I am a little lost on how to get the connection between the instance of my OutputWindow class in the Main java file and the other JPanels that house the different parts of my application.
Appreciate the help.
If what you want is just provide access to an inner class from classes defined elsewhere, as long as it is public and static you should be able to it.
If you are going to have just the one instance throughout all your project you should use the Singleton pattern to properly ensure this.
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() ).