I want to change the default color when selecting a JButton. Per default it is "lightblue". When pressing a JButton the background changes for some 0.2 seconds to lightblue.
I don't want to setBackground(). I want to change the default property. Is there something like:
JButton button = new JButton(text);
button.setSelectionColor(Color);
This is controlled by the LAF.
You might be able to use:
UIManager.put("Button.select", new Color( 0, 255, 0) );
at the start of your program before you create your buttons.
However, this will apply to all buttons in your application.
Related
i want to make my JCheckboxes in a JTable bigger (for Touchscreen), but it doesn't change the size.
I tried it with
setPrefferedSize
setSize
What should I do?..
I assume you mean you want a bigger check box. If so then you need to create images to represent the unselected and selected icons of the check box. Then you can create a renderer and editor using these icons. Finally you would need to increase the height of each row in the table. The code might look something like:
Icon normal = new ImageIcon(...);
Icon selected = new ImageIcon(...);
JTable table = new JTable(...);
table.setRowHeight(...);
TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
JCheckBox checkBoxRenderer = (JCheckBox)renderer;
checkBoxRenderer.setIcon( normal );
checkBoxRenderer.setSelectedIcon( selected );
DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Boolean.class);
JCheckBox checkBoxEditor = (JCheckBox)editor.getComponent();
checkBoxEditor.setIcon( normal );
checkBoxEditor.setSelectedIcon( selected );
IMPORTANT NOTE: This was only tested with the default 'Metal' look and feel. I do not guarantee that this will work for any other look and feel. Also I am not entirely sure how it works because it is admittedly a bit of a hack.
I was able to solve this in a slightly different way.
I wanted to use the existing images and just apply a scale to it. I am already scaling the font of my application using the UI defaults and so I have a rather large font. I wondered if I could leverage that and scale the check boxes accordingly.
After scouring the internet and trying a bunch of things I came up with this method:
public static void scaleCheckBoxIcon(JCheckBox checkbox){
boolean previousState = checkbox.isSelected();
checkbox.setSelected(false);
FontMetrics boxFontMetrics = checkbox.getFontMetrics(checkbox.getFont());
Icon boxIcon = UIManager.getIcon("CheckBox.icon");
BufferedImage boxImage = new BufferedImage(
boxIcon.getIconWidth(), boxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
);
Graphics graphics = boxImage.createGraphics();
try{
boxIcon.paintIcon(checkbox, graphics, 0, 0);
}finally{
graphics.dispose();
}
ImageIcon newBoxImage = new ImageIcon(boxImage);
Image finalBoxImage = newBoxImage.getImage().getScaledInstance(
boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
);
checkbox.setIcon(new ImageIcon(finalBoxImage));
checkbox.setSelected(true);
Icon checkedBoxIcon = UIManager.getIcon("CheckBox.icon");
BufferedImage checkedBoxImage = new BufferedImage(
checkedBoxIcon.getIconWidth(), checkedBoxIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB
);
Graphics checkedGraphics = checkedBoxImage.createGraphics();
try{
checkedBoxIcon.paintIcon(checkbox, checkedGraphics, 0, 0);
}finally{
checkedGraphics.dispose();
}
ImageIcon newCheckedBoxImage = new ImageIcon(checkedBoxImage);
Image finalCheckedBoxImage = newCheckedBoxImage.getImage().getScaledInstance(
boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
);
checkbox.setSelectedIcon(new ImageIcon(finalCheckedBoxImage));
checkbox.setSelected(false);
checkbox.setSelected(previousState);
}
What it does is get the size of the font from the checkbox's font metrics. Then using that it derives a new icon based on the icon found in the 'Look and Feel'.
One odd thing that I am not able to explain is how the icon for the checkbox in its 'un-selected' or default state, changes to the 'selected' icon, when I am accessing the same property to get each one.
I start by saving the state of the control so I can restore it at the end. This is done because in order for the icons to be set properly, the state needs to be unchecked when you first request the icon from the UIManager and then it will need to be checked when you request the icon the second time to get the 'selected' icon.
I am not entirely sure how the UIManager works or why the checkbox icon changes when we call the same property just by setting the 'selected' value of a single checkbox, but that is what is required in order to get both the necessary icons.
If you did not want to base the size on the font you could easily just pass in the height and width as parameters and use them instead of the font's height when setting the buffered image size.
I might mention that this same methodology works with radiobuttons
Does anybody know how to set the background color temporarily on a Button.
buttons[randomI][randomJ].setBackgroundColor(Color.rgb(155, 17, 30));
Here is what I have but I only want to set the background color of this for a certain period of time. I understand that a way to go about this is to remove the background color after a certain period of time but I don't know how to remove background color. I referenced: How to get JButton default background color? and the solutions did not work for me.
In the top answer:
btn.setBackground(new JButton().getBackground());
JButton does not exist for me and using new Button().getBackground says that it can not resolve constructor. So is there any way to temporarily set the background color?
Store the previous color of the Button:
int color = 0; // Black default
Drawable drawable = buttons[randomI][randomJ].getBackground();
if (drawable instanceof ColorDrawable) {
color = ((ColorDrawable) drawable).getColor();
}
Set the new color temporarily:
buttons[randomI][randomJ].setBackgroundColor(Color.rgb(155, 17, 30));
and after some period restore the initial color:
buttons[randomI][randomJ].setBackgroundColor(color);
My current java version is:
$java -version
java version 1.8.0_40
I'm very new to swing and trying to write my first minesweeper app. Now, I need to disable a button when clicking on it and print a number of adjacent mines (with the appropriate text color depending on a number: 1- blue, 2 - green, 3 - red, etc). So as suggested in this answer I wrote the following:
JFrame frame = new JFrame("Hello swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JButton button = new JButton();
button.addActionListener((ActionEvent ev) -> {
button.setSelected(true);
button.setText("<html><font color = red>3</font></html>");
button.setEnabled(false); });
But it still goes grey when disabling:
But enabled button look just as I want it to:
What to do with this issue? How to fix this problem with disabling buttons?
Maybe you know some workarounds
Use Icons instead of text.
Then you can use:
JButton button = new JButton( icon3 );
button.setDisabledIcon( icon3 );
I have tried to add a scroll view to a JOptionsPane, so that the information window can handle more text. It does add a scroll pane to the window. However, it acts funny on scrolling. The first visible text is shown clearly, but when you start scrolling, the text parts will overlap each other, until the text area is all black.
Do you have an explanation of how this can be and maybe a solution to the problem?
My code looks like this:
public void showInfoNoTranslation(String info) {
frame.requestFocusInWindow();
// create a JTextArea
JTextArea textArea = new JTextArea(info, 6, 25);
textArea.setEditable(false);
textArea.setBackground(new Color(255, 255, 255, 0));
textArea.setBorder(BorderFactory.createEmptyBorder());
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
// if (textArea.getLineCount() > 5) {
JScrollPane scrollPane = new JScrollPane(textArea);
JOptionPane.showMessageDialog(_frame, scrollPane, "title", JOptionPane.INFORMATION_MESSAGE);
}
Call textArea.setOpaque(false); instead of setting it's background-color to fully transparent and it will work.
From the docs:
public void setOpaque(boolean isOpaque)
If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.
The default value of this property is false for JComponent. However, the default value for this property on most standard JComponent subclasses (such as JButton and JTree) is look-and-feel dependent.
Is there any way to easily delete an ImageIcon completely from the screen? I can't find one anywhere.
ImageIcon image = new ImageIcon("candle.gif");
image.paintIcon(this, g, 150, 80)
For example, if I wanted to get rid of "image" later on when a button is pressed, what would be the appropriate code? (Not for the button, I know how to do that).
Don't reinvent the wheel. The easiest way to paint an Icon is to use a JLabel. The code would be something like:
ImageIcon icon = new ImageIcon(...);
JLabel label = new JLabel( icon );
panel.add( label );
If you don't want to display the icon anymore then in the ActionListener of your button you simple do:
label.setIcon( null );