Removing default look of JButton in Swing - java

I am making an application in Java Swing and I have to use buttons there.
I have a button and I have set background image of JButton using ImageIcon. I have made this image using Photoshop When I click on the button then its default color is shown. I am also setting background color to white but this is not solving my problem.
How to remove default color of button?

Make your image as a button :
BufferedImage buttonIcon = ImageIO.read(new File("MyImage"));
button = new JButton(new ImageIcon(buttonIcon));
and set your button like so :
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setContentAreaFilled(false);

Related

JFrame Questions (deleting textimages adding new ones)

So I am making a game, and I want to know if it is possible so when lets say "You created a fire" it deletes that line and then displays "Your fire turns into ashes".
two more,
I want to make a jframe background, and let's say I "login" the background disappears, and a new background comes in(but the game, not a background).
I want to add a image icon( already added) (IMAGE = FIRE) it deletes that image and a new one appears( IMAGE = ASHES), how can I do this?
public class FireLabel extends JPanel {
public LabelDemo() {
super(new GridLayout(3,1)); //3 rows, 1 column
JLabel label1;
//Create the first label.
label1 = new JLabel("You created a Fire", JLabel.CENTER);
//Add the labels.
add(label1);
add(label2);
add(label3);
}
The context is a little light, however.
For swicthing from one view to another, I would suggest using a CardLayout, which would allow you to change from the login screen to the game screen.
If you're using JLabel as you primary output...simple change the text or icon using setText or setIcon as required...
To change that, use JLabel.setText. You will then have to call validate for the change to take effect.
I recommend swapping out the content pane for this. Put the login screen in a JPanel and set that as the content pane, then when needed, change the content pane to a second JPanel for the game.
Use the same technique as #1. Use a JLabel to display the image.

Image autofit in a JButton?

I am new to Java and I met a little problem.
Can someone please tell me how I can set the Image of a JButton to be auto resizable/fitable/stretchable in a JButton when I resize the whole JFrame? The JButton is in a JPanel with a GridLayout.
It will be great if someone could write a brief explanation. I need this to complete a calculator I am working on.
Add component listener to the button.
Resize the Image on Fly and update Image on the button.
Image img = icon.getImage();
Image newimg = img.getScaledInstance(NEW_WIDTH, NEW_HEIGHT, java.awt.Image.SCALE_SMOOTH);
icon.setImage(newimg);

How to set " very dark blue " background color to the JButton?

I am trying to create a simple button which should look like this-
JButton connectBtn = new JButton("Connect");
Color blue = new Color(77,176,230);
connectBtn.setBackground(blue);
But the problem is the background blue color is not as dark as it should look.
I have tried all the below possibilities,but of no use:-(
connectBtn.setBackground(Color.blue);
connectBtn.setBackground(Color.BLUE.brighter());
connectBtn.setBackground(Color.decode("#0099cc"));//i tried simply #0099cc just to get any dark background
Kindly help me in setting this color to JButton background.
Thanks.
PrintScreen -> Paint -> Colorpicker tells me that the color of your button is
1691D9
or
(22,145,217)
Zoom shows that your button is painted with a gradient that includes several shades of blue, as well as anti-aliased text. You'll probably need a suitable ButtonUI, as shown here using GradientPaint. Note the use of RenderingHints to suggest aliasing settings.

JButton from image

I want to make JButton from image. But when i do it like this:
private void AddMainActionsButtons(Container powZawartosci){
JPanel mainActionButtons = new JPanel();
JButton applyButton = createImageButton("obrazki/apply.png");
mainActionButtons.add(applyButton);
powZawartosci.add(mainActionButtons);
mainActionButtons.setBounds(150,530,400,90);
}
private JButton createImageButton(String imagePath){
ImageIcon icon = uploadImage(imagePath,"");
JButton button = new JButton(icon);
return button;
}
JButton looks bad, because image is inside button component, and in fact I got image on image. How can I create as big JButton as my image, and how to cover it by image in 100%. In other words: how to make my image be a Button?
You need to take the space out of the buttons by removing the border. See this answer for details.
Seen above is the single image split into 9 parts. The N/S/E/W are buttons (East is activated - shown with a red border) and the rest are labels - each containing the relevant part of the original image.
See also this answer for a version that uses simpler images with JToggleButton instances.
Have you tried setting the background to a transparent color?
This will remove the funky grey border around your image that often looks bad.

JToolBar buttons with different size images-NetBeans

I'm having problem in my JToolBar. I'm using images of different size.
Tool bar is not looking good. How can I use different size images in the JToolbar buttons?
How can I show the button label below each image?
How can all the buttons be aligned from the top, left corner?
For example,
java.net.URL imageURL2 = cldr.getResource("Images/report2.jpg");
ImageIcon aceOfDiamonds1 = new ImageIcon(imageURL2);
btnReport = new JButton(aceOfDiamonds1);
btnReport.setMaximumSize(new Dimension(49, 43));
btnReport.addActionListener(this);
jToolBar1.add(btnReport);
I'm using images of different size.
Resize them. Either once at time of build (recommended), or at run-time.
And I want to show the button label below each image
This is close.
newJButton(String,Icon);
And i want to show the button label below to the each image?
With the setVerticalTextPosition and setHorizontalTextPosition methods of JButton:
// Place text below icon
button.setVerticalTextPosition(SwingConstants.BOTTOM);
button.setHorizontalTextPosition(SwingConstants.CENTER);
How to show all this buttons align from the left top corner
As Andrew Thompson said, the images all have to be the same size.

Categories

Resources