hyperlink with JLabel in Java Swing

i have a little problem with my code, meaning that hyperlink not showing under JLabel. I tried to resolve that problem by creating JLabel in different class and run the method in main class, but when i do that, it returning me only white square instead of label. no errors while compiling.

Main class:

import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.Desktop;
import java.awt.event.ActionListener;
import java.net.URI;

public class Main extends JFrame
{
    static JLabel appicationStatute;

    public Main() throws Exception
    {
       super();
       registerWindowFrame = new JFrame("name");                                                                      
       registerWindowFrame.setSize(500,750);
       registerWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                                           
       registerWindowFrame.setLayout(null);
       registerWindowFrame.getContentPane().setBackground(Color.RED);

       Statute status = new Statute();
       status.setSize(status.getPreferredSize());
       status.setLocation(10,450);
       registerWindowFrame.getContentPane().add(status);
       registerWindowFrame.setVisible(true);                                                                         
    }

    public static void main (String [] args) throws Exception
    {
        new Main();                                                                         
    }
}

Statue class

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.*;

public class Statute extends JPanel {
    private String text = "application statute";
    private JLabel hyperlink = new JLabel(text);

    public Statute() throws HeadlessException {
        super();
        hyperlink.setForeground(Color.BLUE.darker());
        hyperlink.setCursor(new Cursor(Cursor.HAND_CURSOR));
        hyperlink.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    Desktop.getDesktop().browse(new URI("http://www.codejava.net"));
                } catch (IOException | URISyntaxException e1) {
                    e1.printStackTrace();
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                hyperlink.setText(text);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                hyperlink.setText("<html><a href=''>" + text + "</a></html>");
            }

        });
    }
}

Leave a Reply

avatar
  Subscribe  
Notify of