Java JTextField Select createSelected(final JTextField textField, final JButton... btns)

Here you can find the source of createSelected(final JTextField textField, final JButton... btns)

Description

create Selected

License

LGPL

Declaration

public static JPanel createSelected(final JTextField textField, final JButton... btns) 

Method Source Code


//package com.java2s;
import java.awt.Component;
import java.awt.Dimension;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.UIManager;

import javax.swing.plaf.metal.MetalLookAndFeel;

public class Main {
    static final String LAF_METAL = MetalLookAndFeel.class.getName();

    public static JPanel createSelected(final JTextField textField, final JButton... btns) {
        final Dimension btnDim = new Dimension(18, 18);
        initComponentHeight(textField);/* w ww  .j  av  a  2 s .  c  o  m*/
        final JPanel jp = new JPanel(new GridBagLayout());
        jp.setBorder(textField.getBorder());
        textField.setBorder(BorderFactory.createEmptyBorder());
        jp.setPreferredSize(textField.getPreferredSize());
        final GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        jp.add(textField, gbc);

        gbc.weightx = 0.0;
        if (btns != null) {
            for (final JButton btn : btns) {
                gbc.gridx++;
                btn.setMaximumSize(btnDim);
                btn.setMinimumSize(btnDim);
                btn.setPreferredSize(btnDim);
                btn.setText("..");
                jp.add(btn, gbc);
            }
        }
        return jp;
    }

    public static void initComponentHeight(final Component... components) {
        if (components == null) {
            return;
        }
        for (final Component component : components) {
            if ((component instanceof JComboBox) || (component instanceof JButton)) {
                component.setPreferredSize(new Dimension(component.getPreferredSize().width, 22));
            } else if (component instanceof JTextField) {
                final String lf = UIManager.getLookAndFeel().getClass().getName();
                int i = 22;
                if (lf.equals(LAF_METAL)) {
                    i = 23;
                }
                component.setPreferredSize(new Dimension(component.getPreferredSize().width, i));
            }
        }
    }
}

Related

  1. addSelectAllListener(final JTextField field)
  2. addSelectOnFocus(JTextField one)
  3. buttonFolderSelect(final JTextField text)
  4. focusAndSelectTextInTextField(JTextField textField)
  5. newFileChooser(String dialogTitle, int fileSelectionMode, JTextField textField, String globalLocation)
  6. recipientHintSelected(String hintString, JTextField toList, boolean shiftKeyPressed)
  7. selectAllOnFocus(final JTextField jtf, final String recoverText, final Color forTip, final Color forContent)