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.
Related
Learning GUIs for an assignment, and would appreciate some advice...no code, for the aforementioned reason.
I have a programme with a JFrame, and a single JPanel, that contains methods that take keyboard input using the KeyEvent class.
My class that extends JPanel, has the following in it:
setFocusable(true);
requestFocus();
I've since modified the programme, to include a second JPanel (to add a control panel to the right containing 4 JButtons).
The problem is that the keyboard input no longer works on the first JPanel when I run the programme (it did before).
The keyboard input on the right JPanel only works before the second JPanel has been added to the programme. If I remove the second JPanel, keyboard input works, when it is there it doesn't....
I realised that I had setFocusable(true); and requestFocus(); in both panels, so I deleted it from the second panel (with the JButtons), but it still doesn't work. I sense it's a focus issue.....any advice?
Do I need to look at KeyBindings (don't know what this is yet, but a few similar threads suggest it)....
We can't tell you what is wrong based on two lines of code. We have no idea what panel1 and panel2 do or what components they contain.
My class that extends JPanel, has the following in it:
A couple of problems with that:
You should NOT be using the requestFocus() method. Read the API for that method and it will tell you the appropriate method to use.
Even if you do use the appropriate method, you can't use that method in the constructor of a class. Requesting focus on a component can only be done to visible components on a GUI.
Do I need to look at KeyBindings
Yes, Swing has newer and better API's than AWT. In AWT you didn't have a choice. In Swing you should be using Key Bindings. All Swing components do use Key Bindings and Actions. One of the main reasons is you don't have the focus issue.
Start by reading the Swing tutorial for Swing basics. There are sections on:
How to Use Key Bindings
How to Use Actions to get you started.
A Key Binding is simply the process of mapping a KeyStroke to an Action.
Also, in the future, when you ask a question post a proper SSCCE that demonstrates the problem.
If you mean with "pass back and force" that the focus should move on tab keypress between those two, use a FocusTraversalPolicy where you define who should get focus in which order.
When you click on a button, that button receives the keyboard focus. If you want the focus to revert back to the first JPanel, then you should add an ActionListener to each button, and in the actionPerformed() method of the listener just add the line:
firstPanel.requestFocusINnWindow();
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 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.
I have music Applet which uses keyboard, mouse and GUI buttons. When applet is first loaded keyboard events work fine, as do mouse events. However, after I have pressed one of my GUI buttons the mouse events still work but the keyboard events don't, and don't start working again until I refresh the applet.
After hunting around on the net I found some posible solutions, I've tried adding button.setFocusable(true); and button.addKeyListener(this); to all my buttons, and and my panels. No effect at all. I've seen recommendations for converting to a JApplet and using key binding, but surely there must be a simpler way?
Sorry for the lack of code, I've been working on this project since I was a newbie and it's a bit of a mess, and very long!
Any help much appreciated!
button.setFocusable(true); and button.addKeyListener(this); to all my buttons
For JButton use Swing Action or default implementations for ActionListener, rather than KeyBindings (for Swing based Container and JComponents), nor using KeyListener
EDIT
if isn't there really important reasons, don't use prehistoric AWT Applet, use JApplet, may be sufficient would be plain JFrame
Try to cut the problem area out of your project and put it here. Its highly probable, than while localizing the problem area you'll find some errors already.
If your project is a mess already, then the first and the most important thing you should do, is to order it. If it is a mess for you, that means you don't understand it. So, it simply can't work. That is your first and main error.
Can anyone tell me on how the swing components drawn from the palette and put into the Jfame can work together? For example If a button pressed can the result show on the textArea, Or Can I drag some listed objects from the Jlist in my JFrame onto the JtextArea/JTabpanel?
Thank you!
You will need to add an ActionListener to your button and in the actionPerformed method you can write the code to display a result in your JTextArea:
http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html
More info on buttons:
http://java.sun.com/docs/books/tutorial/uiswing/components/button.html
You can drag objects from a JList to another component (such as a JTextArea or JTable), but you would probably not drag them directly onto a JTabbedPane. You'll have to code this behavior with the DnD API:
http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html
Rob has a pretty good answer.
Here's my 2 cents,
"For example If a button pressed can the result show on the textArea"
If you are using Netbeans, this is pretty quick and easy to do:
Double click your button.
The IDE will jump to the actionPerformed method. (and put in the required boiler-plate code)
Place your code in the generated actionPerfored method
e.g "jTextArea1.setText("hello world");"
I'd advise reading the swing tutorial on the sun website:
http://java.sun.com/docs/books/tutorial/uiswing/