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(Icon image) 

Source Link

Document

Creates a JLabel instance with the specified image.

Usage

From source file:Main.java

public static void main(String[] args) throws AWTException {
    Runnable r = new Runnable() {
        @Override// w  ww. j a v  a 2 s. c o m
        public void run() {
            try {
                URL url = new URL("http://www.java2s.com/style/download.png");
                BufferedImage bi = ImageIO.read(url);
                JPanel gui = new JPanel(new GridLayout(1, 2, 2, 2));

                gui.add(new JLabel(new ImageIcon(bi)));
                gui.add(new JLabel(new ImageIcon(getFlippedImage(bi))));

                JOptionPane.showMessageDialog(null, gui);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    JTextPane textPane = new JTextPane();
    JTextField tf = new JTextField("is");
    String word = "";
    Highlighter highlighter = new UnderlineHighlighter(null);

    textPane.setHighlighter(highlighter);
    textPane.setText("This is a test");
    panel.setLayout(new BorderLayout());
    panel.add(new JLabel("Enter word, then press ENTER key: "), "West");
    panel.add(tf, "Center");

    final WordSearcher searcher = new WordSearcher(textPane);
    tf.addActionListener(e -> {//from  w  w  w. j  av a2  s.  c  o m
        String w = tf.getText().trim();
        int offset = searcher.search(w);
        if (offset == -1) {
            return;
        }
        try {
            textPane.scrollRectToVisible(textPane.modelToView(offset));
        } catch (BadLocationException ex) {
        }

    });
    textPane.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent evt) {
            searcher.search(word);
        }

        @Override
        public void removeUpdate(DocumentEvent evt) {
            searcher.search(word);
        }

        @Override
        public void changedUpdate(DocumentEvent evt) {
        }
    });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(panel, "South");
    f.add(new JScrollPane(textPane), "Center");
    f.setSize(400, 400);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Object[][] data = { { "A", new Integer(3), new Double(7.23), new Boolean(true) },
            { "J", new Integer(2), new Double(4.64), new Boolean(false) },
            { "S", new Integer(1), new Double(8.81), new Boolean(true) } };

    String[] columns = { "Col", "Col", "Col", "Col" };

    JTable table = new JTable(data, columns);
    JScrollPane scroll = new JScrollPane(table);

    JFrame f = new JFrame();
    f.setContentPane(scroll);//from   w w w. j  av  a 2s.  c o m
    f.pack();

    int x = (int) table.getTableHeader().getSize().getWidth();
    int y = (int) table.getTableHeader().getSize().getHeight() + (int) table.getSize().getHeight();

    BufferedImage bi = new BufferedImage((int) x, (int) y, BufferedImage.TYPE_INT_RGB);

    Graphics g = bi.createGraphics();
    table.getTableHeader().paint(g);
    g.translate(0, table.getTableHeader().getHeight());
    table.paint(g);
    g.dispose();

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
    ImageIO.write(bi, "png", new File("c:/Java_Dev/table.png"));

    System.exit(0);
}

From source file:SampleSliders.java

