Java 试图使链接在JTable中可单击时,未处理的异常类型为URISyntaxException

Java 试图使链接在JTable中可单击时,未处理的异常类型为URISyntaxException,java,swing,compiler-errors,Java,Swing,Compiler Errors,其目的是使链接在jtable中可单击,以便当用户单击链接时,所需页面在浏览器中打开。从数据库中获取的项目之一是link,我的尝试是使其处于活动状态并可单击。我得到的错误是 Unhandled exception type URISyntaxException Cannot refer to a non-final variable uri inside an inner class defined in a different method 对于“我的代码”中的行: final URI

其目的是使链接在jtable中可单击,以便当用户单击链接时,所需页面在浏览器中打开。从数据库中获取的项目之一是link,我的尝试是使其处于活动状态并可单击。我得到的错误是

Unhandled exception type URISyntaxException 
Cannot refer to a non-final variable uri inside an inner class defined in a different method 
对于“我的代码”中的行:

final URI uri = new URI("http://www.roseindia.net");
即使我把它放在try-catch块中,错误似乎也无法解决。相反,在try-catch块中,我得到的错误如下

Unhandled exception type URISyntaxException 
Cannot refer to a non-final variable uri inside an inner class defined in a different method 
那么,可能的解决方案和修复方案是什么呢

 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();
                }
            }
        }

    }
public类JTableButtonMouseListener扩展了MouseAdapter
{
专用最终JTable表;
公共JTableButtonMouseListener(JTable表)
{
this.table=表格;
}
公共无效mouseClicked(MouseEvent e){
计数器=0;
//System.out.println(“****************************************************************************************************************”);
System.out.println(“计数器值=“+counter++”);
//System.out.println(////////////////////////;
int column=table.getColumnModel().getColumnIndexAtX(e.getX());
int row=e.getY()/table.getrowhight();
if(行=0&&column=0){
对象值=table.getValueAt(行、列);
//System.out.println(“单击行=”+行);
//System.out.println(“单击的列=”+列);
System.out.println(“对象值=”+值);
系统输出打印项次(“…………”);
/*public void getsecname(字符串s)
{
字符串ss=s;
}*/
if(table.getValueAt(第4行)!=null)
{
对象ob=table.getValueAt(第4行);
String link_String=ob.toString();
//最终URI=null;
//URI;
试一试{
最终URI=新URI(“http://www.roseindia.net");
} 
捕获(URISyntaxException e1)
{
e1.printStackTrace();
}
系统输出打印项次(……);
((AbstractButton)ob.addActionListener(新ActionListener()){
已执行的公共无效操作(操作事件e){
if(Desktop.isDesktopSupported()){
Desktop=Desktop.getDesktop();
试一试{
桌面浏览(uri);
//button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_Cursor));
//setCursor(新光标(Cursor.HAND_Cursor));
}捕获(例外情况除外){
}
}否则{
}
}
});
}
//String link_String=ob.toString();
//ob.可点击设置(真);
如果(值==null)
{
对象v=table.getValueAt(第1行);
//System.out.println(“--------------------------------------------------”);
s=v.toString();
jmenu_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jmenu_frame.setContentPane(新的ListModelExample);
jmenu帧设置大小(260200);
jmenu_frame.setVisible(真);
jmenu_frame.setLocationRelativeTo(空);
//到此为止
}
if(JButton的值实例){
((JButton)值).doClick();
}
}
}
}

告诉您需要一个
try catch
块来处理
URISyntaxException

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

要解决无法将
uri解析为变量的问题,可以在声明
uri
的方法中添加
抛出URISyntaxException
,而不是使用
try catch
。但我认为这不是一个好的做法。也许它在您的情况下有效。

您不能在内部类中使用非final变量


是的,我知道,但是在这样做的时候,我得到了上面提到的第二个错误。可能的解决方法是什么?要解决
无法引用在不同方法中定义的内部类中的非最终变量uri的问题
,您需要将变量声明为
最终
,您确实这样做了,它现在不应该显示该错误。我按照u的建议做了,现在它给出了错误,因为uri无法解析为line desktop.browse(uri)中的变量;在我的代码中,这是因为如果尝试失败,该变量将无法在代码的其余部分访问。您在哪个内部类中使用
uri
?我现在已经在类中包含了完整的代码。请看一看,并让我知道错误
final-URI=new-URI(“http://www.roseindia.net");应该抛出一个
crapyCodeSiteException
。。