[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1387
  • Last Modified:

a clickable text

One way to add a clickable link to GUI and make that link open a new window when clicked
is the one below advised by an EE Expert: He has used Jabel to do this.
My question: isn't there any other way to do this without using labels?
The text i want to make clickable is a string returned from Class A to the CUI class B
(that helds the link), ... So label does not work in this case.
======================CODE===========================
Does this help?

/*
 * LinkDemo.java
 *
 */

import java.awt.*;
import java.io.*;
import javax.swing.*;
/**
 *
 * @author  zzynx
 */
public class LinkDemo extends javax.swing.JFrame {

    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private Cursor prevCursor;
   
    public LinkDemo() {
        initComponents();
    }
   
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        getContentPane().setLayout(new java.awt.FlowLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jLabel1.setText("this is a long line of text ending by a");
        getContentPane().add(jLabel1);

        jLabel2.setText("<HTML><U>thisIsALink</U></HTML>");
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel2MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel2MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel2MouseExited(evt);
            }
        });

        getContentPane().add(jLabel2);

        pack();
    }

    private void jLabel2MouseExited(java.awt.event.MouseEvent evt) {
        setCursor(prevCursor);
    }

    private void jLabel2MouseEntered(java.awt.event.MouseEvent evt) {
        prevCursor = getCursor();
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {
        JOptionPane.showMessageDialog(this, "Hey, you clicked me! ;°)");
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        LinkDemo frame = new LinkDemo();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.show();
    }
}
0
komlaaa
Asked:
komlaaa
  • 4
  • 3
  • 3
2 Solutions
 
objectsCommented:
Use a JEditorPane.
0
 
zzynxSoftware engineerCommented:
>> The text i want to make clickable is a string returned from Class A to the CUI class B
>> (that helds the link), ... So label does not work in this case.
Could you elaborate on "So label does not work in this case". Don't understand that one.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
komlaaaAuthor Commented:
in piece of code below, when item is selected,
1.)a string retuned by CDs.getCDList(selectedGenre) eg.==> "cdTitle1|artist1|cdTitle2|artist2...." is splited by "|" into array.
2.) create two  n (EVEN) labels for artists and n(EVEN) labelsfor titles in the string. But the title is supposed to be a link that should respond to a mouseclicked event and pop up a message dialog with some message.
3.) put all those labels in Container c in format:
cdTitle1 artist1
cdTitle2 artist2
..........
Note: titles appear as clickable links on the gui.

My Problem: Nothing is being displayed.
Any suggestion?

=========================CODE============================
public class DisplayCdCollections extends JFrame
{
 
    private String selectedGenre;
    private JComboBox categories;
    private final String genres[] ;
    private Container c;
    private CDCollection CDs;                      
    private JLabel titleLabel;
    private JLabel artistLabel;

   
    public DisplayCdCollections()
       {  
            super("CDs Display");
           
            CDs = new CDCollection();
            CDs.read();
            genres = CDs.getCdCategoies();
                                   
            this.setLocation(300,300 );
            this.addWindowListener
            (
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        setVisible(false);
                        dispose();
                        System.exit(0);
                    }
                }
            );
            //create a contianer with scrollbar where the CdList should go.
            c = getContentPane();
            c.setLayout(new BorderLayout());
           // c.add(new JScrollPane(CdList), BorderLayout.CENTER);
           
            //creating combobox to hold the type of CD
            categories = new JComboBox(genres);
            categories.setBackground(Color.white);
            categories.setMaximumRowCount(12);  
            categories.addItemListener
            (
                new ItemListener()
                {
           
                    public void itemStateChanged(ItemEvent e)
                    {
                     selectedGenre= (String)categories.getSelectedItem();
                        {
                            if( selectedGenre.equals("All") )
                            {
                               String fields[] =  CDs.getCDList(selectedGenre).split(" -- ");
                              for(int i = 0; i < fields.length; i+=1)
                              {
                                  String aTtitle = fields[i];
                                titleLabel = new JLabel("<HTML><U> aTtitle </U></HTML>" );
                                  artistLabel = new JLabel( fields[i+1] );
                                 c.add(artistLabel);
                                  c.add(titleLabel);
                              }
                     
                            }
                               
                        }
                    }
                }
            );

        //creat a panel for the Cd categories combobox
        JPanel eastPanel = new JPanel();
        eastPanel.add(categories);
        c.add(eastPanel, BorderLayout.EAST);
       
        pack();
        setSize(250,100);
        show();
    }
}
0
 
objectsCommented:
>        c.setLayout(new BorderLayout());

change that to use a GridLayout

        c.setLayout(new GridLayout(0, 2));

>                                 c.add(artistLabel);
>                                  c.add(titleLabel);

After adding the labels add the following call:

c.validate();
0
 
komlaaaAuthor Commented:
>c.setLayout(new BorderLayout());

> c.setLayout(new GridLayout(0, 2));

What's the difference? i prefer u tell me than trying figure that out in book

What about  c.validate(); ==> what does do? is that different from show( )
0
 
objectsCommented:
> What's the difference? i prefer u tell me than trying figure that out in book

They layout your components differently. BorderLayout lays its components in 5 regions, you are adding all components to the same region so only the last will be displayed.
A GridLayout lays out any numbers of components in a grid

> What about  c.validate(); ==> what does do? is that different from show( )

yes its different from show(), it tells the container to relayout its components (because you have added new children)

0
 
zzynxSoftware engineerCommented:
>> change that to use a GridLayout
komlaaa, that could work.
But from a previous Q, I remember that using the BorderLayout was a solution to have your textarea + scrollbars work OK.
Moreover, isn't your goal to add those "Artist - Title" pairs in the text area? Or am I wrong?

I remember you liked the combo to be at the right and the JTextArea at the left.
Now, where exactly do you want the "Artist - Title" pairs to appear?

0
 
komlaaaAuthor Commented:
>Or am I wrong?
you r never wrong :) ... well i am not too sure about NEVER but not in this case... -- I don't sometime make myself clear enough.
>Now, where exactly do you want the "Artist - Title" pairs to appear?
same as before except tha is:  "Artist - Title" ===> left and combo ====> right
But i don't have to put this in textArea. My actual goal is to put "Artist"  on one label and "Title" on another label BUT in
format
"Artist1 - Title1"
"Artist2 - Title2"
"Artist3 - Title3"
"Artist4 - Title4"
And then make "Title" as clickable link so that when i click a "title" of a given another information window can pop up with some info. about the artist.
0
 
zzynxSoftware engineerCommented:
>> My actual goal is to put "Artist"  on one label and "Title" on another label BUT in format
>>"Artist1 - Title1"
>>"Artist2 - Title2"
>>"Artist3 - Title3"
>>"Artist4 - Title4"
>>And then make "Title" as clickable link

Well, I don't see why you said:
>> So label does not work in this case

>>"Artist1 - Title1"
>>"Artist2 - Title2"
>>"Artist3 - Title3"
>>"Artist4 - Title4"
are just pairs of JLabel's:
the first containing "Artist1 -" as text,
the second containing "Title1" and being clickable as in the original code I gave you.
Don't see the problem...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now