Example usage for javax.swing JLabel setLabelFor

List of usage examples for javax.swing JLabel setLabelFor

Introduction

In this page you can find the example usage for javax.swing JLabel setLabelFor.

Prototype

@BeanProperty(description = "The component this is labelling.")
public void setLabelFor(Component c) 

Source Link

Document

Set the component this is labelling.

Usage

From source file:LabelForComboBox.java

public LabelForComboBox() {
    JLabel lbl = new JLabel("Color:");
    lbl.setDisplayedMnemonic('c');
    add(lbl);//w w  w  .j a v a  2  s  .  c  o  m
    add(Box.createHorizontalStrut(20));
    JComboBox cbColor = new JComboBox();
    cbColor.addItem("red");
    cbColor.addItem("blue");
    lbl.setLabelFor(cbColor);
    add(cbColor);
}

From source file:QandE.Beeper2.java

public Beeper2() {
    super(new BorderLayout());

    //Create and set up the spinner.
    String numBeepsString = "Number of Beeps: ";
    beepsModel = new SpinnerNumberModel(1, 1, 10, 1);
    JLabel numBeepsLabel = new JLabel(numBeepsString);
    add(numBeepsLabel, BorderLayout.PAGE_START);
    JSpinner spinner = new JSpinner(beepsModel);
    numBeepsLabel.setLabelFor(spinner);
    add(spinner, BorderLayout.CENTER);

    button = new JButton("Click Me");
    button.setPreferredSize(new Dimension(200, 80));
    add(button, BorderLayout.PAGE_END);
    button.setActionCommand(BUTTON_CMD);
    button.addActionListener(this);
}

From source file:Main.java

public Main() throws HeadlessException {
    setSize(400, 200);//from w ww  .ja  va  2  s.  c  o  m
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JLabel usernameLabel = new JLabel("Username: ");
    JLabel passwordLabel = new JLabel("Password: ");
    JTextField usernameField = new JTextField(20);
    JPasswordField passwordField = new JPasswordField(20);

    usernameLabel.setDisplayedMnemonic(KeyEvent.VK_U);
    usernameLabel.setLabelFor(usernameField);
    passwordLabel.setDisplayedMnemonic(KeyEvent.VK_P);
    passwordLabel.setLabelFor(passwordField);

    getContentPane().add(usernameLabel);
    getContentPane().add(usernameField);
    getContentPane().add(passwordLabel);
    getContentPane().add(passwordField);
}

From source file:components.ListDialog.java

private ListDialog(Frame frame, Component locationComp, String labelText, String title, Object[] data,
        String initialValue, String longValue) {
    super(frame, title, true);

    //Create and initialize the buttons.
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(this);
    ///*from   ww w  . j  av  a  2  s.  c  o  m*/
    final JButton setButton = new JButton("Set");
    setButton.setActionCommand("Set");
    setButton.addActionListener(this);
    getRootPane().setDefaultButton(setButton);

    //main part of the dialog
    list = new JList(data) {
        //Subclass JList to workaround bug 4832765, which can cause the
        //scroll pane to not let the user easily scroll up to the beginning
        //of the list.  An alternative would be to set the unitIncrement
        //of the JScrollBar to a fixed value. You wouldn't get the nice
        //aligned scrolling, but it should work.
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            int row;
            if (orientation == SwingConstants.VERTICAL && direction < 0
                    && (row = getFirstVisibleIndex()) != -1) {
                Rectangle r = getCellBounds(row, row);
                if ((r.y == visibleRect.y) && (row != 0)) {
                    Point loc = r.getLocation();
                    loc.y--;
                    int prevIndex = locationToIndex(loc);
                    Rectangle prevR = getCellBounds(prevIndex, prevIndex);

                    if (prevR == null || prevR.y >= r.y) {
                        return 0;
                    }
                    return prevR.height;
                }
            }
            return super.getScrollableUnitIncrement(visibleRect, orientation, direction);
        }
    };

    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    if (longValue != null) {
        list.setPrototypeCellValue(longValue); //get extra space
    }
    list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    list.setVisibleRowCount(-1);
    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                setButton.doClick(); //emulate button click
            }
        }
    });
    JScrollPane listScroller = new JScrollPane(list);
    listScroller.setPreferredSize(new Dimension(250, 80));
    listScroller.setAlignmentX(LEFT_ALIGNMENT);

    //Create a container so that we can add a title around
    //the scroll pane.  Can't add a title directly to the
    //scroll pane because its background would be white.
    //Lay out the label and scroll pane from top to bottom.
    JPanel listPane = new JPanel();
    listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
    JLabel label = new JLabel(labelText);
    label.setLabelFor(list);
    listPane.add(label);
    listPane.add(Box.createRigidArea(new Dimension(0, 5)));
    listPane.add(listScroller);
    listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    //Lay out the buttons from left to right.
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    buttonPane.add(Box.createHorizontalGlue());
    buttonPane.add(cancelButton);
    buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
    buttonPane.add(setButton);

    //Put everything together, using the content pane's BorderLayout.
    Container contentPane = getContentPane();
    contentPane.add(listPane, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.PAGE_END);

    //Initialize values.
    setValue(initialValue);
    pack();
    setLocationRelativeTo(locationComp);
}

