Example usage for javax.swing JButton setPreferredSize

List of usage examples for javax.swing JButton setPreferredSize

Introduction

In this page you can find the example usage for javax.swing JButton setPreferredSize.

Prototype

@BeanProperty(preferred = true, description = "The preferred size of the component.")
public void setPreferredSize(Dimension preferredSize) 

Source Link

Document

Sets the preferred size of this component.

Usage

From source file:BoxSample.java

private static void tweak(Vector buttons) {
    // calc max preferred width
    JButton button;
    Dimension dim;/*from   www  .  j  a va 2s . c  o m*/
    int maxWidth = 0;
    Enumeration e = buttons.elements();
    while (e.hasMoreElements()) {
        button = (JButton) e.nextElement();
        dim = button.getPreferredSize();
        if (dim.width > maxWidth)
            maxWidth = dim.width;
    }
    // set max preferred width
    e = buttons.elements();
    while (e.hasMoreElements()) {
        button = (JButton) e.nextElement();
        dim = button.getPreferredSize();
        dim.width = maxWidth;
        button.setPreferredSize(dim);
    }
}

From source file:GUIUtils.java

/**
 * Change the sizes of all the passed buttons to be the size of the largest
 * one./*  www. j  a v a2 s  .  c  o  m*/
 * 
 * @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:Main.java

public static void setJButtonSizesTheSame(final JButton[] btns) {
    if (btns == null) {
        throw new IllegalArgumentException();
    }//from  w ww  . ja  va  2 s.c  o 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:Main.java

private JButton createButton(String text, Dimension size) {
    JButton button = new JButton(text);
    button.setPreferredSize(size);
    button.setMinimumSize(size);//from  w w  w .  ja v a2  s. co m
    button.setMaximumSize(size);
    return button;
}

From source file:TabComponent.java

public TabComponent(String title, JTabbedPane pane) {
    this.pane = pane;
    setOpaque(false);//from  w  ww .  j ava2 s  . c o  m
    JLabel label = new JLabel(title);
    add(label);
    JButton button = new JButton("Close");
    button.setPreferredSize(new Dimension(50, 10));
    button.addActionListener(this);
    add(button);
}

From source file:Main.java

protected void createThumb() {
    JButton thumb = new JButton("Thumb");
    thumb.setPreferredSize(THUMB_SIZE);
    thumbPanel.add(thumb);/*from  w w w  . ja  v a2 s . c  o  m*/
    revalidate();
    repaint();
}

From source file:JDK6TabbedPaneExample.java

public void add() {
    final JPanel content = new JPanel();
    JPanel tab = new JPanel();
    tab.setOpaque(false);/*w ww  .ja va2  s .  c o  m*/

    JLabel tabLabel = new JLabel("Tab " + (++tabCounter));

    JButton tabCloseButton = new JButton(closeXIcon);
    tabCloseButton.setPreferredSize(closeButtonSize);
    tabCloseButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int closeTabNumber = tabbedPane.indexOfComponent(content);
            tabbedPane.removeTabAt(closeTabNumber);
        }
    });

    tab.add(tabLabel, BorderLayout.WEST);
    tab.add(tabCloseButton, BorderLayout.EAST);

    tabbedPane.addTab(null, content);
    tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tab);
}

From source file:net.sf.jabref.gui.help.HelpAction.java

public JButton getHelpButton() {
    JButton button = new JButton(this);
    button.setText(null);/*ww  w  .  j av  a  2s .  c  om*/
    button.setPreferredSize(new Dimension(24, 24));
    button.setToolTipText(getValue(Action.SHORT_DESCRIPTION).toString());
    return button;
}

From source file:com.game.ui.views.UserDialog.java

public UserDialog(String message, JFrame frame) {
    setLayout(new BorderLayout(5, 5));
    setModalityType(ModalityType.APPLICATION_MODAL);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setResizable(false);/*w  w w  .java 2s. com*/
    ImageIcon icon = null;
    try {
        icon = GameUtils.shrinkImage("warning.gif", 30, 30);
    } catch (IOException e) {
        System.out.println("Dialog : showDialogForMap(): Exception occured :" + e);
        e.printStackTrace();
    }
    JPanel panel = new JPanel();
    JLabel label = new JLabel(icon);
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    label.setText(message);
    label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    label.setHorizontalAlignment(0);
    panel.add(label);
    add(panel, BorderLayout.NORTH);
    JPanel contentPanel = new JPanel();
    contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
    txt = new JTextField();
    txt.setPreferredSize(new Dimension(150, 30));
    txt.setAlignmentX(.5f);
    txt.setMaximumSize(new Dimension(150, 30));
    contentPanel.add(txt);
    contentPanel.add(Box.createVerticalStrut(10));
    JButton btn = new JButton("Submit.");
    btn.setAlignmentX(.5f);
    btn.setPreferredSize(new Dimension(50, 25));
    btn.addActionListener(this);
    validationMess = new JLabel("All fields are mandatory");
    validationMess.setVisible(false);
    validationMess.setForeground(Color.red);
    validationMess.setAlignmentX(.5f);
    contentPanel.add(btn);
    contentPanel.add(Box.createVerticalStrut(10));
    contentPanel.add(validationMess);
    contentPanel.add(Box.createVerticalGlue());
    add(contentPanel, BorderLayout.CENTER);
    pack();
    setSize(new Dimension(300, 200));
    setLocationRelativeTo(frame);
    setVisible(true);
}

From source file:de.kletterfreak98.xmass.ui.BMIChart.java

public BMIChart(String title, TimeSeries values) {
    super(title);
    setIconImage(/*from   ww w .  j  ava 2s.c  o m*/
            new ImageIcon(getClass().getClassLoader().getResource("de/kletterfreak98/xmass/resources/fav.png"))
                    .getImage());

    // create a title...
    final String chartTitle = strings.getString("bmicourse");
    final XYDataset dataset = new TimeSeriesCollection(values);

    final JFreeChart chart = ChartFactory.createTimeSeriesChart(chartTitle, strings.getString("date"),
            strings.getString("bmi"), dataset, false, true, false);

    final XYPlot plot = chart.getXYPlot();
    plot.setDataset(1, new TimeSeriesCollection(values));
    plot.mapDatasetToRangeAxis(1, 1);
    final XYItemRenderer renderer = plot.getRenderer();
    renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
    if (renderer instanceof StandardXYItemRenderer) {
        final StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
        rr.setShapesFilled(true);
    }

    final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
    renderer2.setSeriesPaint(0, Color.black);
    renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
    plot.setRenderer(1, renderer2);

    final DateAxis axis = (DateAxis) plot.getDomainAxis();
    SimpleDateFormat sdf;
    if (Main.settings.getLang().equals(Locale.GERMANY)) {
        sdf = new SimpleDateFormat("dd.MM.yyyy");
    } else {
        sdf = new SimpleDateFormat("MM/dd/yyyy");
    }
    axis.setDateFormatOverride(sdf);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    JPanel panel = new JPanel();
    JButton close = new JButton(strings.getString("close"));
    close.setPreferredSize(new Dimension(close.getWidth(), 30));
    close.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    panel.setLayout(new BorderLayout());
    panel.add(chartPanel, BorderLayout.CENTER);
    panel.add(close, BorderLayout.SOUTH);
    panel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(panel);
    setUndecorated(true);
}