0

The aim is to make the link clickable in jtable so that when user clicks on the link the desired page gets opened in the browser. One of the items fetched from database is link and my attempt is to make it active and clickable. I get the error as

Unhandled exception type URISyntaxException 

For the line in my code:

final URI uri = new URI("http://www.roseindia.net");

And even if i put it in try catch block, the error doesn't seem to resolve. Rather on surrounding in a try-catch block, I get the error as

Cannot refer to a non-final variable uri inside an inner class defined in a different method 

So what could be the possible solution and fix?

 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("***************************************************************");
            System.out.println("counter value="+counter++);
            //System.out.println("/////////////////////////////////////////////////////////////////////");
            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("row clicked="+row);
                //System.out.println("column clicked="+column);
                System.out.println("object value="+value);
                System.out.println(".............................................................");
              /* public void getsecname(String s)
                {
                    String ss=s;
                }*/
                if(table.getValueAt(row, 4)!=null)
                {
                    Object ob = table.getValueAt(row, 4);
                    String link_string=ob.toString();
                    // final URI uri = null;
                    // URI uri;
                     try{
                            final URI uri = new URI("http://www.roseindia.net");
                        } 
                     catch (URISyntaxException e1)
                     {
                            e1.printStackTrace();
                        }


                    System.out.println(".....................");
                      ((AbstractButton) ob).addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                    if (Desktop.isDesktopSupported()) {
                                            Desktop desktop = Desktop.getDesktop();
                                            try {
                                                    desktop.browse(uri);
                                                  //  button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

                                                   // desktop.setCursor(new Cursor(Cursor.HAND_CURSOR));
                                            } catch (Exception ex) {
                                            }
                                    } else {
                                    }
                            }


                    });


                }

              //  String link_string=ob.toString();
            //ob.setClickable(true);

                if(value==null)
                {
                    Object v=table.getValueAt(row, 1);
                    //System.out.println("--------------------------------------------");

                     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);
                     //it ends here
                }
                if (value instanceof JButton) {
                    ((JButton)value).doClick();
                }
            }
        }

    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3608233
  • 103
  • 2
  • 13

2 Answers2

1

What is telling you is that you need a try catch block to handle a URISyntaxException:

final URI;
try{
    uri = new URI("http://www.roseindia.net");
} catch (URISyntaxException e) {
    e.printStackTrace();
}

To solve uri cannot be resolved to a variable You could instead of using try catch, add a throws URISyntaxException to the method in which uri is declared. But I do not think that is a good practice. Maybe it works in your case.

Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
  • yes i know but on doing so i get the second error as mentioned above. What could be the possible fix ? – user3608233 May 26 '14 at 12:27
  • To solve `Cannot refer to a non-final variable uri inside an inner class defined in a different method` you need to declare your variable as `final`, and you did, it should not show that error now. – Alejandro Alcalde May 26 '14 at 12:28
  • I did what u suggested and now it gives the error as uri cannot be resolved to a variable at line desktop.browse(uri); in my code – user3608233 May 26 '14 at 12:32
  • That is because if the try fails, the variable would not bee accesible in the rest of your code. In what inner class are you using `uri`? – Alejandro Alcalde May 26 '14 at 12:33
  • I've now included my full code with class. Kindly have a look and let me know the error – user3608233 May 26 '14 at 12:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54446/discussion-between-user3608233-and-algui91). – user3608233 May 26 '14 at 12:41
1

You can not use non-final variable inside your inner class. Discussion.

if(table.getValueAt(row, 4)!=null)
    {
        Object ob = table.getValueAt(row, 4);
        String link_string=ob.toString();

        try {
            final URI uri = new URI("http://www.roseindia.net");
            System.out.println(".....................");

            ((AbstractButton) ob).addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (Desktop.isDesktopSupported()) {
                        Desktop desktop = Desktop.getDesktop();
                        try {
                            desktop.browse(uri);
                            //button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                            // desktop.setCursor(new Cursor(Cursor.HAND_CURSOR));
                        } catch (Exception ex) {
                        }
                    }
                }
            });
        } catch (URISyntaxException e1) {
           e1.printStackTrace();
        }
    }
Community
  • 1
  • 1
Vishrant
  • 15,456
  • 11
  • 71
  • 120