From source file:eu.delving.sip.frames.OutputFrame.java

private JPanel createOutputPanel() {
    final JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createTitledBorder("Output record"));
    outputArea = new JTextArea(sipModel.getRecordCompileModel().getOutputDocument());
    outputArea.setLineWrap(true);//from w w  w. j a  v a  2 s. co  m
    outputArea.setFont(MONOSPACED);
    outputArea.setWrapStyleWord(true);
    outputArea.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent documentEvent) {
            try {
                String first = documentEvent.getDocument().getText(0, 1);
                final boolean error = first.startsWith("#");
                setError(outputArea, error);
                sipModel.exec(new Swing() {
                    @Override
                    public void run() {
                        outputArea.setCaretPosition(0);
                    }
                });
            } catch (BadLocationException e) {
                // who cares
            }
        }

        @Override
        public void removeUpdate(DocumentEvent documentEvent) {
        }

        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
        }
    });
    outputArea.getInputMap().put(KeystrokeHelper.DOWN, "next");
    outputArea.getInputMap().put(KeystrokeHelper.RIGHT, "next");
    outputArea.getInputMap().put(KeystrokeHelper.UP, "prev");
    outputArea.getInputMap().put(KeystrokeHelper.LEFT, "prev");
    outputArea.getActionMap().put("next", new NextAction());
    outputArea.getActionMap().put("prev", new PrevAction());
    searchField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            found.clear();
            foundSelected = 0;
            String xml = outputArea.getText().toLowerCase();
            String sought = searchField.getText().toLowerCase();
            if (!sought.isEmpty()) {
                int start = 0;
                while (found.size() < 30) {
                    int pos = xml.indexOf(sought, start);
                    if (pos < 0)
                        break;
                    found.add(pos);
                    start = pos + sought.length();
                }
            }
            selectFound();
        }
    });
    JPanel sp = new JPanel();
    JLabel label = new JLabel("Search:", JLabel.RIGHT);
    label.setLabelFor(searchField);
    sp.add(label);
    sp.add(searchField);
    sp.add(new JLabel("Press ENTER, then use arrow keys"));
    p.add(scrollVH(outputArea), BorderLayout.CENTER);
    p.add(sp, BorderLayout.SOUTH);
    return p;
}

From source file:ShowDocument.java

public URLWindow(AppletContext appletContext) {
    super("Show a Document!");
    this.appletContext = appletContext;

    JPanel contentPane = new JPanel(new GridBagLayout());
    setContentPane(contentPane);/*w ww . j a  va2s  .c  om*/
    contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    JLabel label1 = new JLabel("URL of document to show: ", JLabel.TRAILING);
    add(label1, c);

    urlField = new JTextField("http://java.sun.com/", 20);
    label1.setLabelFor(urlField);
    urlField.addActionListener(this);
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weightx = 1.0;
    add(urlField, c);

    JLabel label2 = new JLabel("Window/frame to show it in: ", JLabel.TRAILING);
    c.gridwidth = 1;
    c.weightx = 0.0;
    add(label2, c);

    String[] strings = { "(browser's choice)", //don't specify
            "My Personal Window", //a window named "My Personal Window"
            "_blank", //a new, unnamed window
            "_self", "_parent", "_top" //the Frame that contained this applet
    };
    choice = new JComboBox(strings);
    label2.setLabelFor(choice);
    c.fill = GridBagConstraints.NONE;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.insets = new Insets(5, 0, 0, 0);
    c.anchor = GridBagConstraints.LINE_START;
    add(choice, c);

    JButton button = new JButton("Show document");
    button.addActionListener(this);
    c.weighty = 1.0;
    c.ipadx = 10;
    c.ipady = 10;
    c.insets = new Insets(10, 0, 0, 0);
    c.anchor = GridBagConstraints.PAGE_END;
    add(button, c);
}

From source file:de.atomfrede.tools.evalutation.ui.ExceptionDialog.java

