Example usage for javax.swing JButton setMaximumSize

List of usage examples for javax.swing JButton setMaximumSize

Introduction

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

Prototype

@BeanProperty(description = "The maximum size of the component.")
public void setMaximumSize(Dimension maximumSize) 

Source Link

Document

Sets the maximum size of this component to a constant value.

Usage

From source file:OverlaySample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Overlay Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel() {
        public boolean isOptimizedDrawingEnabled() {
            return false;
        }//  w w w  .  ja v a 2 s  .c  o  m
    };
    LayoutManager overlay = new OverlayLayout(panel);
    panel.setLayout(overlay);

    JButton button = new JButton("Small");
    button.setMaximumSize(new Dimension(25, 25));
    button.setBackground(Color.white);
    panel.add(button);

    button = new JButton("Medium");
    button.setMaximumSize(new Dimension(50, 50));
    button.setBackground(Color.gray);
    panel.add(button);

    button = new JButton("Large");
    button.setMaximumSize(new Dimension(100, 100));
    button.setBackground(Color.black);
    panel.add(button);

    frame.add(panel, BorderLayout.CENTER);

    frame.setSize(400, 300);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Overlay Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel() {
        public boolean isOptimizedDrawingEnabled() {
            return false;
        }/* w  ww .j  a  v a2  s.  c o  m*/
    };
    LayoutManager overlay = new OverlayLayout(panel);
    panel.setLayout(overlay);

    JButton button = new JButton("Small");
    button.setMaximumSize(new Dimension(25, 25));
    button.setBackground(Color.white);
    button.setAlignmentX(0.0f);
    button.setAlignmentY(0.0f);
    panel.add(button);

    button = new JButton("Medium");
    button.setMaximumSize(new Dimension(50, 50));
    button.setBackground(Color.gray);
    button.setAlignmentX(0.0f);
    button.setAlignmentY(0.0f);
    panel.add(button);

    button = new JButton("Large");
    button.setMaximumSize(new Dimension(100, 100));
    button.setBackground(Color.black);
    button.setAlignmentX(0.0f);
    button.setAlignmentY(0.0f);
    panel.add(button);

    frame.add(panel, BorderLayout.CENTER);

    frame.setSize(400, 300);
    frame.setVisible(true);
}

From source file:OverlaySample.java

public static void main(String args[]) {

    ActionListener generalActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JComponent comp = (JComponent) actionEvent.getSource();
            System.out.println(actionEvent.getActionCommand() + ": " + comp.getBounds());
        }// w ww .j  a  v  a2 s .  c  o m
    };

    ActionListener sizingActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            setupButtons(actionEvent.getActionCommand());
        }
    };

    JFrame frame = new JFrame("Overlay Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    LayoutManager overlay = new OverlayLayout(panel);
    panel.setLayout(overlay);

    Object settings[][] = { { "Small", new Dimension(25, 25), Color.white },
            { "Medium", new Dimension(50, 50), Color.gray },
            { "Large", new Dimension(100, 100), Color.black } };
    JButton buttons[] = { smallButton, mediumButton, largeButton };

    for (int i = 0, n = settings.length; i < n; i++) {
        JButton button = buttons[i];
        button.addActionListener(generalActionListener);
        button.setActionCommand((String) settings[i][0]);
        button.setMaximumSize((Dimension) settings[i][1]);
        button.setBackground((Color) settings[i][2]);
        panel.add(button);
    }

    setupButtons(SET_CENTRAL);

    JPanel actionPanel = new JPanel();
    actionPanel.setBorder(BorderFactory.createTitledBorder("Change Alignment"));
    String actionSettings[] = { SET_MINIMUM, SET_MAXIMUM, SET_CENTRAL, SET_MIXED };
    for (int i = 0, n = actionSettings.length; i < n; i++) {
        JButton button = new JButton(actionSettings[i]);
        button.addActionListener(sizingActionListener);
        actionPanel.add(button);
    }

    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(actionPanel, BorderLayout.SOUTH);

    frame.setSize(400, 300);
    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  ww w . j a  va2s . c  om
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: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.
 *///ww  w.j  av a 2  s . 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);
    }
}

From source file:Main.java

