Un esempi banale è questo:
codice:
public class Test extends JFrame implements ActionListener
{
JTable tabella;
public Test()
{
String[] prova = new String[]
{ "Prova" };
String data[][] = new String[10][1];
for (int i = 0; i < 10; i++)
{
data[i] = prova;
}
tabella = new JTable(data, new String[]
{ "Colonna" });
JPanel panel = new JPanel();
JScrollPane pane = new JScrollPane(tabella);
panel.add(pane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Prova");
button.addActionListener(this);
panel.add(button);
this.add(panel);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0)
{
for (int i = 0; i < tabella.getRowCount(); i++)
{
for (int j = 0; j < tabella.getColumnCount(); j++)
{
if (tabella.isCellSelected(i, j) == true)
System.out.println(i + ", " + j);
}
}
}
public static void main(String[] args)
{
new Test();
}
}