Example usage for javax.swing SwingConstants TOP

List of usage examples for javax.swing SwingConstants TOP

Introduction

In this page you can find the example usage for javax.swing SwingConstants TOP.

Prototype

int TOP

To view the source code for javax.swing SwingConstants TOP.

Click Source Link

Document

Box-orientation constant used to specify the top of a box.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    JButton button = new JButton();
    // Place text over center of icon; they both occupy the same space
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.CENTER);

    // Place text above icon
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.CENTER);

    // Place text below icon
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.CENTER);

    // Place text to the left of icon, vertically centered
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.LEFT);

    // Place text to the left of icon and align their tops
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.LEFT);

    // Place text to the left of icon and align their bottoms
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.LEFT);

    // Place text to the right of icon, vertically centered
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);

    // Place text to the right of icon and align their tops
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);

    // Place text to the right of icon and align their bottoms
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);

}

From source file:Main.java

public static void main(String[] args) {
    JLabel label1 = new JLabel("BottomRight", SwingConstants.RIGHT);
    JLabel label2 = new JLabel("CenterLeft", SwingConstants.LEFT);
    JLabel label3 = new JLabel("TopCenter", SwingConstants.CENTER);
    label1.setVerticalAlignment(SwingConstants.BOTTOM);
    label2.setVerticalAlignment(SwingConstants.CENTER);
    label3.setVerticalAlignment(SwingConstants.TOP);

    label1.setBorder(BorderFactory.createLineBorder(Color.black));
    label2.setBorder(BorderFactory.createLineBorder(Color.black));
    label3.setBorder(BorderFactory.createLineBorder(Color.black));

    JFrame frame = new JFrame("AlignmentExample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel(new GridLayout(3, 1, 8, 8));
    p.add(label1);//from  w  w w .  j  a  v a  2s.  com
    p.add(label2);
    p.add(label3);
    p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    frame.setContentPane(p);
    frame.setSize(200, 200);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Initializes a default margin in provided directions.
 * <p>//from   ww  w.  ja v a  2 s.c om
 * Directions provided can be one of the TOP, LEFT, DOWN and BOTTOM
 * constants from the {@link SwingConstants} interface.
 * 
 * @param directions Array of directions where the border is to be created.
 * @return Empty border to be used as margin; border width defined as
 * {@link #DEFAULT_MARGIN}.
 */
public static Border defaultMargin(int... directions) {
    int[] borders = new int[4];
    if (directions == null)
        return BorderFactory.createEmptyBorder();

    for (int i = 0; i < directions.length; i++) {
        if (directions[i] == SwingConstants.TOP)
            borders[0] = DEFAULT_MARGIN;
        if (directions[i] == SwingConstants.LEFT)
            borders[1] = DEFAULT_MARGIN;
        if (directions[i] == SwingConstants.BOTTOM)
            borders[2] = DEFAULT_MARGIN;
        if (directions[i] == SwingConstants.RIGHT)
            borders[3] = DEFAULT_MARGIN;
    }

    return BorderFactory.createEmptyBorder(borders[0], borders[1], borders[2], borders[3]);
}

From source file:Main.java

public Main() {
    super("JLabel Demo");
    setSize(600, 100);/* w ww. java 2  s .  c  o  m*/

    JPanel content = new JPanel(new GridLayout(1, 4, 4, 4));

    JLabel label = new JLabel("Java2s");
    label.setBackground(Color.white);
    content.add(label);

    label = new JLabel("Java2s", SwingConstants.CENTER);
    label.setOpaque(true);
    label.setBackground(Color.white);
    content.add(label);

    label = new JLabel("Java2s");
    label.setFont(new Font("Helvetica", Font.BOLD, 18));
    label.setOpaque(true);
    label.setBackground(Color.white);
    content.add(label);

    ImageIcon image = new ImageIcon("java2sLogo.gif");
    label = new JLabel("Java2s", image, SwingConstants.RIGHT);
    label.setVerticalTextPosition(SwingConstants.TOP);
    label.setOpaque(true);
    label.setBackground(Color.white);
    content.add(label);

    getContentPane().add(content);
    setVisible(true);
}

From source file:LabelDemo.java

public LabelDemo() {
    super("JLabel Demo");
    setSize(600, 100);//from   w w  w. j  a  va  2s . co m

    JPanel content = new JPanel(new GridLayout(1, 4, 4, 4));

    JLabel label = new JLabel("Java2s");
    label.setBackground(Color.white);
    content.add(label);

    label = new JLabel("Java2s", SwingConstants.CENTER);
    label.setOpaque(true);
    label.setBackground(Color.white);
    content.add(label);

    label = new JLabel("Java2s");
    label.setFont(new Font("Helvetica", Font.BOLD, 18));
    label.setOpaque(true);
    label.setBackground(Color.white);
    content.add(label);

    ImageIcon image = new ImageIcon("java2sLogo.gif");
    label = new JLabel("Java2s", image, SwingConstants.RIGHT);
    label.setVerticalTextPosition(SwingConstants.TOP);
    label.setOpaque(true);
    label.setBackground(Color.white);
    content.add(label);

    getContentPane().add(content);
    setVisible(true);
}

From source file:com.samebug.clients.idea.ui.component.ExceptionMessageLabel.java

public ExceptionMessageLabel(@Nullable String message) {
    {//  www.  j  ava2s  . c o m
        final String escapedText;
        if (message == null) {
            escapedText = String.format("<html><i>%s</i></html>",
                    SamebugBundle.message("samebug.exception.noMessage"));
        } else {
            // Escape html, but keep line breaks
            String broken = StringEscapeUtils.escapeHtml(message).replaceAll("\\n", "<br>");
            escapedText = String.format("<html>%s</html>", broken);
        }

        setFont(UIManager.getFont("TextArea.font"));
        setText(escapedText);
        setVerticalAlignment(SwingConstants.TOP);
    }

}

From source file:PaintUtils.java

/**
 * Returns the bounds that the text of a label will be drawn into.
 * Takes into account the current font metrics.
 *///from   ww  w . j  a v a 2 s .  c  o  m
public static Rectangle getTextBounds(Graphics g, JLabel label) {
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D r2d = fm.getStringBounds(label.getText(), g);
    Rectangle rect = r2d.getBounds();
    int xOffset = 0;
    switch (label.getHorizontalAlignment()) {
    case SwingConstants.RIGHT:
    case SwingConstants.TRAILING:
        xOffset = label.getBounds().width - rect.width;
        break;
    case SwingConstants.CENTER:
        xOffset = (label.getBounds().width - rect.width) / 2;
        break;
    default:
    case SwingConstants.LEFT:
    case SwingConstants.LEADING:
        xOffset = 0;
        break;
    }
    int yOffset = 0;
    switch (label.getVerticalAlignment()) {
    case SwingConstants.TOP:
        yOffset = 0;
        break;
    case SwingConstants.CENTER:
        yOffset = (label.getBounds().height - rect.height) / 2;
        break;
    case SwingConstants.BOTTOM:
        yOffset = label.getBounds().height - rect.height;
        break;
    }
    return new Rectangle(xOffset, yOffset, rect.width, rect.height);
}

From source file:com.kbotpro.ui.FieldWatcher.java

private void initComponents() {
    // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    label2 = new JLabel();
    updateRateSpinner = new JSpinner();
    label3 = new JLabel();
    scrollPane1 = new JScrollPane();
    infoLabel = new JLabel();

    //======== this ========
    setTitle("Field Watch");
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    Container contentPane = getContentPane();

    //---- label2 ----
    label2.setText("Update rate:");
    label2.setLabelFor(updateRateSpinner);

    //---- updateRateSpinner ----
    updateRateSpinner.setModel(new SpinnerNumberModel(1.0, 0.1, null, 0.5));

    //---- label3 ----
    label3.setText("Times per second");

    //======== scrollPane1 ========
    {/* ww w. j a v  a 2s.  co m*/

        //---- infoLabel ----
        infoLabel.setText("Field info");
        infoLabel.setVerticalAlignment(SwingConstants.TOP);
        scrollPane1.setViewportView(infoLabel);
    }

    GroupLayout contentPaneLayout = new GroupLayout(contentPane);
    contentPane.setLayout(contentPaneLayout);
    contentPaneLayout.setHorizontalGroup(contentPaneLayout.createParallelGroup()
            .add(contentPaneLayout.createSequentialGroup().addContainerGap().add(contentPaneLayout
                    .createParallelGroup().add(scrollPane1, GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
                    .add(contentPaneLayout.createSequentialGroup().add(label2)
                            .addPreferredGap(LayoutStyle.RELATED)
                            .add(updateRateSpinner, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.RELATED).add(label3)))
                    .addContainerGap()));
    contentPaneLayout
            .setVerticalGroup(contentPaneLayout.createParallelGroup().add(GroupLayout.TRAILING,
                    contentPaneLayout.createSequentialGroup().addContainerGap()
                            .add(scrollPane1, GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                            .addPreferredGap(LayoutStyle.RELATED)
                            .add(contentPaneLayout.createParallelGroup(GroupLayout.BASELINE).add(label2)
                                    .add(updateRateSpinner, GroupLayout.PREFERRED_SIZE,
                                            GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .add(label3))
                            .addContainerGap()));
    pack();
    setLocationRelativeTo(getOwner());
    // JFormDesigner - End of component initialization  //GEN-END:initComponents
}

From source file:au.org.ala.delta.intkey.ui.FindInTaxaDialog.java

public FindInTaxaDialog(Intkey intkeyApp) {
    super(intkeyApp.getMainFrame(), false);
    setResizable(false);//from   w w w  .  j  ava2 s  .com

    ResourceMap resourceMap = Application.getInstance().getContext().getResourceMap(FindInTaxaDialog.class);
    resourceMap.injectFields(this);
    ActionMap actionMap = Application.getInstance().getContext().getActionMap(this);

    _intkeyApp = intkeyApp;

    _numMatchedTaxa = 0;
    _currentMatchedTaxon = -1;

    _findAction = actionMap.get("findTaxa");
    _nextAction = actionMap.get("nextFoundTaxon");

    this.setTitle(windowTitle);

    getContentPane().setLayout(new BorderLayout(0, 0));

    _pnlMain = new JPanel();
    _pnlMain.setBorder(new EmptyBorder(20, 20, 20, 20));
    getContentPane().add(_pnlMain, BorderLayout.CENTER);
    _pnlMain.setLayout(new BorderLayout(0, 0));

    _pnlMainTop = new JPanel();
    _pnlMain.add(_pnlMainTop, BorderLayout.NORTH);
    _pnlMainTop.setLayout(new BoxLayout(_pnlMainTop, BoxLayout.Y_AXIS));

    _lblEnterSearchString = new JLabel(enterSearchStringCaption);
    _lblEnterSearchString.setBorder(new EmptyBorder(0, 0, 5, 0));
    _lblEnterSearchString.setHorizontalAlignment(SwingConstants.LEFT);
    _lblEnterSearchString.setVerticalAlignment(SwingConstants.TOP);
    _lblEnterSearchString.setAlignmentY(Component.TOP_ALIGNMENT);
    _pnlMainTop.add(_lblEnterSearchString);

    _textField = new JTextField();
    _textField.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) {
            reset();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            reset();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            reset();
        }
    });

    _pnlMainTop.add(_textField);
    _textField.setColumns(10);

    _pnlMainMiddle = new JPanel();
    _pnlMainMiddle.setBorder(new EmptyBorder(10, 0, 0, 0));
    _pnlMain.add(_pnlMainMiddle, BorderLayout.CENTER);
    _pnlMainMiddle.setLayout(new BoxLayout(_pnlMainMiddle, BoxLayout.Y_AXIS));

    _rdbtnSelectOne = new JRadioButton(selectOneCaption);
    _rdbtnSelectOne.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            reset();
        }
    });
    _pnlMainMiddle.add(_rdbtnSelectOne);

    _rdbtnSelectAll = new JRadioButton(selectAllCaption);
    _rdbtnSelectAll.setSelected(true);
    _rdbtnSelectAll.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            reset();
        }
    });
    _pnlMainMiddle.add(_rdbtnSelectAll);

    ButtonGroup radioButtonGroup = new ButtonGroup();
    radioButtonGroup.add(_rdbtnSelectOne);
    radioButtonGroup.add(_rdbtnSelectAll);

    _pnlMainBottom = new JPanel();
    _pnlMain.add(_pnlMainBottom, BorderLayout.SOUTH);
    _pnlMainBottom.setLayout(new BoxLayout(_pnlMainBottom, BoxLayout.Y_AXIS));

    _chckbxSearchSynonyms = new JCheckBox(searchSynonymsCaption);
    _chckbxSearchSynonyms.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            reset();
        }
    });

    _pnlMainBottom.add(_chckbxSearchSynonyms);

    _chckbxSearchEliminatedTaxa = new JCheckBox(searchEliminatedTaxaCaption);
    _chckbxSearchEliminatedTaxa.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            reset();
        }
    });

    _pnlMainBottom.add(_chckbxSearchEliminatedTaxa);

    _pnlButtons = new JPanel();
    _pnlButtons.setBorder(new EmptyBorder(20, 0, 0, 10));
    getContentPane().add(_pnlButtons, BorderLayout.EAST);
    _pnlButtons.setLayout(new BorderLayout(0, 0));

    _pnlInnerButtons = new JPanel();
    _pnlButtons.add(_pnlInnerButtons, BorderLayout.NORTH);
    GridBagLayout gbl_pnlInnerButtons = new GridBagLayout();
    gbl_pnlInnerButtons.columnWidths = new int[] { 0, 0 };
    gbl_pnlInnerButtons.rowHeights = new int[] { 0, 0, 0, 0 };
    gbl_pnlInnerButtons.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
    gbl_pnlInnerButtons.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
    _pnlInnerButtons.setLayout(gbl_pnlInnerButtons);

    _btnFindNext = new JButton();
    _btnFindNext.setAction(_findAction);
    GridBagConstraints gbc_btnFind = new GridBagConstraints();
    gbc_btnFind.fill = GridBagConstraints.HORIZONTAL;
    gbc_btnFind.insets = new Insets(0, 0, 5, 0);
    gbc_btnFind.gridx = 0;
    gbc_btnFind.gridy = 0;
    _pnlInnerButtons.add(_btnFindNext, gbc_btnFind);

    _btnPrevious = new JButton();
    _btnPrevious.setAction(actionMap.get("previousFoundTaxon"));
    _btnPrevious.setEnabled(false);
    GridBagConstraints gbc_btnPrevious = new GridBagConstraints();
    gbc_btnPrevious.insets = new Insets(0, 0, 5, 0);
    gbc_btnPrevious.gridx = 0;
    gbc_btnPrevious.gridy = 1;
    _pnlInnerButtons.add(_btnPrevious, gbc_btnPrevious);

    _btnDone = new JButton();
    _btnDone.setAction(actionMap.get("findTaxaDone"));
    GridBagConstraints gbc_btnDone = new GridBagConstraints();
    gbc_btnDone.fill = GridBagConstraints.HORIZONTAL;
    gbc_btnDone.gridx = 0;
    gbc_btnDone.gridy = 2;
    _pnlInnerButtons.add(_btnDone, gbc_btnDone);

    this.pack();
    this.setLocationRelativeTo(_intkeyApp.getMainFrame());
}

From source file:de.codesourcery.flocking.ui.NumberInputField.java

/**
 * Create instance.//from  ww  w  .  j ava 2s .c  o  m
 *  
 * <p>Creates a resizable panel that holds a label, a textfield and a slider
 * for entering/adjusting a numeric value.</p>
 * 
 * @param label the label to display
 * @param model the model that is used to read/write the value to be edited. If the model returns <code>null</code> values,
 * these will be treated as "0" (or "0.0" respectively).
 * @param minValue valid minimum value (inclusive) the user may enter
 * @param maxValue vali maximum value (inclusive) the user may enter
 * @param onlyIntValues whether the user may enter only integers or integers <b>and</b> floating-point numbers. 
 */
public NumberInputField(String label, IModel<T> model, double minValue, double maxValue,
        final boolean onlyIntValues) {
    if (model == null) {
        throw new IllegalArgumentException("model must not be NULL.");
    }

    this.model = model;
    this.minValue = minValue;
    this.maxValue = maxValue;
    this.onlyIntValues = onlyIntValues;

    textField = new JTextField("0");
    textField.setColumns(5);
    textField.setHorizontalAlignment(JTextField.RIGHT);

    slider = new JSlider(0, SLIDER_RESOLUTION);

    textField.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (selfTriggeredEvent) {
                return;
            }

            final String s = textField.getText();
            if (!StringUtils.isBlank(s)) {
                Number number = null;
                try {
                    if (onlyIntValues) {
                        number = Long.parseLong(s.trim());
                    } else {
                        number = Double.parseDouble(s.trim());
                    }
                } catch (Exception ex) {
                    textField.setText(numberToString(NumberInputField.this.model.getObject()));
                    return;
                }

                updateModelValue(number);
            }
        }
    });

    textField.setText(numberToString(model.getObject()));

    slider.getModel().setValue(calcSliderValue(model.getObject()));
    slider.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            if (selfTriggeredEvent) {
                return;
            }
            final double percentage = slider.getModel().getValue() / (double) SLIDER_RESOLUTION; // 0...1

            final double range = Math.abs(NumberInputField.this.maxValue - NumberInputField.this.minValue);
            final double newValue = NumberInputField.this.minValue + range * percentage;

            updateModelValue(newValue);
        }
    });

    slider.setMinimumSize(new Dimension(100, 20));
    slider.setPreferredSize(new Dimension(100, 20));

    // do layout
    setLayout(new GridBagLayout());

    GridBagConstraints cnstrs = new GridBagConstraints();
    cnstrs.fill = GridBagConstraints.NONE;
    cnstrs.weightx = 0;
    cnstrs.weighty = 0;
    cnstrs.gridx = 0;
    cnstrs.gridy = 0;

    final JLabel l = new JLabel(label);
    l.setMinimumSize(new Dimension(1500, 20));
    l.setPreferredSize(new Dimension(150, 20));
    l.setVerticalAlignment(SwingConstants.TOP);
    add(l, cnstrs);

    cnstrs = new GridBagConstraints();
    cnstrs.fill = GridBagConstraints.NONE;
    cnstrs.weightx = 0;
    cnstrs.weighty = 0;
    cnstrs.gridx = 1;
    cnstrs.gridy = 0;
    cnstrs.insets = new Insets(0, 0, 0, 10);

    add(textField, cnstrs);

    cnstrs = new GridBagConstraints();
    cnstrs.fill = GridBagConstraints.HORIZONTAL;
    cnstrs.weightx = 1.0;
    cnstrs.weighty = 1.0;
    cnstrs.gridx = 2;
    cnstrs.gridy = 0;

    add(slider, cnstrs);
}