Basic Java GUI design - java

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() ).

Related

Two classes implementing one ListnerInferface, only the first class runs events?

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.

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.

Access one instance of a jdialog from different class java

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.

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.

Swing - Organization of JFrames (or JDialogs???) in my program

I have a main controller class that shows a JFrame containing a JTable and, for each row in this table, I have to show a specific "form" on doubleclick.
This secondary window will need information about the specific row selected on the main JTable, as well as some objects saved as fields in the controller class.
An conceptual example of what I need to do is the following:
I have a set of Shops (listed in the JTable in the main JFrame) and, on double click on a row, another window has to appear, allowing for a management of the Shop (sending orders, checking deliveries, etc...).
My question, me being such a newbie with Swing, is: what is the best organization for a common pattern like this one?
Should I model another JFrame and pass as arguments all the data that I could happen to need (I really don't like this), or should I pass only a reference to the Controller class (this would be against the MVC pattern, I think).
Or maybe I should use a JDialog instead of another JFrame? The thing is that, really, the functionality that I need from this second window are a little too big for a dialog, I think...
I am confused, any tip/suggestion/advice will be much appreciated!
Thank you
Regards
Or maybe I should use a JDialog instead of another JFrame?
Bingo.
I actually don't like the idea of having a listener inside my Model class (aka Shop) – implementing the ActionListener. I think I would extend the JDialog class (let’s call it MyJDialog) then when a row is double clicked … create a new instance of MyJDialog class and pass in the Shop object in the constructor. Within the MyJDialog class you can modify the Shop object by calling mutators (setters). Moreover, the Shop class should have a way of notifying observers when a property is changed – take a look at PropertyChangeSupport.

Categories

Resources