List of usage examples for javax.swing JButton getFontMetrics
public FontMetrics getFontMetrics(Font font)
FontMetrics
for the specified Font
. From source file:Main.java
public static void setJButtonSizesTheSame(final JButton[] btns) { if (btns == null) { throw new IllegalArgumentException(); }//from w w w .ja v a 2 s.co m final Dimension maxSize = new Dimension(0, 0); for (int i = 0; i < btns.length; ++i) { final JButton btn = btns[i]; final FontMetrics fm = btn.getFontMetrics(btn.getFont()); final Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics()); final int boundsHeight = (int) bounds.getHeight(); final int boundsWidth = (int) bounds.getWidth(); maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width; maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height; } final Insets insets = btns[0].getInsets(); maxSize.width += insets.left + insets.right; maxSize.height += insets.top + insets.bottom; for (int i = 0; i < btns.length; ++i) { final JButton btn = btns[i]; btn.setPreferredSize(maxSize); } initComponentHeight(btns); }
From source file:GUIUtils.java
/** * Change the sizes of all the passed buttons to be the size of the largest * one.//from w w w .jav a 2 s.com * * @param btns * Array of buttons to eb resized. * * @throws IllegalArgumentException * If <TT>btns</TT> is <TT>null</TT>. */ public static void setJButtonSizesTheSame(JButton[] btns) { if (btns == null) { throw new IllegalArgumentException("null JButton[] passed"); } // Get the largest width and height final Dimension maxSize = new Dimension(0, 0); for (int i = 0; i < btns.length; ++i) { final JButton btn = btns[i]; final FontMetrics fm = btn.getFontMetrics(btn.getFont()); Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics()); int boundsHeight = (int) bounds.getHeight(); int boundsWidth = (int) bounds.getWidth(); maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width; maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height; } Insets insets = btns[0].getInsets(); maxSize.width += insets.left + insets.right; maxSize.height += insets.top + insets.bottom; for (int i = 0; i < btns.length; ++i) { JButton btn = btns[i]; btn.setPreferredSize(maxSize); } }
From source file:org.swiftexplorer.gui.AboutDlg.java
public static void show(Component parent, HasLocalizedStrings stringsBundle) { URI uri = null;/*from w w w . jav a 2s.c o m*/ try { uri = new URI("http://www.swiftexplorer.org"); } catch (URISyntaxException e) { logger.error("URL seems to be ill-formed", e); } final String buttontext = "www.swiftexplorer.org"; Box mainBox = Box.createVerticalBox(); mainBox.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 0)); StringBuilder sb = loadResource("/about.html"); JLabel label = new JLabel(sb.toString()); label.getAccessibleContext().setAccessibleDescription(getTitle(stringsBundle)); mainBox.add(label); if (uri != null) { JButton button = new JButton(); button.setText(buttontext); button.setToolTipText(uri.toString()); button.addActionListener(new OpenUrlAction(uri)); FontMetrics metrics = button.getFontMetrics(button.getFont()); if (metrics != null) button.setSize(metrics.stringWidth(buttontext), button.getHeight()); button.getAccessibleContext().setAccessibleDescription(buttontext); mainBox.add(button); } mainBox.add(Box.createVerticalStrut(10)); JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); panel.add(mainBox); JOptionPane.showMessageDialog(parent, panel, getTitle(stringsBundle), JOptionPane.INFORMATION_MESSAGE, getIcon()); }