public static void main(String args[]) {
    String title = (args.length == 0 ? "Sample Slider" : args[0]);
    JFrame f = new JFrame(title);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider js1 = new JSlider();
    js1.putClientProperty("JSlider.isFilled", Boolean.TRUE);
    JSlider js2 = new JSlider();
    js2.setMajorTickSpacing(25);/*from w w  w .  j a  v  a2 s.c  o  m*/
    js2.setPaintTicks(true);
    js2.setSnapToTicks(true);
    JSlider js3 = new JSlider(JSlider.VERTICAL);
    js3.setPaintTrack(false);
    js3.setMinorTickSpacing(5);
    js3.setMajorTickSpacing(10);
    js3.setPaintTicks(true);
    js3.setPaintLabels(true);
    js3.setSnapToTicks(true);
    JSlider js4 = new JSlider(JSlider.VERTICAL);
    Hashtable table = new Hashtable();
    table.put(new Integer(0), new JLabel(new DiamondIcon(Color.red)));
    table.put(new Integer(10), new JLabel("Ten"));
    table.put(new Integer(25), new JLabel("Twenty-Five"));
    table.put(new Integer(34), new JLabel("Thirty-Four"));
    table.put(new Integer(52), new JLabel("Fifty-Two"));
    table.put(new Integer(70), new JLabel("Seventy"));
    table.put(new Integer(82), new JLabel("Eighty-Two"));
    table.put(new Integer(100), new JLabel(new DiamondIcon(Color.black)));
    js4.setLabelTable(table);
    js4.setPaintLabels(true);
    js4.setSnapToTicks(true);
    Container c = f.getContentPane();
    c.add(js1, BorderLayout.NORTH);
    c.add(js2, BorderLayout.SOUTH);
    c.add(js3, BorderLayout.WEST);
    c.add(js4, BorderLayout.EAST);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:RegexPatternFormatter.java

public static void main(String argv[]) {
    // a demo main() to show how RegexPatternFormatter could be used

    JLabel lab1 = new JLabel("even length strings:");
    java.util.regex.Pattern evenLength = java.util.regex.Pattern.compile("(..)*");
    JFormattedTextField ftf1 = new JFormattedTextField(new RegexPatternFormatter(evenLength));

    JLabel lab2 = new JLabel("no vowels:");
    java.util.regex.Pattern noVowels = java.util.regex.Pattern.compile("[^aeiou]*",
            java.util.regex.Pattern.CASE_INSENSITIVE);
    RegexPatternFormatter noVowelFormatter = new RegexPatternFormatter(noVowels);
    noVowelFormatter.setAllowsInvalid(false); // don't allow user to type
    // vowels//  w ww.  ja  v a  2  s . c o  m
    JFormattedTextField ftf2 = new JFormattedTextField(noVowelFormatter);

    JFrame f = new JFrame("RegexPatternFormatter Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pan1 = new JPanel(new java.awt.BorderLayout());
    pan1.add(lab1, java.awt.BorderLayout.WEST);
    pan1.add(ftf1, java.awt.BorderLayout.CENTER);
    lab1.setLabelFor(ftf1);
    f.getContentPane().add(pan1, java.awt.BorderLayout.NORTH);
    JPanel pan2 = new JPanel(new java.awt.BorderLayout());
    pan2.add(lab2, java.awt.BorderLayout.WEST);
    pan2.add(ftf2, java.awt.BorderLayout.CENTER);
    lab2.setLabelFor(ftf2);
    f.getContentPane().add(pan2, java.awt.BorderLayout.SOUTH);
    f.setSize(300, 80);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final int width = 512;
    final int height = 512;
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics g = img.getGraphics();
    g.setColor(Color.black);//  w w  w.  j ava  2s  . c om
    g.fillRect(0, 0, width, height);
    g.setColor(Color.white);
    final double A = 8;
    final double B = 0.5;
    final double N = 4;
    final double scale = 128;
    final double zoom = 50;
    final double step = 1 / scale;
    Point last = null;
    final Point origin = new Point(width / 2, height / 2);

    for (double t = 0; t <= 2 * Math.PI; t += step) {
        final double r = zoom * polarFunction(t, A, B, N);
        final int x = (int) Math.round(r * Math.cos(t));
        final int y = (int) Math.round(r * Math.sin(t));
        Point next = new Point(x, y);
        if (last != null) {
            g.drawLine(origin.x + last.x, origin.y + last.y, origin.x + next.x, origin.y + next.y);
        }
        last = next;
    }

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

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

    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
    StyleConstants.setFontSize(style, 14);
    StyleConstants.setSpaceAbove(style, 4);
    StyleConstants.setSpaceBelow(style, 4);

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    StyleConstants.setBold(attributes, true);
    StyleConstants.setItalic(attributes, true);

    // Third style for icon/component
    Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);

    Icon icon = new ImageIcon("Computer.gif");
    JLabel label = new JLabel(icon);
    StyleConstants.setComponent(labelStyle, label);

    try {/*ww w . ja  v  a 2 s.  c o m*/
        document.insertString(document.getLength(), "Hello www.java2s.com", attributes);
        document.insertString(document.getLength(), "Ignored", labelStyle);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);
    frame.add(scrollPane, BorderLayout.CENTER);

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    final BufferedImage bi = ImageIO.read(url);
    SwingUtilities.invokeLater(new Runnable() {
        @Override/*from ww  w  .  j av  a  2  s  .c  o m*/
        public void run() {
            int strokeWidth = 12;
            int pad = 50;
            Shape shape = new Rectangle(pad, pad, bi.getWidth() - (2 * pad), bi.getHeight() - (2 * pad));
            Image i = getStrokedImage(bi, shape, strokeWidth);
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(i)));
        }
    });
}

From source file:com.rest.samples.getImagePrintJPanel.java

public static void main(String[] args) {
    // TODO code application logic here
    String url = "https://api.adorable.io/avatars/eyes5";
    try {//  w w w.  j a va  2 s. co  m
        HttpClient hc = HttpClientBuilder.create().build();
        HttpGet getMethod = new HttpGet(url);
        getMethod.addHeader("accept", "application/png");
        HttpResponse res = hc.execute(getMethod);
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode());
        }

        InputStream is = res.getEntity().getContent();

        Image image = ImageIO.read(is);
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

    } catch (IOException ex) {
        Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Main.java

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

    final JPopupMenu popup = new JPopupMenu();
    JMenuItem menuItem1 = new JMenuItem("Option 1");
    popup.add(menuItem1);//from www  .j ava2  s  .  co m

    JMenuItem menuItem2 = new JMenuItem("Option 2");
    popup.add(menuItem2);

    final JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                int dotPosition = textField.getCaretPosition();
                Rectangle popupLocation = textField.modelToView(dotPosition);
                popup.show(textField, popupLocation.x, popupLocation.y);
            } catch (BadLocationException badLocationException) {
                System.err.println("Oops");
            }
        }
    };
    KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false);
    textField.registerKeyboardAction(actionListener, keystroke, JComponent.WHEN_FOCUSED);

    frame.add(new JLabel("Press '.' to activate Popup menu"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);
}