Solved

a clickable link on GUI

Posted on 2004-10-27
348 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
Question by:komlaaa
    14 Comments
     
    LVL 2

    Expert Comment

    by:fredwangus
    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
    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
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    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
    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
    >>-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
    >>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
    >>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
    >> 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
    >> 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
    How are things going?
    0
     

    Author Comment

    by:komlaaa
    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:
    >>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
    Thanks
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    dividesSelf challange 15 37
    sumHeights  challenge 17 34
    java continue statement 10 32
    allStar challenge 1 10
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now