Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Ron McLeod
paul wheaton
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Himai Minh
Bartenders:
Forum:
Swing / AWT / SWT
about JTable
Jude Fawly
Ranch Hand
Posts: 33
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
how can i set the first cell in the first row in the JTable to be selected given that i am now at the third row?
Teach me to die that so I may<br />Rise glorious at the awful day
Craig Wood
Ranch Hand
Posts: 1535
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class ProgramaticSelection { public ProgramaticSelection() { JTable table = getTable(); table.setRowSelectionAllowed(true); table.setColumnSelectionAllowed(true); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(getUIPanel(table), "North"); f.add(new JScrollPane(table)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private JTable getTable() { String[] headers = { "column 1", "column 2", "column 3", "column 4" }; int rows = 16, cols = 4; Object[][] data = new Object[rows][cols]; for(int row = 0; row < rows; row++) for(int col = 0; col < cols; col++) data[row][col] = "item " + (row * cols + col + 1); return new JTable(data, headers); } private JPanel getUIPanel(final JTable table) { SpinnerNumberModel rowModel = new SpinnerNumberModel(0, 0, table.getRowCount()-1, 1); final JSpinner rowSpinner = new JSpinner(rowModel); SpinnerNumberModel colModel = new SpinnerNumberModel(0, 0, table.getColumnCount()-1, 1); final JSpinner colSpinner = new JSpinner(colModel); ChangeListener l = new ChangeListener() { int row = 0, col = 0; public void stateChanged(ChangeEvent e) { JSpinner spinner = (JSpinner)e.getSource(); int value = ((Integer)spinner.getValue()).intValue(); if(spinner == rowSpinner) row = value; if(spinner == colSpinner) col = value; table.changeSelection(row, col, false, false); } }; rowSpinner.addChangeListener(l); colSpinner.addChangeListener(l); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; addComponents(new JLabel("row index"), rowSpinner, panel, gbc); addComponents(new JLabel("col index"), colSpinner, panel, gbc); return panel; } private void addComponents(JLabel label, JSpinner spinner, JPanel p, GridBagConstraints gbc) { gbc.anchor = gbc.EAST; p.add(label, gbc); gbc.anchor = gbc.WEST; p.add(spinner, gbc); } public static void main(String[] args) { new ProgramaticSelection(); } }
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable
JTable
JTable
JTable(!!!)
JTable
More...