A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
Cursor positon within JTable
Saket Barve
Ranch Hand
Joined: Dec 19, 2002
Posts: 224
posted
Jan 20, 2007 11:20:00
0
There's a scenario where I explicitly add rows to a table and am then required to set the cursor in the first column of this newly added row.
Once the row is set into the table model, I am making use of the following code to do the needful:
table.changeSelection( row, 0, false, false ); table.setCursor( new Cursor(Cursor.DEFAULT_CURSOR) ); table.requestFocusInWindow();
However, this code block merely makes the newly added row
selected
without any trace of the cursor. How may I tackle this?
Thanks,
Saket
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
Jan 20, 2007 23:55:00
0
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; class Testing { public void buildGUI() { final JTable table = new JTable(5,5); JScrollPane sp = new JScrollPane(table); JButton btn = new JButton("Add New Row"); JFrame f = new JFrame(); f.getContentPane().add(sp,BorderLayout.CENTER); f.getContentPane().add(btn,BorderLayout.SOUTH); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ ((DefaultTableModel)table.getModel()).addRow(new String[]{"","","","",""}); table.editCellAt(table.getRowCount()-1,0); Component editor = table.getEditorComponent(); editor.requestFocusInWindow(); } }); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ new Testing().buildGUI(); } }); } }
I agree. Here's the link:
http://aspose.com/file-tools
subject: Cursor positon within JTable
Similar Threads
How to focus a particular cell in JTable
Select vs Edit in JTable
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
How to find the size of my resultSet
multiple inserts with multiple connections into same db table.
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter