Swing - JLabel Adding a text (and HTML) labels to a GUI
Prerequisites - before this lecture You should have seen: The lecture on JFrame The lecture on JButton The lectuer on JTextField Including having seen and run the examples on the above, found at https://github.com/progund/java-extra-lectures/tree/master/swing/componen ts-examples
Text components can use HTML for pimping Text components, such as labels, tool tips etc can be enhanced by HTML Put the text inside <html> tags: <html><strong>some bold text</strong></html> This gives you control over the design fonts, font sizes colors, style html structure such as <p>, <ul>, <h1> etc Even HTML links work, but require quite some extra code
Let s make a GUI where you create clickable links Enter a URL, hit enter. The label will turn into a link which will open your browser with the url loaded.
Swing class rough layout - reminder public class LabelExample { private JFrame window; // more components... public LabelExample() { initcomponents(); layoutcomponents(); addlisteners(); private void initcomponents() { // initialize the components... // more methods (layoutcomponents(), addlisteners()...)
Let s jump right into the code for the JLabel Components (as instance variables as usual): JLabel (just text with the prompt text link will show up here ) JTextField (the text input box where the user enters a url) ActionListener - we ll use the same action on both the textfield and button private JFrame window; private JButton linkbutton; private JLabel link; // will be transformed to a link! private ActionListener linkchanger; // for both button and private JTextField linktextfield; // the text field
Constructor Constructor as before (the class is called LabelExample): public LabelExample() { initcomponents(); layoutcomponents(); addlisteners();
initwindow() - called from initcomponents() private void initwindow() { window = new JFrame("JLabel link example"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setsize(800, 600);
initcomponents() private void initcomponents() { initwindow(); linkbutton = new JButton("Create link"); link = new JLabel("link will show up here"); link.setcursor(cursor.getpredefinedcursor(cursor.hand_cursor)); linktextfield = new JTextField("enter a url here, please"); linktextfield.selectall(); linktextfield.requestfocus(); linktextfield.settooltiptext(new StringBuilder("<html>A tooltip can ").append("have links: ").append("<a href=\"http://wiki.juneday.se\">").append("wiki.junday.se").append("</a></html>").tostring() );
Startup look of the window
layoutcomponents() layoutcomponents becomes: private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(link); window.add(linktextfield); window.add(linkbutton);
Creating the ActionListener linkchanger = e -> { link.setforeground(blue); link.settext("<html><a href=\"" + linktextfield.gettext() + "\">" + linktextfield.gettext() + "</a></html>"); linktextfield.requestfocus(); linktextfield.selectall(); ; linktextfield.addactionlistener(linkchanger); linkbutton.addactionlistener(linkchanger);
Creating the MouseListener - handle clicks on link link.addmouselistener(new MouseAdapter() { @Override public void mouseclicked(mouseevent me) { try { Desktop.getDesktop().browse(new URL(linkTextField.getText()).toURI()); catch (Exception e) { System.err.println("Error clicking link:" + e.getmessage()); link.settext("malformed url"); );
Source code - A novel in 25 volumes import java.awt.cursor; import java.awt.desktop; import java.awt.flowlayout; import java.awt.event.*; import java.net.url; import javax.swing.*; // to make the mouse a hand... // to open the browser... // layout components row-by-row // ActionListener, MouseAdapter // to make a URI to send to the browser // all components public class LabelExample { private JFrame window; private JButton linkbutton; private JLabel link; private ActionListener linkchanger; private JTextField linktextfield;
Source code - A novel in 25 volumes public LabelExample() { initcomponents(); layoutcomponents(); addlisteners(); private void initwindow() { window = new JFrame("JLabel link example"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setsize(800, 600);
Source code - A novel in 25 volumes private void initcomponents() { initwindow(); linkbutton = new JButton("Create link"); link = new JLabel("link will show up here"); link.setcursor(cursor.getpredefinedcursor(cursor.hand_cursor)); linktextfield = new JTextField("enter a url here, please"); linktextfield.selectall(); linktextfield.requestfocus(); linktextfield.settooltiptext(new StringBuilder ("<html>a tooltip can have links: <a href=\"http://wiki.juneday.se\">").append("wiki.junday.se").append("</a></html>").tostring());
Source code - A novel in 25 volumes private void layoutcomponents() { window.setlayout(new FlowLayout()); window.add(link); window.add(linktextfield); window.add(linkbutton);
Source code - A novel in 25 volumes private void addlisteners() { linkchanger = e -> { link.settext("<html><a href=\"" + linktextfield.gettext() + "\">" + linktextfield.gettext() + "</a></html>"); linktextfield.requestfocus(); linktextfield.selectall(); ; linktextfield.addactionlistener(linkchanger); linkbutton.addactionlistener(linkchanger); link.addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent me) { try { Desktop.getDesktop().browse(new URL(linkTextField.getText()).toURI()); catch (Exception e) { System.err.println("Error clicking link:" + e.getmessage()); link.settext("malformed url"); );
Source code - A novel in 25 volumes public void show() { window.setvisible(true); // end-of-class-declaration
Source code - Main class public static void main(string[] args) { try { javax.swing.swingutilities.invokelater(new Runnable() { public void run() { LabelExample labelex = new LabelExample(); labelex.show(); ); catch(exception e) { System.err.println("Exception in main: " + e.getmessage());
Further reading https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html https://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html https://docs.oracle.com/javase/tutorial/uiswing/components/label.html https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html https://docs.oracle.com/javase/8/docs/api/java/awt/event/mouseadapter.html https://docs.oracle.com/javase/tutorial/uiswing/misc/desktop.html