Results 1 to 5 of 5

Thread: JTable Sort

  1. #1
    theChameleon is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default JTable Sort

    hi, i made the JTable sort by a field Category, followed by the Filters within each Category.

    i notice that clicking on the tab would mess up the original sorting.

    how do i disable the tab so that the user cannot click on each column header to sort it?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    If that doesn't help, you'll have to provide an SSCCE that shows us what you're doing.

  3. #3
    theChameleon is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Java Code:
    //table
    DefaultTableModel model = new DefaultTableModel();
    JTable news_table = new JTable(){
        public Component prepareRenderer(TableCellRenderer renderer,
            int rowIndex, int vColIndex){
            Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
            if(vColIndex == 0 && !isCellSelected(rowIndex, vColIndex)){
                Data temp = (Data) news_table.getValueAt(rowIndex, 2);
                if(!temp.getIsRead()){
                    c.setBackground(Color.pink);
                }
                else{
                    c.setBackground(Color.WHITE);
                }
            }
            return c;
        }
    }
    
    ...
    
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    		model.addColumn("Webpage");
    		model.addColumn("Filter");
    		model.addColumn("Data");
    		table.getTableHeader().setReorderingAllowed(false);
    
    		//sort column 0 and 1, does not work well after clicking on sort
    		TableRowSorter <TableModel> sorter = new TableRowSorter <TableModel> (table.getModel());
    		table.setRowSorter(sorter);
    		List <RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
    		sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
    		sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
    		sorter.setSortKeys(sortKeys);
    
    		TableColumn col0 = table.getColumnModel().getColumn(0);
    		col0.setResizable(false);
    
    		TableColumn col1 = table.getColumnModel().getColumn(1);
    		col1.setResizable(false);
    
    		TableColumn col2 = table.getColumnModel().getColumn(2);
    		col2.setResizable(false);
    like i said earlier, i DONT want the user to be able to click on the column headers to sort them.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    Did you check the API for useful functions?

    DefaultRowSorter (Java Platform SE 6)

  5. #5
    theChameleon is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

Similar Threads

  1. how to sort jtable with row groups
    By mordjah in forum AWT / Swing
    Replies: 0
    Last Post: 10-25-2010, 06:23 PM
  2. Sort jcombo in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 12-11-2009, 06:23 PM
  3. Sort and filter in Jtable (java ver 1.5 )
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-16-2009, 01:03 PM
  4. How to sort column in JTable
    By johnt in forum AWT / Swing
    Replies: 3
    Last Post: 06-14-2008, 06:48 AM
  5. sort columns in jtable
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-14-2007, 06:18 PM

Posting Permissions

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