Results 1 to 8 of 8
  1. #1
    ppreeti is offline Member
    Join Date
    Mar 2014
    Posts
    17
    Rep Power
    0

    Default link not working in Jtable

    The JTable that I'm using in my program fetches its value from the database. Now one of the fields contain the link item. I basically want it to be clickable so that once the user clicks on this link then he is directed to the desired webpage. I've tried to implement this in my code but somehow nothing seems to work. Where is the error in my code and what could be the possible fix for this ? Thanks !

    Java Code:
     public  class JTableButtonMouseListener extends MouseAdapter
                {
                    private final JTable table;
    
                public JTableButtonMouseListener(JTable table)
                {
                    this.table = table;
                }
    
                public void mouseClicked(MouseEvent e) {
                    counter=0;
    
                    System.out.println("counter value="+counter++);
    
                    int column = table.getColumnModel().getColumnIndexAtX(e.getX());
                    int row    = e.getY()/table.getRowHeight(); 
    
                    if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {
                        Object value = table.getValueAt(row, column);
    
                        System.out.println("object value="+value);
                        System.out.println(".............................................................");
    
                        if(table.getValueAt(row, 4)!=null)
                        {
                            Object ob = table.getValueAt(row, 4);
                            String link_string=ob.toString();
    
    
                            System.out.println(".....................");
                              ((AbstractButton) ob).addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                                            if (Desktop.isDesktopSupported()) {
                                                    Desktop desktop = Desktop.getDesktop();
                                                    try {
                                                         URI uri = new URI("http://www.roseindia.net");
                                                            desktop.browse(uri);
    
    
    
                                                    } catch (Exception ex) {
                                                    }
                                            } else {
                                            }
                                    }
    
                        });
                      }
    
                        if(value==null)
                        {
                            Object v=table.getValueAt(row, 1);
                             s = v.toString();
                            jmenu_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
                                    jmenu_frame.setContentPane(new ListModelExample(s));
    jmenu_frame.setSize(260, 200);
                                  jmenu_frame.setVisible(true);
                            jmenu_frame.setLocationRelativeTo(null);
    
                        }
                        if (value instanceof JButton) {
                            ((JButton)value).doClick();
                        }
                    }
                }
       }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: link not working in Jtable

    You need to add a call to the printStackTrace() method in the catch block so any error is displayed.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ppreeti is offline Member
    Join Date
    Mar 2014
    Posts
    17
    Rep Power
    0

    Default Re: link not working in Jtable

    Actually what I find is that the code never enters the block ((AbstractButton) ob).addActionListener(new ActionListener() { . So what could be the remedy

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: link not working in Jtable

    code never enters the block
    Why not? Is there logic that keeps the code from being executed?

    What is printed by the debug printlns in the code?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,385
    Blog Entries
    7
    Rep Power
    20

    Default Re: link not working in Jtable

    Adding an ActionListener to someting doesn't necessarily activate the ActionListener ... are you sure you want to add (another) ActionListener each time a mouse is clicked on that table element?

    kind regards,

    Jo
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    ppreeti is offline Member
    Join Date
    Mar 2014
    Posts
    17
    Rep Power
    0

    Default Re: link not working in Jtable

    no I'm not sure. I've already mentioned you my requirement. Any suggestions how to do that. ( I want to make the link to be clickable which redirects to the desired page. This value for link is retrieved from the database.) Please help as I'm really puzzled as to how to do this

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: link not working in Jtable

    Any suggestions
    Add some more println() statements to show where the code is executing and what the values of variables are.
    Execute the program, copy what is printed out and paste it here so we can what the code is doing when you execute it.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,385
    Blog Entries
    7
    Rep Power
    20

    Default Re: link not working in Jtable

    Quote Originally Posted by ppreeti View Post
    no I'm not sure. I've already mentioned you my requirement. Any suggestions how to do that. ( I want to make the link to be clickable which redirects to the desired page. This value for link is retrieved from the database.) Please help as I'm really puzzled as to how to do this
    Well, I am sure:defining and installing an ActionListener does not call the method in the ActionListener; you should read the Oracle tutorials for the JTable class, because you're on the wrong track; you should install a TableCellEditor for that link column; if an element in that column is clicked the TableCellEditor can fire up your browser. The tutorial has a nice example where you can edit a colour; study that and throw your current code away.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Link is not working in JSP
    By sv63990 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-08-2012, 11:45 AM
  2. jTable not working
    By sathya11491 in forum NetBeans
    Replies: 0
    Last Post: 03-10-2012, 08:14 AM
  3. Jtable with hyper link
    By AnnSophie in forum Advanced Java
    Replies: 1
    Last Post: 10-14-2010, 02:24 PM
  4. Adding a link to JTable cell
    By LianaN in forum New To Java
    Replies: 3
    Last Post: 09-19-2010, 09:06 PM
  5. Working with Jtable
    By blu_fox88 in forum AWT / Swing
    Replies: 3
    Last Post: 02-20-2010, 04:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •