komlaaa
asked on
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
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
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(ne w java.awt.event.MouseAdapte r()
{
public void mouseClicked(MouseEvent e)
{
JFrame myFrame = new JFrame();
myFrame.setBounds(100,100, 200,200);
}
});
JLabel jLabel = new JLabel();
jLabel.setText("Click to open new window");
jLabel.addMouseListener(ne
{
public void mouseClicked(MouseEvent e)
{
JFrame myFrame = new JFrame();
myFrame.setBounds(100,100,
}
});
ASKER
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.
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.
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.WindowAdapt er() {
public void windowClosing(java.awt.eve nt.WindowE vent evt) {
exitForm(evt);
}
});
jLabel1.setText("this is a long line of text ending by a");
getContentPane().add(jLabe l1);
jLabel2.setText("<HTML><U> thisIsALin k</U></HTM L>");
jLabel2.addMouseListener(n ew java.awt.event.MouseAdapte r() {
public void mouseClicked(java.awt.even t.MouseEve nt evt) {
jLabel2MouseClicked(evt);
}
public void mouseEntered(java.awt.even t.MouseEve nt evt) {
jLabel2MouseEntered(evt);
}
public void mouseExited(java.awt.event .MouseEven t evt) {
jLabel2MouseExited(evt);
}
});
getContentPane().add(jLabe l2);
pack();
}
private void jLabel2MouseExited(java.aw t.event.Mo useEvent evt) {
setCursor(prevCursor);
}
private void jLabel2MouseEntered(java.a wt.event.M ouseEvent evt) {
prevCursor = getCursor();
setCursor(Cursor.getPredef inedCursor (Cursor.HA ND_CURSOR) );
}
private void jLabel2MouseClicked(java.a wt.event.M ouseEvent evt) {
JOptionPane.showMessageDia log(this, "Hey, you clicked me! ;°)");
}
private void exitForm(java.awt.event.Wi ndowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
LinkDemo frame = new LinkDemo();
frame.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeT o(null);
frame.show();
}
}
/*
* 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
addWindowListener(new java.awt.event.WindowAdapt
public void windowClosing(java.awt.eve
exitForm(evt);
}
});
jLabel1.setText("this is a long line of text ending by a");
getContentPane().add(jLabe
jLabel2.setText("<HTML><U>
jLabel2.addMouseListener(n
public void mouseClicked(java.awt.even
jLabel2MouseClicked(evt);
}
public void mouseEntered(java.awt.even
jLabel2MouseEntered(evt);
}
public void mouseExited(java.awt.event
jLabel2MouseExited(evt);
}
});
getContentPane().add(jLabe
pack();
}
private void jLabel2MouseExited(java.aw
setCursor(prevCursor);
}
private void jLabel2MouseEntered(java.a
prevCursor = getCursor();
setCursor(Cursor.getPredef
}
private void jLabel2MouseClicked(java.a
JOptionPane.showMessageDia
}
private void exitForm(java.awt.event.Wi
System.exit(0);
}
public static void main(String args[]) {
LinkDemo frame = new LinkDemo();
frame.setDefaultCloseOpera
frame.setSize(400,400);
frame.setLocationRelativeT
frame.show();
}
}
ASKER
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
-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
>>-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)
>>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)
ASKER
>>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.
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.
ASKER
>>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
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
>> 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) {
}
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) {
}
>> 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
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
How are things going?
ASKER
hey zzynx, back? :)
things are going pretty good.
How do i declare a map m< keyString, <a list of MyObject> > ?
things are going pretty good.
How do i declare a map m< keyString, <a list of MyObject> > ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks
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(j
jLabel1.setText("jLabel1")
jLabel1.addMouseListener(n
public void mouseClicked(java.awt.even
clicked(evt);
}
});
getContentPane().add(jLabe
pack();
}
private void clicked(java.awt.event.Mou
d thisd=new d();
thisd.show();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invoke
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(j
pack();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invoke
public void run() {
new d().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}