Re: JTable
GG Andrew Thompson wrote:
On Sep 12, 7:02 pm, mamta81 <roy.ma...@gmail.com> wrote:
Note that a more detailed subject would be better.
For this one, maybe 'JTable many row select'.
My swing application uses a JTable.when i select any row of the
JTable and perform some other work in the same application the row
gets deselected.
Does it, or does the table just lose focus?
An SSCCE might make that more clear.
Yes the OP really ought to post an SSCCE (see http://sscce.org).
Here's my example modified to return focus to the table:
-------------------------------- 8< --------------------------------
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
/**
* @author RedGrittyBrick
*/
public class TableSelection {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TableSelection().createAndShowGUI();
}
});
}
private Object[] headings = { "Alpha", "Bravo", "Charlie" };
private Object[][] data = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
private void createAndShowGUI() {
JPanel p = new JPanel(new BorderLayout());
p.add(new JLabel("Click a row, then click button at bottom"),
BorderLayout.NORTH);
final JTable table = new JTable(data, headings);
p.add(table, BorderLayout.CENTER);
JButton b = new JButton("other work in the same application");
p.add(b, BorderLayout.SOUTH);
final JFrame f = new JFrame("TableSelection");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(f,
"See, other work doesn't affect selection!");
// return focus to table (from button)
SwingUtilities.invokeLater(new Runnable() {
public void run() {
table.requestFocus();
}
});
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
-------------------------------- 8< --------------------------------
--
RGB