Swing JButton compresses JCheckBox focus state

I have a JPanel suite with Windows Look and Feel. I have 3 JCheckBoxes, including 2 disconnected ones that do not interfere with this problem. However, a non-disabled failure with JButton, which I later in this JPanel:

JCheckBox Code:

JCheckBox checkBox = new JCheckBox("TTxJIRA Bash"); checkBox.setSize(300, (checkBox.getFontMetrics(checkBox.getFont()).getHeight())); checkBox.setLocation(10, 100); checkBox.setVisible(true); checkBox.setSelected(true); checkBox.setBackground(new Color(0, 0, 0, 0)); checkBox.setFocusable(false); add(checkBox); 

And the JButton code:

 JButton button = new JButton("Install"); button.setSize(80, 25); button.setLocation(getWidth() - 100, getHeight() - 60); button.setFocusable(false); button.setVisible(true); add(button); 

When I hover over a button, hover over this check box, this will happen:

enter image description here

My wild assumption makes me think that this is due to the simultaneous focusing of the two components, but adding button.setFocusable(false); did not help.

0
java swing jbutton jcheckbox
source share
1 answer

Here is a small example that shows you how you could use LayoutManagers for this, because LayoutManagers will fix your problems with absolute positioning. (Note that this is probably not the best solution and that LineBorders are for visualization only)

Minor code:

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.LineBorder; public class LayoutManagerExample { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); JButton button = new JButton("Install"); JCheckBox cb1 = new JCheckBox("1"); cb1.setEnabled(false); JCheckBox cb2 = new JCheckBox("2"); cb2.setEnabled(false); JCheckBox cb3 = new JCheckBox("3"); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10,10,10,10); JPanel southPanel = new JPanel(); southPanel.setLayout(new BorderLayout()); southPanel.setBorder(new LineBorder(Color.BLACK)); JPanel westPanel = new JPanel(); westPanel.setLayout(new GridLayout(10,1)); westPanel.setBorder(new LineBorder(Color.BLACK)); JPanel southEastPanel = new JPanel(); southEastPanel.setBorder(new LineBorder(Color.BLACK)); mainPanel.add(southPanel,BorderLayout.SOUTH); mainPanel.add(westPanel,BorderLayout.WEST); southPanel.add(southEastPanel,BorderLayout.EAST); westPanel.add(cb1); westPanel.add(cb2); westPanel.add(cb3); southEastPanel.add(button, gbc); frame.add(mainPanel); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(500,500); frame.setVisible(true); } } 
0
source share

All Articles