Example usage for javax.swing JLabel setText

List of usage examples for javax.swing JLabel setText

Introduction

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

Prototype

@BeanProperty(preferred = true, visualUpdate = true, description = "Defines the single line of text this component will display.")
public void setText(String text) 

Source Link

Document

Defines the single line of text this component will display.

Usage

From source file:Main.java

public static void setLabelTop(JLabel label, String str, int width, int height) {
    Graphics g = label.getGraphics();
    label.setText(str);
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = width / 2 - strWidth / 2;
    int lY = g.getFont().getSize() / 2;
    //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY);
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
}

From source file:Main.java

public static void setLabelAboveOut(JLabel label, String str, int x, int y, int width) {
    Graphics g = label.getGraphics();
    label.setText(str);
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = x + width / 2 - strWidth / 2;
    int lY = y - g.getFont().getSize();
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
}

From source file:Main.java

/**
 * Returns the preferred size for a label.
 * /* w w  w.ja v a2  s.co  m*/
 * @param label The label.
 * @param sampleText The sample text used to get the size.
 * @return The preferred size.
 */
public static Dimension getLabelPreferredSize(JLabel label, String sampleText) {
    String text = label.getText();
    label.setText(sampleText);
    Dimension size = label.getPreferredSize();
    label.setText(text);
    return size;
}

From source file:Main.java

/**
 * Create a HTML hyperlink in JLabel component
 *
 * @param label//from  w  w  w.  ja v  a 2s  .  com
 * @param url
 * @param text
 */
public static void createHyperLink(JLabel label, final String url, String text) {
    label.setToolTipText(url);
    label.setText("<html><a href=\"\">" + text + "</a></html>");
    label.setCursor(new Cursor(Cursor.HAND_CURSOR));
    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                Desktop.getDesktop().browse(new URI(url));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            } catch (URISyntaxException ex) {
                throw new RuntimeException(ex);
            }
        }
    });
}

From source file:com.aan.girsang.client.launcher.ClientLauncher.java

public static void jam(JLabel lbl) {
    Thread t = new Thread(() -> {
        while (true) {
            lbl.setText(new SimpleDateFormat("EEEE, dd MMMM yyyy HH:mm:ss").format(new Date()));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Exceptions.printStackTrace(ex);
            }/*from  w  w w . ja v a 2  s.  c  o  m*/
        }
    });
    t.start();
}

From source file:Main.java

public static void setLabelCenter(JLabel label, String str, int width, int height) {
    Graphics g = label.getGraphics();
    Color c = g.getColor();/*from   w ww  .  ja  va  2 s. c om*/
    g.setColor(Color.BLUE);
    label.setText(str);
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = width / 2 - strWidth;
    int lY = g.getFont().getSize() / 2;
    //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY);
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
    g.setColor(c);
}

From source file:Main.java

private static JPanel createPanel() {
    JPanel panel = new JPanel();
    final JLabel label = new JLabel(new Date().toString());
    panel.add(label);/*from  www  . j a va2  s.  c o  m*/
    panel.addAncestorListener(new AncestorListener() {
        @Override
        public void ancestorAdded(AncestorEvent event) {
            // start animation
            label.setText(new Date().toString());
        }

        @Override
        public void ancestorRemoved(AncestorEvent event) {
            // stop animation
        }

        @Override
        public void ancestorMoved(AncestorEvent event) {
        }
    });
    return panel;
}

From source file:Main.java

/**
 * Configures a label as if it was an hyperlink.
 * /*w  w w.  j  a va  2 s.c o m*/
 * @param label
 *            the label to configure.
 */
public static void configureLabelAsHyperlink(JLabel label) {
    if (label == null) {
        return;
    }

    StringBuffer html = new StringBuffer();
    html.append("<html><font color=\"blue\"><u>");
    html.append(label.getText());
    html.append("</u></font></html>");

    label.setText(html.toString());
    label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

From source file:QandE.MyDemo1.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.//from  w w  w  .  ja  v  a2s.co m
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("MyDemo1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add a label with bold italic font.
    JLabel label = new JLabel("My Demo");
    frame.getContentPane().add(BorderLayout.CENTER, label);
    if (true) {
        label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD));
    } else {
        //another way of doing it, but not as appropriate since 
        //setFont is faster than using HTML.
        label.setText("<html><i>My Demo</i></html>");
    }
    label.setHorizontalAlignment(JLabel.CENTER);

    //Display the window, making it a little bigger than it really needs to be.
    frame.pack();
    frame.setSize(frame.getWidth() + 100, frame.getHeight() + 50);
    frame.setVisible(true);
}

From source file:QandE.MyDemo3.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.//  w w w.jav  a  2s . c om
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("MyDemo3");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add a label with bold italic font.
    JLabel label = new JLabel("My Demo");
    frame.getContentPane().add(BorderLayout.CENTER, label);
    if (true) {
        label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD));
    } else {
        //another way of doing it, but not as appropriate since 
        //setFont is faster than using HTML.
        label.setText("<html><i>My Demo</i></html>");
    }
    label.setHorizontalAlignment(JLabel.CENTER);

    JButton b = new JButton("A button");
    frame.getContentPane().add(BorderLayout.PAGE_END, b);
    frame.getRootPane().setDefaultButton(b);

    //Display the window, making it a little bigger than it really needs to be.
    frame.pack();
    frame.setSize(frame.getWidth() + 100, frame.getHeight() + 50);
    frame.setVisible(true);
}