Java Button pressing visual style - java

JButton button = new JButton("Green Button");
button.setBackground(Color.green);
button.setBorderPainted(false);
button.setFocusPainted(false);
With above code, Java remove button's hover style.
Pressing down button makes button's background blue.
But how can Java make button's background never change (in this case keep green) while pressing down it?

You could use something as simple as...
JButton btn = new JButton("Hello");
btn.setBackground(Color.GREEN);
btn.setBorderPainted(false);
btn.setFocusPainted(false);
btn.setContentAreaFilled(false);
btn.setOpaque(true);
btn.setMargin(new Insets(10, 10, 10, 10));
Which paints a nice green button...
This has no "pressed" state rendering

Depend on what you are doing, you may be better off making a JPanel act like a button.
Another way which appears to work is to do button.setEnabled(false); although it fades the text.
The best way I can think of (I am not sure it is the best) is to do:
btnConvert.setContentAreaFilled(false);
btnConvert.setBackground(null);
btnConvert.setBorderPainted(false);
btnConvert.setFocusPainted(false);
btnConvert.setOpaque(true);
This will make everything transparent except for the text. You can then put a JPanel with a green background behind the button.
The following code works and does not change when you hover or click:
JPanel panel = new JPanel();
panel.setBackground(Color.green);
JButton button = new JButton("Button");
button.setContentAreaFilled(false);
button.setBackground(null);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setOpaque(true);
panel.setLayout(new BorderLayout(0, 0));
panel.add(button);
getContentPane().add(panel);
This is what it looks like when you hover or click:

Related

Remove focus from JComboBox Java Swing

I am trying to remove the dotted line from my JComboBox.
The initial ComboBox Initial JComboBox has a dotted line after it has gained focus:
After clicked
Now, I want to remove that focus. However I can't find it in the UIManager's options (https://gist.github.com/itzg/5938035). I have looked at this post from May 2018, but the answer is not there yet.
I have tried the following:
jComboBox.setFocusable(false);
UIManager.put("ComboBox.focus", new Color(0, 0, 0, 0));
but none of them worked.
Any help would be greatly appreciated!
You can do the following:
comboBox.setUI(new BasicComboBoxUI());
This will result in the following after an element was selected and get you rid of the dotted border:
For removing any 'kind' of focus border, you need to override the ComboBoxUI which is used for drawing the box and its component.
Here is the code I used to achieve the example:
public ComboboxWithoutDottedBorder() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception ignored){}
this.setVisible(true);
JLabel label = new JLabel("Label");
JComboBox<String> combo = new JComboBox<>();
this.setLayout(new BorderLayout());
combo.addItem("A");
combo.addItem("B");
combo.addItem("C");
combo.addItem("D");
combo.setUI(new BasicComboBoxUI());
this.add(label, BorderLayout.NORTH);
this.add(combo, BorderLayout.SOUTH);
}

how to stop JButton from changing position when a Jlabel is added before it

I have a JLabel (seriesInformationLabel) that is initially blank. There is a Jbutton (copyButton) next to it. Problem is everytime the JLabel loads a text value, the JButton moves to the right. How do I stop this JButton from moving?
private void init() {
super.initializeLayout();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
Box buttonPanel = Box.createHorizontalBox();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
buttonPanel.setLayout(null);
copyButton = new JButton(Utilities.getString("COPY"));
copyButton.setActionCommand(COPY);
copyButton.setEnabled(false);
seriesInformationLabel = new JLabel();
seriesInformationLabel.setAlignmentY(CENTER_ALIGNMENT);
seriesInformationLabel.setName(this.getClass().getSimpleName() + "_seriesInformationLabel");
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
buttonPanel.add(Box.createHorizontalGlue());
}
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
buttonPanel.add(Box.createHorizontalGlue());
Well the free space changes as the text of the label changes.
This means that the space available for the "glue" will change equally between the two glue components causing the button to shift.
If you don't want the button to move then you need to get rid of the second "glue" component:
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
//buttonPanel.add(Box.createHorizontalGlue());
Maybe instead add another rigid area if you don't want the button completely at the right edge of the panel.

Centering a button in a vertical box

