Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trying to create text links

Posted on 2004-04-06
10
Medium Priority
?
228 Views
Last Modified: 2010-03-31
I'm working on a project and have become stuck. I want to display the contents of an arraylist as a series of adjacent, underlined pieces of text that execute some code when you click on them. I initially displayed the contents as a row of JButtons (with border set to null and editable false). They were quite large however, and I wanted the text to be close together. I tried reducing the size of the JButton using setSize, but whenever its size was set beneath a certain value, the text on the button changed into "...", even though there was blatantly more than enough room to fit the text on. Reducing the font size of the text did not help either.

I'm now trying to use a JTextField (with border set to null and editable false) to display the contents. Although it is possible to add an ActionListener to this, I cannot figure out how to catch the events it generates. The only one i'm interested in is when the mouse is clicked on it. I presume it generates an event because it highlights the current contents of the textfield. I realise it is possible to do this by adding a MouseMotionListener and detecting its clicks, but i was wondering if there was a simpler way.

Is there any way to do this or should I try creating hyperlinks with a jeditorpane?

Thanks
0
Comment
Question by:rm21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10770638
Use a JEditorPane, and HyperlinkListener to listener for click.
0
 
LVL 92

Expert Comment

by:objects
ID: 10770642
You would use html and <a> tags for your links.
0
 

Author Comment

by:rm21
ID: 10770644
How would I create a hyperlink that generates events then?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 10770657
In your html use the <a> tag
0
 

Author Comment

by:rm21
ID: 10771298
Have got a content pane running with some text but what would i put between the <a></a> tags to get some code to run? And subsequently what would i tell the hyperlinklistener to listen for? Thanks again
0
 
LVL 92

Expert Comment

by:objects
ID: 10771324
<a href="url">link text</a>

the url will be passed to the hyperlinklistener in the HyperlinkEvent whenever the link is pressed.


0
 

Accepted Solution

by:
RobCSP earned 500 total points
ID: 10772798
Hi, this comment assist the objects comment:
-------------------------------------------------------
When the user clicks on a link in a non-editable JEditorPane, the pane fires a HyperLinkEvent

Responded to by any registered HyperLinkListener objects

public interface HyperLinkListener {
  public void hyperlinkUpdate(HyperlinkEvent e)
}
 

The HyperlinkEvent object passed to this method contains the URL of the event which is returned by its getURL() method:

public URL getURL()


HyperlinkEvents are fired not just when the user clicks the link but also when the mouse enters or exits the link area. Thus you'll want to check the type of the event before changing the page with the getEventType() method:

public HyperlinkEvent.EventType getEventType()

This will return one of the three mnemonic constants

HyperlinkEvent.EventType.EXITED

HyperlinkEvent.EventType.ENTERED

HyperlinkEvent.EventType.ACTIVATED

//Example

import javax.swing.*;
import javax.swing.event.*;

 
public class LinkFollower implements HyperlinkListener {

  private JEditorPane pane;
 
  public LinkFollower(JEditorPane pane) {
    this.pane = pane;
  }

  public void hyperlinkUpdate(HyperlinkEvent evt) {
   
    if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      try {
        pane.setPage(evt.getURL());        
      }
      catch (Exception e) {        
      }
    }
   
  }

/*
 * SimpleWebBrowser.java
 *
 * Created on 7 de abril de 2004, 10:17
 */



import javax.swing.text.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;

public class SimpleWebBrowser {

  public static void main(String[] args) {
       
    // get the first URL
    String initialPage = "http://www.elmundo.es/traductor/";
    if (args.length > 0) initialPage = args[0];      
   
    // set up the editor pane
    JEditorPane jep = new JEditorPane();
    jep.setEditable(false);  
    jep.addHyperlinkListener(new LinkFollower(jep));
   
    try {
      jep.setPage(initialPage);      
    }
    catch (IOException e) {
      System.err.println("Usage: java SimpleWebBrowser url");
      System.err.println(e);
      System.exit(-1);
    }
     
    // set up the window
    JScrollPane scrollPane = new JScrollPane(jep);    
    JFrame f = new JFrame("Simple Web Browser");
    f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    f.getContentPane().add(scrollPane);
    f.setSize(512, 342);
    f.show();
   
  }

}
0
 

Author Comment

by:rm21
ID: 10774013
Thanks guys for the help
0
 

Expert Comment

by:RobCSP
ID: 10774904
thnx ;P
0
 
LVL 92

Expert Comment

by:objects
ID: 10778782
:(
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question