Example usage for javax.swing JLabel JLabel

List of usage examples for javax.swing JLabel JLabel

Introduction

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

Prototype

public JLabel() 

Source Link

Document

Creates a JLabel instance with no image and with an empty string for the title.

Usage

From source file:DisplayMessage.java

public static void main(String[] args) {
    /*//from www  .j  a va2s  . co  m
     * Step 1: Create the components
     */
    JLabel msgLabel = new JLabel(); // Component to display the question
    JButton yesButton = new JButton(); // Button for an affirmative response
    JButton noButton = new JButton(); // Button for a negative response

    /*
     * Step 2: Set properties of the components
     */
    msgLabel.setText(args[0]); // The msg to display
    msgLabel.setBorder(new EmptyBorder(10, 10, 10, 10)); // A 10-pixel margin 
    yesButton.setText((args.length >= 2) ? args[1] : "Yes"); // Text for Yes button
    noButton.setText((args.length >= 3) ? args[2] : "No"); // Text for no button

    /*
     * Step 3: Create containers to hold the components
     */
    JFrame win = new JFrame("Message"); // The main application window
    JPanel buttonbox = new JPanel(); // A container for the two buttons

    /*
     * Step 4: Specify LayoutManagers to arrange components in the containers
     */
    win.getContentPane().setLayout(new BorderLayout()); // layout on borders
    buttonbox.setLayout(new FlowLayout()); // layout left-to-right

    /*
     * Step 5: Add components to containers, with optional layout constraints
     */
    buttonbox.add(yesButton); // add yes button to the panel
    buttonbox.add(noButton); // add no button to the panel

    // add JLabel to window, telling the BorderLayout to put it in the middle
    win.getContentPane().add(msgLabel, "Center");

    // add panel to window, telling the BorderLayout to put it at the bottom
    win.getContentPane().add(buttonbox, "South");

    /*
     * Step 6: Arrange to handle events in the user interface.
     */
    yesButton.addActionListener(new ActionListener() { // Note: inner class
        // This method is called when the Yes button is clicked.
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    noButton.addActionListener(new ActionListener() { // Note: inner class
        // This method is called when the No button is clicked.
        public void actionPerformed(ActionEvent e) {
            System.exit(1);
        }
    });

    /*
     * Step 7: Display the GUI to the user
     */
    win.pack(); // Set the size of the window based its children's sizes.
    win.show(); // Make the window visible.
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame Main = new JFrame("Gradient Mask");
    JLabel imageLayer = new JLabel();
    JLabel maskLayer = new JLabel();
    BufferedImage image = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    BufferedImage gradientMask = new GradientImage(image.getWidth(), image.getHeight(),
            new Color[] { new Color(255, 255, 255, 125), Color.BLACK }, GradientImage.RADIAL_FROM_CENTER)
                    .getImage();//from   w  w w . ja va  2  s  .co  m
    Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Main.setBounds(100, 50, image.getWidth(), image.getHeight());
    imageLayer.setBounds(0, 0, Main.getWidth(), Main.getHeight());
    maskLayer.setBounds(0, 0, Main.getWidth(), Main.getHeight());
    imageLayer.setIcon(new ImageIcon((Image) image));
    maskLayer.setIcon(new ImageIcon((Image) gradientMask));
    Main.getContentPane().add(imageLayer);
    imageLayer.add(maskLayer);
    Main.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Color colors[] = { Color.GREEN, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };
    JFrame frame = new JFrame("Color JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JComboBox comboBox = new JComboBox(colors);
    comboBox.setMaximumRowCount(5);/*from   w  w  w . j  a v  a2  s  . c  o m*/
    comboBox.setEditable(true);
    comboBox.setRenderer(new ColorCellRenderer());
    frame.add(comboBox, BorderLayout.NORTH);

    final JLabel label = new JLabel();
    label.setOpaque(true);
    label.setBackground((Color) comboBox.getSelectedItem());
    frame.add(label, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color selectedColor = (Color) comboBox.getSelectedItem();
            label.setBackground(selectedColor);
        }
    };
    comboBox.addActionListener(actionListener);

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

From source file:FileSample.java

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

    final JLabel directoryLabel = new JLabel();
    contentPane.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel();
    contentPane.add(filenameLabel, BorderLayout.SOUTH);

    final JButton button = new JButton("Open FileChooser");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component parent = (Component) actionEvent.getSource();
            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setAccessory(new LabelAccessory(fileChooser));
            FileView view = new JavaFileView();
            fileChooser.setFileView(view);
            int status = fileChooser.showOpenDialog(parent);
            if (status == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                directoryLabel.setText(selectedFile.getParent());
                filenameLabel.setText(selectedFile.getName());
            } else if (status == JFileChooser.CANCEL_OPTION) {
                directoryLabel.setText(" ");
                filenameLabel.setText(" ");
            }//from  w  w  w.ja v  a2 s. c om
        }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

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

From source file:FilterSample.java

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

    final JLabel directoryLabel = new JLabel();
    contentPane.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel();
    contentPane.add(filenameLabel, BorderLayout.SOUTH);

    final JButton button = new JButton("Open FileChooser");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component parent = (Component) actionEvent.getSource();
            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setAccessory(new LabelAccessory(fileChooser));
            FileFilter filter1 = new ExtensionFileFilter(null, new String[] { "JPG", "JPEG" });
            //        fileChooser.setFileFilter(filter1);
            fileChooser.addChoosableFileFilter(filter1);
            FileFilter filter2 = new ExtensionFileFilter("gif", new String[] { "gif" });
            fileChooser.addChoosableFileFilter(filter2);
            fileChooser.setFileView(new JavaFileView());
            int status = fileChooser.showOpenDialog(parent);
            if (status == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                directoryLabel.setText(selectedFile.getParent());
                filenameLabel.setText(selectedFile.getName());
            } else if (status == JFileChooser.CANCEL_OPTION) {
                directoryLabel.setText(" ");
                filenameLabel.setText(" ");
            }/*www.ja  v  a 2  s  .  c om*/
        }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

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

From source file:ColorComboBox.java

public static void main(String args[]) {
    Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green,
            Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow };
    JFrame frame = new JFrame("Color JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JComboBox comboBox = new JComboBox(colors);
    comboBox.setMaximumRowCount(5);//from   ww  w .j  av a 2  s  .  c o m
    comboBox.setEditable(true);
    comboBox.setRenderer(new ColorCellRenderer());
    Color color = (Color) comboBox.getSelectedItem();
    ComboBoxEditor editor = new ColorComboBoxEditor(color);
    comboBox.setEditor(editor);
    contentPane.add(comboBox, BorderLayout.NORTH);

    final JLabel label = new JLabel();
    label.setOpaque(true);
    label.setBackground((Color) comboBox.getSelectedItem());
    contentPane.add(label, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color selectedColor = (Color) comboBox.getSelectedItem();
            label.setBackground(selectedColor);
        }
    };
    comboBox.addActionListener(actionListener);

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

From source file:Main.java

public static JLabel createSAFLabel(final String name, final JComponent labelForComponent) {
    JLabel label = new JLabel();
    label.setName(name);//from w w w . j  a  va  2  s  .  c o  m
    if (labelForComponent != null) {
        label.setLabelFor(labelForComponent);
    }
    return label;
}

From source file:Main.java

public static void showDialogWhitImage(URL pathuRL) {

    JPanel panel = new JPanel();
    JLabel jLabel = new JLabel();
    jLabel.setIcon(new javax.swing.ImageIcon(pathuRL)); // NOI18N
    panel.add(jLabel);//from  ww  w . j a v a  2s  .c om
    JOptionPane.showMessageDialog(null, panel);
    jLabel = null;
    panel = null;
}

From source file:Main.java

public static void addNewLineTo(Container container) {
    JLabel label = new JLabel();
    label.setPreferredSize(new Dimension(SCREEN_SIZE.width, 0));
    container.add(label);// w  ww .  j a v a  2 s .  c  o m
}

From source file:Main.java

public static Dimension getTextDimension(final String text, final Font font) {
    if (defaultLabel == null) {
        defaultLabel = new JLabel();
    }//from w  w  w  .  j av a2 s  .com

    // get metrics from the graphics
    FontMetrics fontMetrics = defaultLabel.getFontMetrics(font);

    // get the height of a line of text in this
    // font and render context
    int hgt = fontMetrics.getHeight();
    // get the advance of my text in this font
    // and render context
    int adv = fontMetrics.stringWidth(text);
    // calculate the size of a box to hold the text
    Dimension size = new Dimension(adv, hgt);

    return size;
}