I have JXButton[8][10](extends JButton) which each hold an Icon[2] IcAr
in a gridLayout().
Icon[1] is always the same icon while Icon[0] changes according to what i click.
For instance i click at JXButton[1][3] -> then i click at JXButton[4][7]
-> meaning JXButton[4][7].setIcon(JXButton[1][3].getIcon).
These are the IcAr[0] icons that each hold at the start.
When i finish this move i need some of the icons to change to IcAr[1].
When i make a move again i need now some of the icons to change back to IcAr[0].
With what i have tried so far i get unexplained behaviour.
(Sometimes it works,sometimes it makes the icons null,sometimes it doesnt change the ones i need changed).
If someone is able to write a simple example on how that should be done that thinks it might help i would appreciate it.
Any insights would be helpfull too.
Have you tried:
jbutton.setIcon(image);
Where jbutton is your jbutton and image is the new ImageIcon.
Related
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.
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.
When i try to add background in Jbutton in java by going to its properties and then icon, it then sets the background of the button but the text which i have written on the button,it moves right(outside the button area).What is the solution for this?
If you wanted to add an Image to your JButton with NetBeans follow these steps :
Right Click Source Packages, under Projects and Select New -> Other -> (Under Categories) Select Other -> (Under File Types) Select Folder.
Click Next, and provide a name to the folder. For Example resources, do check that for Parent Folder, src is written inside the field. Now Click Finish.
Now manually go to this location on your Computer and create a New Folder, say images, and then paste the IMAGE inside this folder.
Now Under Design Mode, select your JButton, and on the Right Side go to this JButton 's properties. Just under foreground you will see icon is written, click the Eclipse Button associated with it to open the window as shown in Figure below :
Do check, this FIGURE 1, to fill in your values and then Press OK
You are done adding image to your JButton.
If you want to use the image I used, here it is .
Yeah, I forgot to mention, for this, I had set horizontalTextPosition = CENTER and veritcalTextPosition = BOTTOM under Other Properties, inside Properties.
And Here is the output of the whole thing :
As far as I understand you are not setting background but you are setting Icon to button by this. And that is why you see Image on right side and text on left side. To add image as background to button you need to override paintComponent(g) method of JButon and draw your image in it.
What is the solution for this?
One 'solution'1 it to write the text on the image. This is not a very good solution though, since:
The text would need to be written on each icon used for the button (e.g. normal, roll-over, selected etc.).
It would then become your responsibility to choose an appropriate font face & size.
The text would not automatically change font or size when the PLAF changes.
Things like mnemonics and accelerators will not work with the image-text button.
It was only when writing the points that it became more clear just how quirky it would be to work with.
The JXTaskPane used in Java Swing has a title bar. There is a expand/shrink button in it. I would like to be able to add my own actions in the titlebar that would appear beside
the expand/shrink button.
How can I do this? I try myself many times, but do not get the expected result. I made a new JButton and set its location over the title bar coordinates, but it's added in the element.
I have attached a screenshot that shows a help type action in the title bar to show
you what I mean.
That's not possible. I'd recommend perhaps making a new JFrame pop up when you click the button to extend the dropdown menu. Creating your own JFrame popup would also afford you more creativity and customizability when it comes to the components you can put on it. I've not much experienced with this sort of thing, but you could try to do this. It might not be ideal though.
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