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://ift.tt/T5HOWP");
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();
}
}
}
}
No comments:
Post a Comment