[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

a clickable link on GUI

Posted on 2004-10-27
14
Medium Priority
?
388 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:komlaaa
[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
14 Comments
 
LVL 2

Expert Comment

by:fredwangus
ID: 12430280
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
 
LVL 5

Expert Comment

by:Naeemg
ID: 12430589
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
 

Author Comment

by:komlaaa
ID: 12430795
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
Technology Partners: 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!

 
LVL 37

Expert Comment

by:zzynx
ID: 12431030
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
 

Author Comment

by:komlaaa
ID: 12434293
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12434574
>>-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
 

Author Comment

by:komlaaa
ID: 12434962
>>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
 

Author Comment

by:komlaaa
ID: 12435106
>>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
 
LVL 37

Expert Comment

by:zzynx
ID: 12435215
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12435350
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12441902
How are things going?
0
 

Author Comment

by:komlaaa
ID: 12458612
hey zzynx, back? :)
things are going pretty good.

How do i declare a map m< keyString, <a list of MyObject> > ?
0
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 12470547
>>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
 
LVL 37

Expert Comment

by:zzynx
ID: 12490775
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

649 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