private JButton createButton(String text, Dimension size) {
    JButton button = new JButton(text);
    button.setPreferredSize(size);/*from w  w  w.  j ava 2s  . c om*/
    button.setMinimumSize(size);
    button.setMaximumSize(size);
    return button;
}

From source file:cool.pandora.modeller.ui.jpanel.base.BagInfoForm.java

private void createFormFieldsFromMap(final BagTableFormBuilder formBuilder) {
    int rowCount = 0;
    final int index = 2;

    final Set<String> keys = fieldMap.keySet();
    for (final BagInfoField field : fieldMap.values()) {
        formBuilder.row();// w  w w  .  j ava 2 s . c o  m
        rowCount++;
        final ImageIcon imageIcon = bagView.getPropertyImage("bag.delete.image");
        JButton removeButton = new JButton(imageIcon);
        final Dimension dimension = removeButton.getPreferredSize();
        dimension.width = imageIcon.getIconWidth();
        removeButton.setMaximumSize(dimension);
        removeButton.setOpaque(false);
        removeButton.setBorderPainted(false);
        removeButton.setContentAreaFilled(false);
        removeButton.addActionListener(new RemoveFieldHandler());
        logger.debug("OrganizationInfoForm add: " + field);
        if (field.getValue() != null && field.getValue().length() > 60) {
            field.setComponentType(BagInfoField.TEXTAREA_COMPONENT);
        }
        if (field.isRequired()) {
            removeButton = new JButton();
            removeButton.setOpaque(false);
            removeButton.setBorderPainted(false);
            removeButton.setContentAreaFilled(false);
        }
        switch (field.getComponentType()) {
        case BagInfoField.TEXTAREA_COMPONENT:
            final JComponent[] tlist = formBuilder.addTextArea(field.isRequired(), field.getLabel(),
                    removeButton);
            final JComponent textarea = tlist[index];
            textarea.setEnabled(field.isEnabled());
            textarea.addFocusListener(this);
            ((NoTabTextArea) textarea).setText(field.getValue());
            textarea.setBorder(new EmptyBorder(1, 1, 1, 1));
            ((NoTabTextArea) textarea).setLineWrap(true);
            if (rowCount == 1) {
                focusField = textarea;
            }
            break;
        case BagInfoField.TEXTFIELD_COMPONENT:
            final JComponent[] flist = formBuilder.add(field.isRequired(), field.getLabel(), removeButton);
            final JComponent comp = flist[index];
            comp.setEnabled(field.isEnabled());
            comp.addFocusListener(this);
            ((JTextField) comp).setText(field.getValue());
            if (rowCount == 1) {
                focusField = comp;
            }
            break;
        case BagInfoField.LIST_COMPONENT:
            final List<String> elements = field.getElements();
            final JComponent[] llist = formBuilder.addList(field.isRequired(), field.getLabel(), elements,
                    field.getValue(), removeButton);
            final JComponent lcomp = llist[index];
            lcomp.setEnabled(field.isEnabled());
            lcomp.addFocusListener(this);
            if (field.getValue() != null) {
                ((JComboBox<?>) lcomp).setSelectedItem(field.getValue().trim());
            }
            if (rowCount == 1) {
                focusField = lcomp;
            }
            break;
        default:
        }
    }
    if (focusField != null) {
        focusField.requestFocus();
    }

}

From source file:ConfigFiles.java

