Results 1 to 7 of 7
- 02-14-2010, 04:44 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
dragging the row header autoscroll
hi Im stuck up with this one, i dont know how to solve this problem, when I tried to drag the mouse in the main table it will select all the rows that my mouse is pointing at(dragging at) it also scrolls automatically when dragging, but when i tried to drag the row headers, the selection seems awkward, the selected rows in the table doesnt go with the row headers. Please try to run this code an drag the row headers not the row in the table. I need help pleas ..
Java Code:public class TableSample extends JFrame { private JTable mainTable; private JScrollPane scroll ; private JViewport jv; private JTable rowHeaderTableColumn; private JPanel panel; private TableModel tableModel; private DefaultTableModel mainTableModel; private Integer[] rowHeaderData; public TableSample() { initComponents(); } private void initComponents() { panel = new JPanel(); tableModel = new AbstractTableModel() { String[] header = {"Rows"}; public int getColumnCount() { return 1; } public int getRowCount() { return mainTable.getRowCount(); } public String getColumnName(int col) { // this will set the row headers column name return header[0]; } public Object getValueAt(int row, int col) { // this will set the row headers row data // instead of this; // "return rowHeaderData[col] + row;" // this one will generate its own count for each row' cell count return 1 + row; } }; TableColumnModel rowHeaderModel = new DefaultTableColumnModel() { boolean first = true; public void addColumn(TableColumn tableColumn) { if (first) { tableColumn.setMaxWidth(tableColumn.getPreferredWidth()); super.addColumn(tableColumn); tableColumn.setMaxWidth(100); first = false; } } }; String[] columns = {"Item Code", "Item Desciption", "Item Quantity", "Item Cost", "Cost Extension", "Retail", "Retail Extension", "Expiration Code"}; Object[][] rows = new Object[200][columns.length]; mainTableModel = new DefaultTableModel(rows, columns); mainTable = new JTable(mainTableModel); rowHeaderTableColumn = new JTable(tableModel, rowHeaderModel); rowHeaderTableColumn.createDefaultColumnsFromModel(); rowHeaderTableColumn.setBackground(Color.lightGray); rowHeaderTableColumn.setColumnSelectionAllowed(false); rowHeaderTableColumn.setCellSelectionEnabled(false); jv = new JViewport(); jv.setView(rowHeaderTableColumn); jv.setPreferredSize(rowHeaderTableColumn.getMaximumSize()); mainTable.setSelectionModel(rowHeaderTableColumn.getSelectionModel()); mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); panel.setLayout(new GridLayout()); rowHeaderData = new Integer[mainTable.getRowCount()]; for (int count = 0; count <= mainTable.getRowCount() - 1; count++) { rowHeaderData[count] = count + 1; } scroll = new JScrollPane(mainTable); scroll.setRowHeader(jv); scroll.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, rowHeaderTableColumn.getTableHeader()); panel.add(scroll); getContentPane().add(panel); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500, 250); setLocationRelativeTo(null); setVisible(true); } public static void main(String args[]) { new TableSample(); } }
- 02-14-2010, 05:21 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Take a look at the Row Number Table for a working example. The key is the ChangeListener.
- 02-14-2010, 11:57 PM #3
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
I think it has to do with the fact that when you click a row number and drag. It only scrolls the row numbers because they are separate from the other columns. Notice how when you scroll the row numbers down, the scrollpane's scrollbar doesn't move. I'm not sure how, but you need to link the row numbers and the main table together.
- 02-15-2010, 12:03 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
So you are telling me that my example doesn't work for you?I'm not sure how, but you need to link the row numbers and the main table together.
- 02-15-2010, 12:07 AM #5
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
I didn't look at your example. Just the OP's
- 02-15-2010, 12:18 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
So why did you post a response saying you don't know what the solution is, when a solution was given hours earlier? I don't understand the point of that?
- 02-15-2010, 05:10 AM #7
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
thanks collin for telling me that I should Link the table and the rowHeader table
and thanks camickr for showing me a simple code (on the link you gave me) with a VERY Clear comments i found there how to sync the table in my rowHeader table. with a simple code like this >- the view port. now my rowHeader syncs with my main table i dont even need to change all my table's model. how can I give a thanks to you guys? just reputation? thanks thanks!Java Code:public void stateChanged(ChangeEvent e) { // Keep the scrolling of the row table in sync with main table JViewport viewport = (JViewport) e.getSource(); JScrollPane scrollPane = (JScrollPane)viewport.getParent(); scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y); }Last edited by Allgorythm; 02-15-2010 at 05:13 AM.
Similar Threads
-
drawing Line by dragging mouse !!!
By h9h in forum Java 2DReplies: 14Last Post: 10-23-2009, 05:10 AM -
Dragging JLabel
By techbossmb in forum AWT / SwingReplies: 2Last Post: 09-22-2009, 07:19 PM -
Dragging Boxes
By anilanar in forum New To JavaReplies: 8Last Post: 09-05-2009, 09:23 PM -
Limiting the Dragging area
By Preethi in forum New To JavaReplies: 1Last Post: 07-30-2008, 03:51 PM -
Scaling-ache and mouse dragging
By willemjav in forum Java AppletsReplies: 19Last Post: 07-19-2008, 12:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks