a clickable link on GUI

Hello Experts World,

I would like to make a clickable link on GUI so that when i click that link, another window will pop up.
How do i do this.

Thanks.

Komlaaa
komlaaaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
zzynxConnect With a Mentor Software engineerCommented:
>>How do i declare a map m< keyString, <a list of MyObject> > ?

Just

       Map map = new HashMap();

You don't need to tell beforehand what will be in it. The keys are just Object's and the values too.
So, with the above definition you can use it as said before:

      >> map.put(key, theList); // store the list in the map using the key

Here, you use a String object as key and a (Array)List object as value
0
 
fredwangusCommented:
Is this what u want?


public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        c this =new c();
        thisc.show(true);
       
    }
   
}


public class c extends javax.swing.JFrame {
   
    /** Creates new form c */
    public c() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("jLabel1");
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clicked(evt);
            }
        });

        getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

        pack();
    }

    private void clicked(java.awt.event.MouseEvent evt) {
        d thisd=new d();
        thisd.show();
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new c().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
   
}

public class d extends javax.swing.JFrame {
   
    /** Creates new form d */
    public d() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {                          
       
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        pack();
    }                        
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new d().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify                    
    // End of variables declaration                  
   
}

0
 
NaeemgCommented:
make an instance of ur desired window in the following JLabel click action

    JLabel jLabel = new JLabel();
    jLabel.setText("Click to open new window");

    jLabel.addMouseListener(new java.awt.event.MouseAdapter()
    {
      public void mouseClicked(MouseEvent e)
      {
        JFrame myFrame = new JFrame();
        myFrame.setBounds(100,100,200,200);
      }
    });
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
komlaaaAuthor Commented:
This is not doing it. I basic want to have one word as a clickable link in line of text
Example: this is a long line of text ending by a thisIsALink
This mean when i click the the word 'thisIsALink' i shoud be able to open a new window. (same as a link on a wepage)

Agbeko.
0
 
zzynxSoftware engineerCommented:
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
 
komlaaaAuthor Commented:
The context in which i want this is a little different, So let me reformulate the situtaion in these questions:

-I have a map m< keyString, <a list of MyObject> >
-My Object has two String data fields: data1, data2

-I have a file with the following content:
keyString1   dataString1, dataString1, dataString2, dataString2, dataString1, dataString2
keyString2   dataString1, dataString2, dataString1, dataString2, dataString1, dataString2

-i would like to read the file into map m and have something like:
 MyObject.keyString = keyString1;
 MyObject.data1 = dataString1; MyObject.data2 = dataString2  ......  TILL THE END OF THE FILE

-after all this, i would like to print the content of my map m int the following format:
keyString1
 dataString1, dataString2
 dataString1, dataString2
 dataString1, dataString2

keyString2
 dataString1, dataString2
 dataString1, dataString2
 dataString1, dataString2
============== NOW COMES MY QUESTION ========================
1.) I would like dataString2  to appear on my GUI as a link so that when clicked, it will open a new window.  How?
2.) How do i read this file, store it and print it?

thanks
0
 
zzynxSoftware engineerCommented:
>>-I have a file with the following content:
>>keyString1   dataString1, dataString1, dataString2, dataString2, dataString1, dataString2
>>keyString2   dataString1, dataString2, dataString1, dataString2, dataString1, dataString2
What's between keyString1 and dataString1 ? a tab? Why not a comma also?

>>-after all this, i would like to print the content of my map m int the following format:
On paper? In a Window of your GUI?

I think I showed you in my previous comment how to do 1)
0
 
komlaaaAuthor Commented:
>>I think I showed you in my previous comment how to do 1)
1.) is good, very good example actually.

>>What's between keyString1 and dataString1 ? a tab? Why not a comma also?
Oh sorry, my bad, there should be a comment also

>>On paper? In a Window of your GUI?
on GUI with the link at the end of it.
0
 
komlaaaAuthor Commented:
>>What's between keyString1 and dataString1 ? a tab? Why not a comma also?
Oh sorry, my bad, there is no comma at all

================= CORRECTED FILE FORMAT ===================
keyString1 dataString1 dataString2 dataString1 dataString2 dataString1 dataString2
keyString2 dataString1 dataString2 dataString1 dataString2 dataString1 dataString2



Thanks
0
 
zzynxSoftware engineerCommented:
>> Oh sorry, my bad, there should be a comment also
a commA, you probably mean ;°)

I have no more time today. Have to go offline. I'll have a look at it.

Reading the file will be somehting like this:

try {
        BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String line, key;
        while ((line = in.readLine()) != null) {
            String parts[] = line.split("\\s*,\\s*"); // splits the line of the file into the different parts
            key = parts[0];
            List theList = new ArrayList();
            for (int i=1; i<parts.length; i=i+2) {
                MyObject obj = new Object();
                obj.setData1( parts[i] );
                obj.setData2( parts[i+1] );
                theList.add( obj ); // Store in the list
            }
            map.put(key, theList); // store the list in the map using the key
        }
        in.close();
    } catch (IOException e) {
    }
0
 
zzynxSoftware engineerCommented:
>> Oh sorry, my bad, there is no comma at all

Then in the previous replace

>>     String parts[] = line.split("\\s*,\\s*"); // splits the line of the file into the different parts

by

>>     String parts[] = line.split("\\s*"); // splits the line of the file into the different parts
0
 
zzynxSoftware engineerCommented:
How are things going?
0
 
komlaaaAuthor Commented:
hey zzynx, back? :)
things are going pretty good.

How do i declare a map m< keyString, <a list of MyObject> > ?
0
 
zzynxSoftware engineerCommented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.