public JPanel addPanel(String title, String description, final JTextField textfield, String fieldtext, int Y,
        boolean withbutton, ActionListener actionlistener) {
    JPanel p1 = new JPanel();
    p1.setBackground(Color.WHITE);
    TitledBorder border = BorderFactory.createTitledBorder(title);
    border.setTitleFont(new Font("Arial", Font.PLAIN, 14));
    border.setBorder(BorderFactory.createLineBorder(new Color(150, 150, 150), 1));
    p1.setBorder(border);/*from w  ww.  java  2  s.c  o  m*/
    p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
    p1.setBounds(80, Y, 800, 75);
    paths.add(p1);
    JTextArea tcpath = new JTextArea(description);
    tcpath.setWrapStyleWord(true);
    tcpath.setLineWrap(true);
    tcpath.setEditable(false);
    tcpath.setCursor(null);
    tcpath.setOpaque(false);
    tcpath.setFocusable(false);
    tcpath.setFont(new Font("Arial", Font.PLAIN, 12));
    tcpath.setBackground(getBackground());
    tcpath.setMaximumSize(new Dimension(170, 22));
    tcpath.setPreferredSize(new Dimension(170, 22));
    tcpath.setBorder(null);
    JPanel p11 = new JPanel();
    p11.setBackground(Color.WHITE);
    p11.setLayout(new GridLayout());
    p11.add(tcpath);
    p11.setMaximumSize(new Dimension(700, 18));
    p11.setPreferredSize(new Dimension(700, 18));
    textfield.setMaximumSize(new Dimension(340, 27));
    textfield.setPreferredSize(new Dimension(340, 27));
    textfield.setText(fieldtext);
    JButton b = null;
    if (withbutton) {
        b = new JButton("...");
        if (!PermissionValidator.canChangeFWM()) {
            b.setEnabled(false);
        }
        b.setMaximumSize(new Dimension(50, 20));
        b.setPreferredSize(new Dimension(50, 20));
        if (actionlistener == null) {
            b.addActionListener(new AbstractAction() {
                public void actionPerformed(ActionEvent ev) {
                    Container c;

                    if (RunnerRepository.container != null)
                        c = RunnerRepository.container.getParent();
                    else
                        c = RunnerRepository.window;
                    try {
                        new MySftpBrowser(RunnerRepository.host, RunnerRepository.user,
                                RunnerRepository.password, textfield, c, false);
                    } catch (Exception e) {
                        System.out.println("There was a problem in opening sftp browser!");
                        e.printStackTrace();
                    }
                }
            });
        } else {
            b.addActionListener(actionlistener);
            b.setText("Save");
            b.setMaximumSize(new Dimension(70, 20));
            b.setPreferredSize(new Dimension(70, 20));
        }
    }
    JPanel p12 = new JPanel();
    p12.setBackground(Color.WHITE);
    p12.add(textfield);
    if (withbutton)
        p12.add(b);
    p12.setMaximumSize(new Dimension(700, 32));
    p12.setPreferredSize(new Dimension(700, 32));
    p1.add(p11);
    p1.add(p12);
    return p12;
}

From source file:SuitaDetails.java