public JComponent createDetailsPanel() {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    exception.printStackTrace(pw);/* ww w .j a  v a2s  . c  o  m*/
    JTextArea textArea = new JTextArea(sw.toString());
    // textArea.setRows(10);

    JLabel label = new JLabel("Details:");

    JPanel panel = new JPanel(new BorderLayout(6, 6));
    panel.add(new JScrollPane(textArea));
    panel.add(label, BorderLayout.BEFORE_FIRST_LINE);
    label.setLabelFor(textArea);
    panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));

    setSize((int) panel.getPreferredSize().getWidth() + 20, getHeight() + 95);
    setMinimumSize(getSize());
    setMaximumSize(new Dimension(getSize().width, getSize().height + 150));
    panel.setMaximumSize(new Dimension((int) getMaximumSize().getWidth(), 150));
    return panel;
}

From source file:components.TextSamplerDemo.java

public TextSamplerDemo() {
    setLayout(new BorderLayout());

    //Create a regular text field.
    JTextField textField = new JTextField(10);
    textField.setActionCommand(textFieldString);
    textField.addActionListener(this);

    //Create a password field.
    JPasswordField passwordField = new JPasswordField(10);
    passwordField.setActionCommand(passwordFieldString);
    passwordField.addActionListener(this);

    //Create a formatted text field.
    JFormattedTextField ftf = new JFormattedTextField(java.util.Calendar.getInstance().getTime());
    ftf.setActionCommand(textFieldString);
    ftf.addActionListener(this);

    //Create some labels for the fields.
    JLabel textFieldLabel = new JLabel(textFieldString + ": ");
    textFieldLabel.setLabelFor(textField);
    JLabel passwordFieldLabel = new JLabel(passwordFieldString + ": ");
    passwordFieldLabel.setLabelFor(passwordField);
    JLabel ftfLabel = new JLabel(ftfString + ": ");
    ftfLabel.setLabelFor(ftf);/*from   w w w.  j a  v  a 2  s . c o m*/

    //Create a label to put messages during an action event.
    actionLabel = new JLabel("Type text in a field and press Enter.");
    actionLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));

    //Lay out the text controls and the labels.
    JPanel textControlsPane = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

    textControlsPane.setLayout(gridbag);

    JLabel[] labels = { textFieldLabel, passwordFieldLabel, ftfLabel };
    JTextField[] textFields = { textField, passwordField, ftf };
    addLabelTextRows(labels, textFields, gridbag, textControlsPane);

    c.gridwidth = GridBagConstraints.REMAINDER; //last
    c.anchor = GridBagConstraints.WEST;
    c.weightx = 1.0;
    textControlsPane.add(actionLabel, c);
    textControlsPane.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Text Fields"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Create a text area.
    JTextArea textArea = new JTextArea("This is an editable JTextArea. "
            + "A text area is a \"plain\" text component, " + "which means that although it can display text "
            + "in any font, all of the text is in the same font.");
    textArea.setFont(new Font("Serif", Font.ITALIC, 16));
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    JScrollPane areaScrollPane = new JScrollPane(textArea);
    areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    areaScrollPane.setPreferredSize(new Dimension(250, 250));
    areaScrollPane.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Plain Text"),
                    BorderFactory.createEmptyBorder(5, 5, 5, 5)),
            areaScrollPane.getBorder()));

    //Create an editor pane.
    JEditorPane editorPane = createEditorPane();
    JScrollPane editorScrollPane = new JScrollPane(editorPane);
    editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    editorScrollPane.setPreferredSize(new Dimension(250, 145));
    editorScrollPane.setMinimumSize(new Dimension(10, 10));

    //Create a text pane.
    JTextPane textPane = createTextPane();
    JScrollPane paneScrollPane = new JScrollPane(textPane);
    paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    paneScrollPane.setPreferredSize(new Dimension(250, 155));
    paneScrollPane.setMinimumSize(new Dimension(10, 10));

    //Put the editor pane and the text pane in a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, editorScrollPane, paneScrollPane);
    splitPane.setOneTouchExpandable(true);
    splitPane.setResizeWeight(0.5);
    JPanel rightPane = new JPanel(new GridLayout(1, 0));
    rightPane.add(splitPane);
    rightPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Styled Text"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Put everything together.
    JPanel leftPane = new JPanel(new BorderLayout());
    leftPane.add(textControlsPane, BorderLayout.PAGE_START);
    leftPane.add(areaScrollPane, BorderLayout.CENTER);

    add(leftPane, BorderLayout.LINE_START);
    add(rightPane, BorderLayout.LINE_END);
}

From source file:TextSamplerDemo.java

public TextSamplerDemo() {
    setLayout(new BorderLayout());

    //Create a regular text field.
    JTextField textField = new JTextField(10);
    textField.setActionCommand(textFieldString);
    textField.addActionListener(this);

    //Create a password field.
    JPasswordField passwordField = new JPasswordField(10);
    passwordField.setActionCommand(passwordFieldString);
    passwordField.addActionListener(this);

    //Create a formatted text field.
    JFormattedTextField ftf = new JFormattedTextField(java.util.Calendar.getInstance().getTime());
    ftf.setActionCommand(textFieldString);
    ftf.addActionListener(this);

    //Create some labels for the fields.
    JLabel textFieldLabel = new JLabel(textFieldString + ": ");
    textFieldLabel.setLabelFor(textField);
    JLabel passwordFieldLabel = new JLabel(passwordFieldString + ": ");
    passwordFieldLabel.setLabelFor(passwordField);
    JLabel ftfLabel = new JLabel(ftfString + ": ");
    ftfLabel.setLabelFor(ftf);/*from  w  w w. jav  a 2  s  . c  o m*/

    //Create a label to put messages during an action event.
    actionLabel = new JLabel("Type text and then Enter in a field.");
    actionLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));

    //Lay out the text controls and the labels.
    JPanel textControlsPane = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

    textControlsPane.setLayout(gridbag);

    JLabel[] labels = { textFieldLabel, passwordFieldLabel, ftfLabel };
    JTextField[] textFields = { textField, passwordField, ftf };
    addLabelTextRows(labels, textFields, gridbag, textControlsPane);

    c.gridwidth = GridBagConstraints.REMAINDER; //last
    c.anchor = GridBagConstraints.WEST;
    c.weightx = 1.0;
    textControlsPane.add(actionLabel, c);
    textControlsPane.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Text Fields"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Create a text area.
    JTextArea textArea = new JTextArea("This is an editable JTextArea. "
            + "A text area is a \"plain\" text component, " + "which means that although it can display text "
            + "in any font, all of the text is in the same font.");
    textArea.setFont(new Font("Serif", Font.ITALIC, 16));
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    JScrollPane areaScrollPane = new JScrollPane(textArea);
    areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    areaScrollPane.setPreferredSize(new Dimension(250, 250));
    areaScrollPane.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Plain Text"),
                    BorderFactory.createEmptyBorder(5, 5, 5, 5)),
            areaScrollPane.getBorder()));

    //Create an editor pane.
    JEditorPane editorPane = createEditorPane();
    JScrollPane editorScrollPane = new JScrollPane(editorPane);
    editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    editorScrollPane.setPreferredSize(new Dimension(250, 145));
    editorScrollPane.setMinimumSize(new Dimension(10, 10));

    //Create a text pane.
    JTextPane textPane = createTextPane();
    JScrollPane paneScrollPane = new JScrollPane(textPane);
    paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    paneScrollPane.setPreferredSize(new Dimension(250, 155));
    paneScrollPane.setMinimumSize(new Dimension(10, 10));

    //Put the editor pane and the text pane in a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, editorScrollPane, paneScrollPane);
    splitPane.setOneTouchExpandable(true);
    splitPane.setResizeWeight(0.5);
    JPanel rightPane = new JPanel(new GridLayout(1, 0));
    rightPane.add(splitPane);
    rightPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Styled Text"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Put everything together.
    JPanel leftPane = new JPanel(new BorderLayout());
    leftPane.add(textControlsPane, BorderLayout.PAGE_START);
    leftPane.add(areaScrollPane, BorderLayout.CENTER);

    add(leftPane, BorderLayout.LINE_START);
    add(rightPane, BorderLayout.LINE_END);
}

From source file:net.vanosten.dings.swing.SummaryView.java

private void initializeChartPropertiesPanel() {
    chartPropertiesP = new JPanel();
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    chartPropertiesP.setLayout(gbl);//from  w w  w. ja v  a 2 s . c  om

    TitledBorder title;
    title = BorderFactory.createTitledBorder("Chart Properties");
    chartPropertiesP.setBorder(title);

    JLabel typeL = new JLabel("Chart Type:");
    typeL.setDisplayedMnemonic("C".charAt(0));
    typeL.setLabelFor(chartTypeCB);
    JLabel emptyL = new JLabel();

    Insets vghg = new Insets(DingsSwingConstants.SP_V_G, DingsSwingConstants.SP_H_G, 0, 0);
    //----type
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.LINE_END;
    gbc.insets = vghg;
    gbl.setConstraints(typeL, gbc);
    chartPropertiesP.add(typeL);
    //----
    gbc.gridx = 1;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbl.setConstraints(chartTypeCB, gbc);
    chartPropertiesP.add(chartTypeCB);
    //----
    gbc.gridx = 2;
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbl.setConstraints(emptyL, gbc);
    chartPropertiesP.add(emptyL);
    //----chosen
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.weightx = 0.0;
    gbc.fill = GridBagConstraints.NONE;
    gbl.setConstraints(chosenCB, gbc);
    chartPropertiesP.add(chosenCB);
}