Rectangular Java Swing Radio buttons? - java

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

Related

Change size of JFrame according to user selected image

I'm about to write a program using Java and i want it to have the next behavior:
Start with a small screen, just one button (i'm going for the JMenuBar) for the user to select a image file (a country or state map)
Once selected the image file, i'll need to resize the frame to the size of the selected image, and put the image as background.
when the user clicks somewhere inside the frame (click on a state or city) the program will have to create a visual object there, a circle, square or any form in that coordinates.
will need also a listener in those objects to know when they are clicked.
Summary: User has to select an image and trace a graph on it.
I am not asking for the code to do this. I would like to have some ideas about which components use to achieve this since i have been reading and there are plenty of ways to set the background image and stuff. But, considering the requirements, can you recommend me which components to use? I am a bit short of time since i've been given only about a week to code this, otherwise i would try all the alternatives by myself.
Some answer like:
"use a label to set the background and then resize the frame by this way: (some stuff) and then you can create a class extending from JLabel to create the circles with the listeners...." that would be enough help
I hope I was clear, any idea is welcome
Many thanks!
If you're going to stick with Swing I would use a JFileChooser to select the image. Once you got the image you can easily resize the JFrame by using the frame.setSize(image.getWidth(), image.getHeight());
To listen for mouse clicks inside your JFrame you need to use a MouseListener, make sure to add it to the frame, I always forget doing that.
Not sure whether you've succeeded drawing images/shapes at all. If not, you need to use a JPanel, check this topic if you need extra help.
If you are going to use a "JFrame " then you should definitely use Swing JFrames JPanels, and JLabels (as well as any other JComponents you need.) to accomplish this. Use only one JFrame. Use JPanel as the content pane/background for your JFrame and add everything else to it. But I would also suggest learning and using JavaFX because its the newest and I think it would be the easiest to use to do something like this. But if you only have a week and you know some swing use what you know. If you need more information post some code. Or ask a more direct question.

Focusing in different JPanels? (java)

Quick question, I have 3 JPanels, one has a keylistener and it draw my game graphics, and you can alter it by moving your character around, etc. and I have a second JPanel which is just a JTextArea, and it just displays text, and finally I have another JPanel which has a JTextArea and a JButton, the text area takes inputs and you may hit enter key or click the button to send the text which will appear in the other text box. But my question is, how can I request focus in each panel? How do i transfer the focus with lets say a mouse click?
I know that if I dont constantly call requestFocus(true); for the first JPanel with displays all the graphics it wont work for some reason. I believe its because its always painting and maybe it looses focus on every go around. But how to I in all get the focus to transfer with a mouse click, etc?
One possible solution is to not use a KeyListener. Instead use Key Bindings which are much more forgiving with regard to focus. Have a look at the Key Bindings tutorials which Google can help you find in a jiffy.
For more specific help, consider creating and posting an sscce.

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

Program with split screen, how can I change the content [Java]

I had just a few class of java on college, but I want to do a thing that I don't know how.
Is basically a window with a SplitPane, on Left side I have a menu made with toggle buttons, and on the Right side I need to change the content based on each button.
Theres any way to design the ViewA and ViewB on separated JFrame Form and load then inside my Right Side when I click on menu items?
Another idea is, put the ViewA and ViewB put a JTabbedPane on the Right Side, and hide the Tabs. So there's any way to hide the tabs?
I have none experience developing in java, any problem about this concept (difficult, loading time, memory, maintenance), If you guy know a better way to to this, I just don't want a lot of windows popping up.
A really simply way would be to simply have a set of jPanels in the right side, with only one ever set to Visible.
Basically, for each toggle on the left side, you will have an Event Listener that does this:
private void toggle1ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(true);
}
Simply changing the true value depending on the individual toggle.
In Netbeans, if using the GUI editor, you can simply double click the toggle button to generate the listener and appropriate method, then fill in the code for it.

Making a game menu using Canvas?

I am already familiar with JButtons, JLabels and such, but I want to start making a game a very "colorful" menu. Is there a way to do this using Canvas (like adding a mouse listener and make some buttons in PhotoShop and detect if the mouse hovers over and clicks the button), or is there a better way?
Since you're already familiar with JButtons, you may find it easier (and more practical) simply to extend the existing JButton and modify its appearance, so that it looks like an image rather than the traditional grey button.
Amongst other things, it means you get all the standard button behaviour for free, including special cases, and in exchange all you have to do is override a couple of methods.
Have a read through the accepted answer for the following question, which explains pretty much exactly how to do that:
Creating a custom button in Java

Categories

Resources