Re: JTable
GG mamta81 wrote:
Hi all,
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.
O RLY?
------------------------------- 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;
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);
p.add(new JTable(data, headings), 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!");
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
------------------------------- 8< ------------------------
How can i keep the row selected
By not doing whatever it is you are doing that you shouldn't be doing.
We could help if you pasted a SSCCE (see explanation at http://sscce.org).
--
RGB