gpt4 book ai didi

java - 尝试使 JTable 中的链接可单击时出现未处理的异常类型 URISyntaxException

转载 作者:行者123 更新时间:2023-12-02 05:47:54 26 4
gpt4 key购买 nike

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

Unhandled exception type URISyntaxException 

对于我的代码中的行:

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

即使我把它放在 try catch block 中,错误似乎也没有解决。相反,在 try-catch block 中,我得到的错误为

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

}

最佳答案

您不能在内部类中使用非最终变量。 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();
}
}

关于java - 尝试使 JTable 中的链接可单击时出现未处理的异常类型 URISyntaxException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23870110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com