I have just started using JFrame and I have a button that when clicked will perform calculations and output a result and a button that clears all of the users inputs. I know that I need to create two new classes which implement Action Listener. When it comes to creating the classes.
I was wondering if it is better practice to create the classes within my class that creates the JFrame or to make two separate classes and use getters and setters to modify the content of my JFrame class.
Related
I am working on a java swing project and I have a frame with multiple buttons and when someone clicks any of that button,user is asked for an input(say the user enters n) and a new frame is created with n text fields with another button "Ok"
{new frame is created inside the actionPerformed method.So my question is how to handle the action events for this Ok button.I tried using an anonymous class but I can't do much with variables inside it.
Source File:http://www.mediafire.com/file/9ed2h0yeyis5tk6/OSProject.txt
Use a modal JDialog or JOptionPane. See How to Make Dialogs
Define the functionality for the second window within it's own class, so you can isolate the functionality and management in more easily manageable manner. Provide setters and getters to allow information to passed between it and the class which needs to use it. If you're using a JOptionPane, this step makes it easier to define a custom component which can be displayed by it
I am writing a Java GUI application using the MVC design pattern. At first, I was putting the ActionListener classes for the JButtons as subclasses within the Controller. Then, I decided that I wanted the user to have the choice to be able to press a button OR the ENTER key to submit text in a text field. I read online that Action is better than ActionListener when you want more than one button click and key stroke to perform the same action.
I'm a little confused about where I should place the Action classes that extend AbstractAction when using the MVC pattern. Should they go in the in the Controller as subclasses the same way
I was originally doing the ActionListener classes?
Also, If I place the Action classes as subclasses, my Controller will be pretty full of subclasses since I'll have a lot of buttons in the GUI. Is this the best practice?
I ended up keeping the Action classes in the Controller and made the subclasses static like the answer to this question. I'm still not totally sure if this is the best practice, but everything works and I can reuse the classes for buttons and key strokes that perform the same action.
Basically, i have a button that works fine within one panel. I want the exact same button to display concurrently in another JPanel and react to the same actionlistener etc.
A component can only belong to a single parent, so you cannot add a button to two or more containers.
You can, however, use a ActionListener on multiple buttons.
A simple solution would be use an Action. An Action is a self contained unit of work, it carries with it the configuration details and actionPerformed handler.
You could create two separate instance of the Action and apply it to two different JButtons and get the same visual details and they would carry out the same work when triggered...
See How to Use Actions
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.
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.