I have ControlP5 running on the main frame. And I also have two additional frames using ControlP5, they are contained in a class that extends PApplet. Like the example - http://www.sojamo.de/libraries/controlP5/examples/extra/ControlP5frame/ControlP5frame.pde
They all display fine, and seem to be working. But then clicking a button does nothing, it doesn't even highlight when you mouse over it. So I'm assuming the controlEvent function for those classes isn't being called.
The main frame contains a ControlP5 declaration in the setup, and has a controlEvent function in the main pde file. That handles the buttons pressed on the main frame perfectly well. But then when you click the buttons to initiate one of the additional frames, the frame loads fine and displays the buttons located on it, but they don't do anything when clicked. Nor do they look like buttons as they don't highlight like the buttons on the main frame do.
I'm not sure what exactly is wrong, as there are multiple instances of controlEvent, although one in the main file and the other two inside a class. Does anyone have any ideas as to why the events aren't being picked up on the additional frames? (I would've included the code, but it's really long and could overcomplicate things).
Thanks.
In case anyone views this to find an answer, I just used G4P in unison. It was annoying having two different GUI packages, but it did work.
Related
I am making project with GUI. The thing is, that I have a button and what I need to do is that after clicking this button I need to change Frame layout. For example, like when you are installing some program and you click "next" button, the Frame layout changes and you can see some different content. Basicly, dynamic wizard.
I have tried use another Frame, but it opens in another window and that is not what I want. I want to open it in the same window.
Another thing I have tried is set visibility of these components I don't want to be displayed to false, but I find it unprofessional and it is overlook in making a desing, when I have components over themselfs.
So do you guys have any idea? Thank you.
Most of the times for a wizard like GUI, you should have JFrame and a set of JPanels. In each step you can pass the shared data as constructor arguments to each panel, and when you are making one of them invisible and make another one visible, you can get some date from the previous step panel and pass it to the next step panel(if needed).
It is very common that your panels extend the JPanel and have some argument in their constructor(s). You use these data for initializing your panel and managing the state of the overall progress.
There is no a total plan for all situations. So you should decide what to do which is best fit for your case.
Try not to have multiple JFrames.
Hope this would be helpful.
I want to fill values of multiple jTextBox from a jFrame into another, using accessor methods like
String getNameVal()
{
return jTextBox1.getText();
}
How to call these methods from another jFrame?
Suggestions:
It sounds like your GUI code is geared towards making JFrames, and if so, you will want to avoid this. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this.
More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
This question has direct bearing on your problem. I will guess that your main problem isn't how to give classes getter methods, and how to have other classes call the getter methods. More often then not, when faced with the issue of extracting information from one GUI view to another, the issue is one of when to extract the information. If you displayed your second window as a non-modal JFrame, and then had the calling class immediately extract the data from that second JFrame, you'd get nonsense data, because you'd be extracting data before the user would have time to interact with the 2nd window and enter data.
One possible solution to this when using non-modal windows to get information from the user is to use a WindowListener so you can be notified when the user has completed his dealing with the second window, and so now data can be safely extracted.
Often better is for the 2nd window not be non-modal, as JFrames are, but instead to be a modal window such as a modal JDialog. When the calling code displays a modal dialog, all code flow in the calling code stops until the dialog is no longer visible. In this situation, no WindowListener is needed since you will know exactly when the dialog has been dealt with -- on the code line immediately after you set it visible -- and so can extract your data from it with ease.
A nice variant on this has already been mentioned in by Andrew Thompson in comments -- use a JOptionPane. Don't poo-poo this option since JOptionPanes are powerful tools, likely much more powerful than you realize as they can hold fully formed complex JPanel views, and behave just as described above, as modal dialogs.
If you need more specific help, then please don't hesitate to comment to this answer. Also if so, then consider creating and posting a Minimal, Complete, and Verifiable Example Program where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem.
Edit
For my mcve code examples of the above suggestions, please my answers to the following StackOverflow Questions:
Using a modal JDialog to extract information
Using a JOptonPane to extract information
I assume the textfields are present in frame1 and you want to access them in frame2. The following can be a way to achieve this:
First create getters for all JTextFields that you have in your frame1. Alternatively you can have them in a panel and call getComponents() method.
Create a private variable of JFrame type in your frame2.
Modify the constructor of frame2 to receive the frame1 object and assign it to the private variable of JFrame type.
Now you can create a close() method in frame2 which disposes the frame2 and sets frame1 to visible.
But in my opinion you should create a class which handles the data in these textfields. Initialize the class object in any button click of frame1 and check for any inconsistency in the input. I can guess there is something wrong with your design.
I've been working on a console based program that acts as an inventory of Plant objects.
I have a parent class "Plant" that has child classes of "Flower", "Weed", etc... These objects are added, removed, displayed, searched through another class containing the main method and methods for the actions above.
The methods/actions are chosen by the user via console input processed with a switch statement.
My question is this: We are adding a GUI to this console based program using a JFrame, JPanels, etc... Would the proper way to go about this be to create a new class for the interface and a new main method in that class to run the program? I would of course change the former main method to a method called by the new main.
Moving from a console program requires a lot more than just changing main methods. GUI programs are event driven. So you're not going to be running endless loops like you would in a console program.
What I mean by event driven is, for example, a button get pressed, an event is fired. You as the programmer are responsible for coding what happens when that event is fired.
So some advice.
You should go through the tutorials and learn some of the basic components and how they work. Some of the basic ones are JLabel, JTextField, JButton
You will definitely need to focus on how to write event listeners. Some of the basic ones you may want to focus on are ActionListener for button presses MouseListener for mouse events.
Should learn to layout out components correctly. Some of the basic layouts you may want to focus on are GridLayout, BorderLayout, and FlowLayout
You want to learn about the basic containers like JFrame and JPanel and learn their capabilities
The Swing tutorials are always a good place to start. Once you pick up on the basics, then move on to some more complex material.
I have developed my Java code in Netbeans, and now I want to develop the GUI for my application.
The application communicates with a server, so it's going to have a login frame for sure. After that there will be a main frame. From the main frame the user can choose where to go and as you can understand there will be a lot of frames.
I have already developed a version of the application where there are a lot of frames and using the "setVisible()", but I want something better looking. I want a stable frame and inside it, changing the panels or something similar.
How would I do this?
You might use JInternalFrames if you like them, or simply use a main panel with a CardLayout, and display the appropriate card depending on the clicked menu item, or the selected JTree node (as it's done in Windows Explorer and similar applications).
Use the Swing tutorial to get you started.
You can, at any time, make any Container object a JFrame's ContentPane. You can also add and remove Containers from any other Container. If you want a user to be able to jump to any of a dozen panels at any time, CardLayout, as suggested in another answer, is easily the best route. If, however, you intend to lead the user along a somewhat controlled path, you can start with a login JPanel. When that's done, you can create the next panel (a JPanel or something else), add it, and dispose of the first one. And so on until the user exits.
If the transition from one panel to another affects nothing else in the program besides the two panels and the parent Container (JFrame or descendant), this is probably the way to go. If a bunch of other places in the program need to know about the change, you'll want a more centralized mechanism, maybe using CardLayout.
Hello my Japplet is using a JComboBox and 5 JRadioButtons to draw and paint on the applet. Currently everything works except for my JRadioButtons which doesn't call the itemStateChanged() when a button is selected. So on the applet I can click on a button, but it won't fire. My combobox is also using the itemlistener interface and it works, but no matter what I've tried I can't get the buttons to send information/fire.
I noticed that it requires two clicks to select a button, and hope that a problem lies within that.
This is for a homework problem and if I could use an actionperformed and actionlistener I would :(. I need to use itemlistener. Below are examples of how I'm calling my radiobuttons, adding them to the shapes buttongroup, and adidng the buttons to the container c.
Thanks for any help!
Sorry to anyone reading this but because it was homework I'm not 100% sure I can keep the code up, PM me if you need help understanding it!
The code you posted is NOT a SSCCE!
Your question is about an ItemListener, so why did you post code related to a MouseListener and a MouseMotionListener? What does the custom painting code have to do with your problem?
How do you know you the ItemListener code isn't being invoked? Did you add a System.out.println(...) statement to the listener code? Test your code first using the "appletviewer". Its easier than using the browser. From the command line all you do is:
appletviewer P6.html
Or I find it easer to test the applet without even creating an HTML file. YOu can add the following line of code to the top of your source file:
// <applet code="P6.class" width="800" height="600"></applet>
Now from the command you you can test the applet just by using:
appletviewer P6.java
The problem with you code is that your radio buttons are defined as both class and local variables. The ItemListener generates a NullPointerException, because the class variables are null.
JRadioButton jrbOval = new JRadioButton("Oval");
should be:
jrbOval = new JRadioButton("Oval");
Also, you should not be overriding the paint() method of JApplet. Custom painting is done by overriding the paintComponent() method of a JPanel. Then you add the panel to the applet.
In general it's a bad idea to use your Applet class for so many Listeners. It just adds to the confusion and you now have a God object that handles too many events. See this discussion for more info:
Advantages to Nested Classes For Listeners in GUIs
The second issue is that you are heavily mixing java.awt and javax.swing objects, which are known to cause problems when they are put in the same container. You should definitely try splitting your Applet into 2 JPanels, one for awt stuff (paint, shapes, etc) and one for swing stuff (buttons, boxes, etc).
You seem to be using the ItemListener class properly, but when I saw that it takes two clicks to select a button, that was an obvious sign of awt/swing mixing/painting problems.