Is it possibly to create selectable hyperlink with basic Swing components in Java?

You can display HTML content in a non-editable JEditorPane It's selectable, and you can make the links functional via a HyperlinkListener.

You can display HTML content in a non-editable JEditorPane. It's selectable, and you can make the links functional via a HyperlinkListener: JEditorPane content = new JEditorPane(); content. SetContentType("text/html"); content.

SetEditable(false); content. SetText("Link")); content. AddHyperlinkListener(new HyperlinkListener() { @Override public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.

ACTIVATED) { try { Desktop.getDesktop(). Browse(e.getURL().toURI()); } catch (Exception e1) { Logger. GetLogger(getClass()).

Error( "Error opening link " + e.getURL(), e1); } } } }).

Here how can you create a JLabel with a hyperlink ,then you can just add it to your Jpanel: public HyperLinkLabel() { JPanel p = new JPanel(); final String strURL = "yahoo.com"; final JLabel label = new JLabel(" click "); final JEditorPane htmlPane = new JEditorPane(); p. Add(label); getContentPane(). Add(BorderLayout.

NORTH, p); getContentPane(). Add(BorderLayout. CENTER, new JScrollPane(htmlPane)); setBounds(20,200, 500,500); label.

AddMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { label. SetCursor(new Cursor(Cursor. HAND_CURSOR)); } public void mouseExited(MouseEvent me) { label.

SetCursor(Cursor. GetDefaultCursor()); } public void mouseClicked(MouseEvent me) { System.out. Println("Clicked on Label..."); try { htmlPane.

SetPage(new URL(strURL)); } catch(Exception e) { System.out. Println(e); } } }).

2 The problem with your example as it stands, is that it is not keyboard focusable. Though such functionality is easily added. 1) Set the JLabel focusable.2) Add a FocusListener that changes the color of the text (i.e.

SetForeground(Color)) on focus gained/lost. Changing the Color might be a good idea in the MouseListener as well. – Andrew Thompson Apr 8 at 22:20.

You have to create a custom Jlabel extend Jlabel and write a MouseListener for the JLabel. Your mouse listener must do the job of directing the user to the link when the user clicks on the custom JLabel. The mouse event basically the method of the MouseListener interface where you have to write the redirecting code that you are looking for is mouseClicked.

See my comment to 'sfrj'. – Andrew Thompson Apr 8 at 22:20 Is my solution incorrect? I gave the above solution of creating a custom component in the view that he will use this component in many places.

Having the code in a class as a separate component facilitates reuse of the code. – Swaranga Sarma Apr 9 at 8:41 That comment seems to have no connection with 'focusable', which is what I was referring to. (No, your solution is 'correct', but it would be better to tweak it a little.) – Andrew Thompson Apr 9 at 9:37.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions