Java – how to return xxxsize from JComponent added to jlabel
How to correctly return xxsize from JComponent added to jlabel
1. Figure > > make layoutmanager work like JPanel, and jlabel returns size (0,0)
No. 2 Figure > > adding some preferredSize to jlabel
3. Figure > > calculated preferredSize from JComponent added to jlabel
4. Figure > > let layoutmanager change jlabel to JPanel. Now layoutmanager correctly calculates the dimension without using any xxsize
Notice that nimbus L & F is used, and all accessible L & F have the same output
import java.awt.*; import java.awt.event.*; import java.util.LinkedList; import java.util.Queue; import javax.swing.*; public class NimbusBorderPainterDemo extends JFrame { private static final long serialVersionUID = 1L; private JFrame frame = new JFrame(); private JPanel fatherPanel = new JPanel(),titlePanel = new JPanel(); private JLabel buttonPanel = new JLabel(); //figure ---> 4th. switch JLabel with JPanel //private JPanel buttonPanel = new JPanel(); private Queue<Icon> iconQueue = new LinkedList<Icon>(); public NimbusBorderPainterDemo() { iconQueue.add(UIManager.getIcon("OptionPane.errorIcon")); iconQueue.add(UIManager.getIcon("OptionPane.informationIcon")); iconQueue.add(UIManager.getIcon("OptionPane.warningIcon")); JButton button0 = createButton(); JButton button1 = createButton(); JButton button2 = createButton(); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(1); } }); int gap = 5; buttonPanel.setLayout(new GridLayout(0,3,gap,0)); buttonPanel.add(button0); buttonPanel.add(button1); buttonPanel.add(button2); // figure 1st. ---> without PreferredSize // figure 2nd. ---> //buttonPanel.setPreferredSize(new Dimension(160,30)); // figure 3rd. ---> /*Dimension dim = button0.getPreferredSize(); int w = dim.width; int h = dim.height; w = (w + 5) * 3; h += 4; dim = new Dimension(w,h); buttonPanel.setPreferredSize(dim);*/ titlePanel.setLayout(new BorderLayout()); titlePanel.add(new JLabel(nextIcon()),BorderLayout.WEST); titlePanel.add(new JLabel("My Frame"),BorderLayout.CENTER); titlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); titlePanel.add(buttonPanel,BorderLayout.EAST); fatherPanel.setLayout(new BorderLayout()); fatherPanel.add(titlePanel,BorderLayout.CENTER); frame.setUndecorated(true); frame.add(fatherPanel); frame.setLocation(50,50); frame.pack(); frame.setDefaultCloSEOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.setVisible(true); } private JButton createButton() { JButton button = new JButton(); button.setBorderPainted(false); button.setBorder(null); button.setFocusable(false); button.setMargin(new Insets(0,0)); button.setContentAreaFilled(false); button.setIcon(nextIcon()); //button.setRolloverIcon(nextIcon()); //button.setPressedIcon(nextIcon()); //button.setDisabledIcon(nextIcon()); nextIcon(); return button; } private Icon nextIcon() { Icon icon = iconQueue.peek(); iconQueue.add(iconQueue.remove()); return icon; } public static void main(String[] args) { SwingUtilities.invokelater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception fail) { } UIManager.getLookAndFeelDefaults().put("nimbusFocus",Color.RED); NimbusBorderPainterDemo nimbusBorderPainterDemo = new NimbusBorderPainterDemo(); } }); } }
Solution
The default preferred size calculation uses the layout manager to determine the preferred size of components This means that the layout manager traverses all subcomponents to determine the preferred size of each subcomponent Use this calculation for jpanels to be used as containers
However, for other swing components, the getpreferredsize () method is always overridden to provide a reasonable size for a given component
For jlabel, the preferred size calculation takes into account the text and icons used Since you did not provide any preferred size of zero Of course, if you manually override this calculation using the setpreferredsize () method, the component will have the preferred size
Therefore, even if swing allows you to add components to any component and use the layout manager to layout subcomponents, these subcomponents will not be used in the preferred size calculation
This is not just a nimbus problem