Adding background in jButton (netbeans gui builder) - java

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.

Related

JButton Icon Change

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.

Java - Extra Window Button

Google Chrome Recently had an update that added an extra button onto to the top of the window that allowed you edit your account settings. I have a great use for an extra button like this one but I do not know how to make it. So, how can I add an extra button at the top of the window?
This is what I would like to do or have in mind.
This is more of an amateur/simple answer but it could be possible just to make a title bar with all of the drop downs without text until you reach the button you want and customize that

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.

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

animation for drag and drop JLabel

I have ask alot of question about drag and drop for JLabel , but im learning from them ,im trying to drag and drop JLabel with image icon inside it , i want to be able to make a copy from it and drag it while user can see the image moving with the mouse and drop it in another JLabel , also i want to move it from one JPanel to another,how i can do this ?
In principle, you need the java.awt.dnd package.
Get a DragSource, associate it with your source JLabel. Create a DropTarget, associate it with your target JLabel.
Add the right listeners (DropTargetListener, DragSourceListener), and react on the method calls appropriately.
Okay, seriously: Read the tutorial, try to implement it, and come back if you have more concrete problems.

Categories

Resources