Im creating a JDialog and adding components to it as such:
Window thisWin = SwingUtilities.getWindowAncestor(ancestorPanel);
final JDialog progressDialog = new JDialog(ancestorPanel, "There was an error");
progressDialog.setUndecorated(true);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(600, 600));
Next I add one JLabel, one JTextArea, one JScrollPane that contains a table, and finally a button as such:
label.setAlignmentX(JLabel.LEFT_ALIGNMENT);
label.setHorizontalAlignment(JLabel.LEFT);
area.setAlignmentX(JLabel.LEFT_ALIGNMENT);
Box vBox1 = Box.createVerticalBox();
vBox1.add(label);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(area);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(scroll);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(button);
contentPane.add(vBox1);
progressDialog.setContentPane(contentPane);
progressDialog.pack();
progressDialog.setLocationRelativeTo(ancestorPanel);
progressDialog.setVisible(true);
The results is exactly as I want except for the button. The label is on top (aligned to the left), then the text area comes below it (also aligned to the left), then below that comes the table, and finally the button, but I can't seem to make the button appear in the middle of the row. Its appearing on the left. I tried using the following line but it didn't work:
button(JButton.CENTER_ALIGNMENT);
How can I get the button to appear in the center of the last row?
I managed to solve the above by adding the button to a separate Box and then using Boxlayout to add both boxes to the panel as such:
Box vBox1 = Box.createVerticalBox();
vBox1.add(label);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(area1);
vBox1.add(Box.createVerticalStrut(7));
vBox1.add(scroll);
vBox1.add(Box.createVerticalStrut(7));
Box vBox2 = Box.createVerticalBox();
vBox2.add(button);
contentPane.add(vBox1, BorderLayout.CENTER);
contentPane.add(vBox2, BorderLayout.PAGE_END);

How to Change the Colour of a JLabel and JButton

I just changed the background colour of my frame using:
panel1.setBackground(Color.GRAY);
Now, it would probably look better if the text and the buttons are changed. How can I change the JLabel and the JButtons a bit more brighter? It will be much better when it is white because right now it looks like this:
Imagine that!
How to Change the Colour of a JLabel and JButton
To change the background color of JLabel and JButton, you can do this:
JButton btn = new JButton();
btn.setBackground(Color.WHITE);
JLabel lbl= new JLabel ();
lbl.setBackground(Color.WHITE);
lbl.setOpaque(true); //If opaque property is false, you can't see the color
To change the text color of JLabel and JButton, you can do this:
btn.setForeground(Color.WHITE);
lbl.setForeground(Color.WHITE);
JButton button = new JButton("test");//sample JButton
button.setBackground(Color.WHITE);//Set background color
button.setOpaque(true);//needs to be true in order to show color
JLabel title = new JLabel("I love stackoverflow!", JLabel.CENTER);
title.setForeground(Color.white);//simply set color

Java Swing: How to remove the default-spacing for JButtons in a JPanel

I'm busy writing a button menu for a Java Swing application and I am wondering if it is possible to remove the padding between JButtons that are added to a JPanel.
The JPanel uses a FlowLayout that is aligned left.
JPanel panelMenu = new JPanel(new FlowLayout(FlowLayout.LEFT));
The buttons are standard JButtons
JButton buttOne = new JButton("One");
JButton buttTwo = new JButton("Two");
I added the JButtons to the panel as normal
add(panelMenu, BorderLayout.NORTH);
panelMenu.add(buttOne);
panelMenu.add(buttTwo);
Everything works as expected but what do I need to do to remove the default spacing between the buttons?
I found a suggested solution online which is the following
buttOne.setBorder(null);
buttOne.setBorderPainted(false);
buttOne.setMargin(new Insets(0,0,0,0));
buttTwo.setBorder(null);
buttTwo.setBorderPainted(false);
buttTwo.setMargin(new Insets(0,0,0,0));
However this seems to remove the spacing inside of the button and not the spacing between each button.
Is this spacing produced by the FlowLayout? If so, how can I remove it?
// 0, 0 equates to horizontal and vertical offsets, the default is 5.
JPanel panelMenu = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
Should sort it!
The FlowLayout controls the spacing, the default is 5.
Use new FlowLayout(FlowLayout.LEFT, 0) to remove the spacing.

Categories

Resources