Results 1 to 7 of 7
  1. #1
    Allgorythm is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Default 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();
         }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Take a look at the Row Number Table for a working example. The key is the ChangeListener.

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I'm not sure how, but you need to link the row numbers and the main table together.
    So you are telling me that my example doesn't work for you?

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    I didn't look at your example. Just the OP's

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    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?

  7. #7
    Allgorythm is offline Member
    Join Date
    Dec 2009
    Posts
    20
    Rep Power
    0

    Talking

    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 >
    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);
    	}
    - 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!
    Last edited by Allgorythm; 02-15-2010 at 05:13 AM.

Similar Threads

  1. drawing Line by dragging mouse !!!
    By h9h in forum Java 2D
    Replies: 14
    Last Post: 10-23-2009, 05:10 AM
  2. Dragging JLabel
    By techbossmb in forum AWT / Swing
    Replies: 2
    Last Post: 09-22-2009, 07:19 PM
  3. Dragging Boxes
    By anilanar in forum New To Java
    Replies: 8
    Last Post: 09-05-2009, 09:23 PM
  4. Limiting the Dragging area
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-30-2008, 03:51 PM
  5. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 12:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •