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:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel btmPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weighty = 1.0;/*from  w w  w.j  a v  a  2 s  .c  o m*/
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.NORTH;
    JButton comp = new JButton("Panel-1");
    btmPanel.add(comp, gbc);
    JButton comp2 = new JButton("Panel-2");
    btmPanel.add(comp2, gbc);
    JButton comp3 = new JButton("Panel-3");
    comp3.setPreferredSize(new Dimension(comp.getPreferredSize().width, comp.getPreferredSize().height + 10));
    btmPanel.add(comp3, gbc);
    frame.add(btmPanel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("CardLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JPanel buttonPanel = new JPanel();
    JButton nextButton = new JButton("Next");
    buttonPanel.add(nextButton);/*from www .  j  a v a2 s  .  c o m*/
    contentPane.add(buttonPanel, BorderLayout.SOUTH);

    final JPanel cardPanel = new JPanel();
    final CardLayout cardLayout = new CardLayout();
    cardPanel.setLayout(cardLayout);

    for (int i = 1; i <= 5; i++) {
        JButton card = new JButton("Card " + i);
        card.setPreferredSize(new Dimension(200, 200));
        String cardName = "card" + 1;
        cardPanel.add(card, cardName);
    }
    contentPane.add(cardPanel, BorderLayout.CENTER);
    nextButton.addActionListener(e -> cardLayout.next(cardPanel));

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    int x = 5;/*from   w  ww.  j  a v a 2 s  .c om*/
    int y = 5;
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(x, y));
    for (int i = 0; i < x * y; i++) {
        JButton button = new JButton(String.valueOf(i));
        button.setPreferredSize(new Dimension(100, 100));
        panel.add(button);
    }
    JPanel container = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
    container.add(panel);
    JScrollPane scrollPane = new JScrollPane(container);
    f.getContentPane().add(scrollPane);

    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JButton btnA = new JButton("A");
    JButton btnB = new JButton("B");

    btnA.setPreferredSize(new Dimension(50, 25));
    btnB.setPreferredSize(new Dimension(100, 25));

    JPanel btnAPanel = new JPanel();
    JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    btnAPanel.add(btnA);// www.  j  a v a2  s.c o m
    btnBPanel.add(btnB);

    JPanel topPanel = new JPanel(new GridLayout(1, 0));
    topPanel.add(new JLabel("hi"));
    topPanel.add(btnAPanel);
    topPanel.add(btnBPanel);

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.NORTH);

    mainPanel.setPreferredSize(new Dimension(400, 300));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(mainPanel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Sets the JButtons inside a JPanelto be the same size.
 * This is done dynamically by setting each button's preferred and maximum
 * sizes after the buttons are created. This way, the layout automatically
 * adjusts to the locale-specific strings.
 *
 * @param jPanelButtons JPanel containing buttons
 *//*from  w w w . j av  a2s.co m*/
public static void equalizeButtonSizes(JPanel jPanelButtons) {
    ArrayList<JButton> lbuttons = new ArrayList<JButton>();
    for (int i = 0; i < jPanelButtons.getComponentCount(); i++) {
        Component c = jPanelButtons.getComponent(i);
        if (c instanceof JButton) {
            lbuttons.add((JButton) c);
        }
    }

    // Get the largest width and height
    Dimension maxSize = new Dimension(0, 0);
    for (JButton lbutton : lbuttons) {
        Dimension d = lbutton.getPreferredSize();
        maxSize.width = Math.max(maxSize.width, d.width);
        maxSize.height = Math.max(maxSize.height, d.height);
    }

    for (JButton btn : lbuttons) {
        btn.setPreferredSize(maxSize);
        btn.setMinimumSize(maxSize);
        btn.setMaximumSize(maxSize);
    }
}

From source file:Main.java

/** Constructs a JButton with an icon from the given file id. */
public static JButton makeButton(final Object owner, final String id, final String altText, final int wpad,
        final int hpad) {
    final URL url = owner.getClass().getResource(id);
    ImageIcon icon = null;/*from   ww  w .  j  a  va 2s .c om*/
    if (url != null)
        icon = new ImageIcon(url);
    JButton button;
    if (icon == null)
        button = new JButton(altText);
    else {
        button = new JButton(icon);
        button.setPreferredSize(new Dimension(icon.getIconWidth() + wpad, icon.getIconHeight() + hpad));
    }
    return button;
}

From source file:layout.BorderLayoutDemo.java

public static void addComponentsToPane(Container pane) {

    if (!(pane.getLayout() instanceof BorderLayout)) {
        pane.add(new JLabel("Container doesn't use BorderLayout!"));
        return;// w  w w.j  a  v a 2  s.  co m
    }

    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
    }

    JButton button = new JButton("Button 1 (PAGE_START)");
    pane.add(button, BorderLayout.PAGE_START);

    //Make the center component big, since that's the
    //typical usage of BorderLayout.
    button = new JButton("Button 2 (CENTER)");
    button.setPreferredSize(new Dimension(200, 100));
    pane.add(button, BorderLayout.CENTER);

    button = new JButton("Button 3 (LINE_START)");
    pane.add(button, BorderLayout.LINE_START);

    button = new JButton("Long-Named Button 4 (PAGE_END)");
    pane.add(button, BorderLayout.PAGE_END);

    button = new JButton("5 (LINE_END)");
    pane.add(button, BorderLayout.LINE_END);
}

From source file:Main.java

/**
 * Returns a styled JButton.//from w  ww  .j  a  v  a  2 s .c  o m
 *
 * @param style button type
 * @return a styled button
 */
public static JButton creaStyledButton(int style) {
    JButton jb = new JButton();
    jb.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    jb.setMargin(new Insets(0, 5, 1, 5));
    switch (style) {
    case STYLE_EDIT: {
        jb.setText("edit");
        jb.setToolTipText("edit");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_OK: {
        jb.setText("ok");
        jb.setToolTipText("confirm changes");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_CANCEL: {
        jb.setText("cancel");
        jb.setToolTipText("discard changes");
        jb.setPreferredSize(new Dimension(60, 30));
        return jb;
    }
    case STYLE_INSBEFOREROW: {
        jb.setIcon(ICON_INSBEFOREROW);
        jb.setToolTipText("insert row before");
        jb.setName("INSBEFOREROW");
        return jb;
    }
    case STYLE_INSAFTERROW: {
        jb.setIcon(ICON_INSAFTERROW);
        jb.setToolTipText("insert row after");
        jb.setName("INSAFTERROW");
        return jb;
    }
    case STYLE_DELETEROW: {
        jb.setIcon(ICON_DELETEROW);
        jb.setToolTipText("delete row");
        jb.setName("DELETEROW");
        return jb;
    }
    case STYLE_CLONEBEFOREROW: {
        jb.setIcon(ICON_CLONEBEFOREROW);
        jb.setToolTipText("clone row before");
        jb.setName("CLONEBEFOREROW");
        return jb;
    }
    case STYLE_CLONEAFTERROW: {
        jb.setIcon(ICON_CLONEAFTERROW);
        jb.setToolTipText("clone row after");
        jb.setName("CLONEAFTERROW");
        return jb;
    }
    case STYLE_DEFAULTROWS: {
        jb.setIcon(ICON_DEFAULTROWS);
        jb.setToolTipText("set default values");
        jb.setName("DEFAULTROWS");
        return jb;
    }
    default: {
        return null;
    }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopComponentsHelper.java

public static void adjustSize(JButton button) {
    button.setPreferredSize(new Dimension(0, BUTTON_HEIGHT));
    button.setMaximumSize(new Dimension(Integer.MAX_VALUE, BUTTON_HEIGHT));
}

From source file:Main.java

/**
 * Ensures that all buttons are the same size, and that the chosen size is sufficient to contain the content of any.
 *//*from   w  w w . j  av  a 2s .c o m*/
public static void tieButtonSizes(List<JButton> buttons) {
    int maxWidth = 0;
    int maxHeight = 0;
    for (JButton button : buttons) {
        Dimension buttonSize = button.getPreferredSize();
        maxWidth = (int) Math.max(buttonSize.getWidth(), maxWidth);
        maxHeight = (int) Math.max(buttonSize.getHeight(), maxHeight);
    }
    Dimension maxButtonSize = new Dimension(maxWidth, maxHeight);
    for (JButton button : buttons) {
        // Seemingly, to get the GTK+ LAF to behave when there are buttons with and without icons, we need to set every size.
        button.setPreferredSize(maxButtonSize);
        button.setMinimumSize(maxButtonSize);
        button.setMaximumSize(maxButtonSize);
        button.setSize(maxButtonSize);
    }
}