public DefPanel(String descriptions, String button, String id, int width, final int index,
        SuitaDetails container) {//w  w w  .j  a  v  a2s  . co  m
    this.descriptions = descriptions;
    this.id = id;
    reference = this;
    this.container = container;
    this.index = index;
    setBackground(new Color(255, 255, 255));
    setBorder(BorderFactory.createEmptyBorder(2, 20, 2, 20));
    setMaximumSize(new Dimension(32767, 30));
    setMinimumSize(new Dimension(100, 30));
    setPreferredSize(new Dimension(300, 30));
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    description = new JLabel(descriptions);
    description.setPreferredSize(new Dimension(width, 20));
    description.setMinimumSize(new Dimension(width, 20));
    description.setMaximumSize(new Dimension(width, 20));
    add(description);
    filedsGap = new JPanel();
    filedsGap.setBackground(new Color(255, 255, 255));
    filedsGap.setMaximumSize(new Dimension(20, 20));
    filedsGap.setMinimumSize(new Dimension(20, 20));
    filedsGap.setPreferredSize(new Dimension(20, 20));
    GroupLayout filedsGapLayout = new GroupLayout(filedsGap);
    filedsGap.setLayout(filedsGapLayout);
    filedsGapLayout.setHorizontalGroup(
            filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 20, Short.MAX_VALUE));
    filedsGapLayout.setVerticalGroup(
            filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 20, Short.MAX_VALUE));
    add(filedsGap);
    userDefinition = new JTextField();
    doclistener = new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            setParentField(userDefinition.getText(), false);
        }

        public void removeUpdate(DocumentEvent e) {
            setParentField(userDefinition.getText(), false);
        }

        public void insertUpdate(DocumentEvent e) {
            setParentField(userDefinition.getText(), false);
        }
    };
    userDefinition.getDocument().addDocumentListener(doclistener);
    userDefinition.setText("");
    userDefinition.setMaximumSize(new Dimension(300, 100));
    userDefinition.setMinimumSize(new Dimension(50, 20));
    userDefinition.setPreferredSize(new Dimension(100, 20));
    add(userDefinition);
    filedsGap = new JPanel();
    filedsGap.setBackground(new Color(255, 255, 255));
    filedsGap.setMaximumSize(new Dimension(20, 20));
    filedsGap.setMinimumSize(new Dimension(20, 20));
    filedsGap.setPreferredSize(new Dimension(20, 20));
    filedsGapLayout = new GroupLayout(filedsGap);
    filedsGap.setLayout(filedsGapLayout);
    filedsGapLayout.setHorizontalGroup(
            filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 20, Short.MAX_VALUE));
    filedsGapLayout.setVerticalGroup(
            filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 20, Short.MAX_VALUE));
    add(filedsGap);
    if (button.equals("UserSelect")) {
        final JButton database = new JButton("Database");
        database.setMaximumSize(new Dimension(100, 20));
        database.setMinimumSize(new Dimension(50, 20));
        database.setPreferredSize(new Dimension(80, 20));
        add(database);
        database.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                DatabaseFrame frame = new DatabaseFrame(reference);
                frame.executeQuery();
                frame.setLocation((int) database.getLocationOnScreen().getX() - 100,
                        (int) database.getLocationOnScreen().getY());
                frame.setVisible(true);
            }
        });
    } else if (button.equals("UserScript")) {
        JButton script = new JButton("Script");
        script.setMaximumSize(new Dimension(100, 20));
        script.setMinimumSize(new Dimension(50, 20));
        script.setPreferredSize(new Dimension(80, 20));
        add(script);
        script.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                Container c;
                if (RunnerRepository.container != null)
                    c = RunnerRepository.container.getParent();
                else
                    c = RunnerRepository.window;
                try {
                    //                         String passwd = RunnerRepository.getRPCClient().execute("sendFile", new Object[]{"/etc/passwd"}).toString();
                    //                         new MySftpBrowser(RunnerRepository.host,RunnerRepository.user,RunnerRepository.password,userDefinition,c,passwd);
                    new MySftpBrowser(RunnerRepository.host, RunnerRepository.user, RunnerRepository.password,
                            userDefinition, c, false);
                } catch (Exception e) {
                    System.out.println("There was a problem in opening sftp browser!");
                    e.printStackTrace();
                }
            }
        });
        filedsGap = new JPanel();
        filedsGap.setBackground(new Color(255, 255, 255));
        filedsGap.setMaximumSize(new Dimension(10, 10));
        filedsGap.setMinimumSize(new Dimension(10, 10));
        filedsGap.setPreferredSize(new Dimension(10, 10));
        filedsGapLayout = new GroupLayout(filedsGap);
        filedsGap.setLayout(filedsGapLayout);
        filedsGapLayout.setHorizontalGroup(filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGap(0, 20, Short.MAX_VALUE));
        filedsGapLayout.setVerticalGroup(filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGap(0, 20, Short.MAX_VALUE));
        filedsGap.setLayout(filedsGapLayout);
        add(filedsGap);
        final JButton value = new JButton("Value");
        value.setMaximumSize(new Dimension(100, 20));
        value.setMinimumSize(new Dimension(50, 20));
        value.setPreferredSize(new Dimension(80, 20));
        add(value);
        value.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                String script = userDefinition.getText();
                if (script != null && !script.equals("")) {
                    try {
                        String result = RunnerRepository.getRPCClient().execute("runUserScript",
                                new Object[] { script }) + "";
                        JFrame f = new JFrame();
                        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                        f.setLocation(value.getLocationOnScreen());
                        JLabel l = new JLabel("Script result: " + result);
                        f.getContentPane().add(l, BorderLayout.CENTER);
                        f.pack();
                        f.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    } else if (button.equals("UserText")) {
        JPanel database = new JPanel();
        database.setBackground(Color.WHITE);
        database.setMaximumSize(new Dimension(100, 20));
        database.setMinimumSize(new Dimension(50, 20));
        database.setPreferredSize(new Dimension(80, 20));
        add(database);
    }
}