How to make my GUI work - java

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/

Related

java swing jpanel display when clicking a button

i'm new to swing. i've created a jpanel form using the swing palette design. It has jlabels, jtextfields and a jbutton. when the button is clicked, the data entered in the jtextfields is inserted in the database through the model.
i don't want the the jpanel to be always displayed so i would like to add another button that displays this jpanel form when i click on it. is it possible to perform this using the palette design knowing that the source code is locked in the initComponent private function, or do i have to re-code all the components and layout myself?
thanks for your time!
"knowing that the source code is locked in the initComponent private function"
You can edit the auto-generated source code, as see here in this answer.
"that displays this jpanel form when i click on it. is it possible to perform this using the palette design"
Sounds like you want to swap views. You can do that using CardLayout and yes, it can be done from the design view. You can see more at How to Use CardLayout with Netbeans GUI Builder

external panels in swing

I have something like this...
Its a bunch of JLabels within JPanels inside a
--JFrame
--JPanel
--Set<JPanel>
--JLabel object contained in each JPanel object cotained in the set
I want to create an external panel/frame so that each time I hover over each individual JPanel, a new frame/panel pops up giving me some data. This will essentially overlap over the JFrame.
Its pretty brief but I just need some guidance as to what I need to look up.
Maybe you can just use JToolTip. When the mouse hovers over the label for a couple of seconds it will display automatically. See the section from the Swing tutorial on How to Use Tool Tips.
Or if you want something more complicated use a MouseListener. On mouseEntered() you can display a JPopupMenu or an undecorated JDialog.
You can start by reading the Swing tutorial on How to Write a Mouse Listener.

Trouble with Java GUI

I am creating a GUI for my project. I am completely stuck at one point. I was able to create a GUI which will make some simple queries like the release number, command name, and few other boolean questions.
Now I want the user to press CONTINUE button to move to the next page of the GUI form. I have created the continue button, but now I need to register an event to it. This is where I am stuck. I have no idea what event can I register to it which will move the GUI to next page. (By moving to the next page I mean, the different GUI pages we see when we are installing a software for our computer.)
For instance, if I am installing iTunes, I'd first select the radio button for "I accept the terms and conditions" and then I'd press the CONTINUE or the NEXT button to move ahead. If I need to come back, I'd press the BACK button.
One logical answer would be to create another GUI form and then link it to the one I created first.
EDIT:: this is the first time I am working in Java, so I might have ignored some obvious facts.
Look into using a CardLayout, adding several JPanels to the CardLayout-using container, and then to swap to the next view (the next JPanel), call the layout's next(...) method in the JButton's ActionListener. You could also randomly access components held by the CardLayout using its show(...) method.
To learn more about this including sample code, please have a look at the CardLayout Tutorial, and the API.
Well, register an ActionListener or an Action with the button. To do that wizard style, have a look at the CardLayout layout manager to switch cards or use a tabbed pane, hide the tabs and switch them inside the action or action listener.
if i understood, i think you should use the CardLayout
If you are using Swing, you can using several instances of JPanel over a one instance of JFrame.
You can have something like that structure:
JFrame
+------JPanel:root
|
+---JPanel:current // This panel change by other instance
|
+---JPanel:controlPanel // This panel contains you button
In you button need add a ActionListener with the method addActionListener
The panel control may need changes your elements, may the text of button or remove the listener and change by other.
I hope that can help

Rectangular Java Swing Radio buttons?

I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src

JRadioButton itemlistener not firing

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.

Categories

Resources