Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8486345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:55:15+00:00 2026-06-10T20:55:15+00:00

I have a JTable, and i want to sort rows sometimes by integer (size

  • 0

I have a JTable, and i want to sort rows sometimes by integer (size column), sometimes by String (file path).
So i searched it on google and i’ve arrived here. i’ve known that i’ve to override a method of DefaultTableModel, called getColumnClass. So i link here my code.

class Personal_model extends DefaultTableModel{

 Personal_model(String[][] s,String[] i){
      super(s,i);
 }


 @Override
 public Class<?> getColumnClass(int columnIndex){

      if (columnIndex!=2) 
              return String.class;
      else
              return Integer.class;

 }
}

And here the code to create the table, by the model ‘Personal_model’; i also set rowsorter.
BUT ALL THIS DOESN’T WORK!!!!! help me pls

      modeltable = new Personal_model(data,col);   
      table = new JTable(modeltable);
      table.setRowSorter(new TableRowSorter<Personal_model>(modeltable));

Normally, without my sorter, all is perfeclty visualized, and Strings are sorted correctly (it’s obvious, beacuse normally it’s all sorted by String..)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:55:17+00:00Added an answer on June 10, 2026 at 8:55 pm

    1) please read tutorial about JTable that’s contains TableRowSorter example, issue about RowSorter must be in your code

    2) by default you can to use follows definition for ColumnClass,

    public Class getColumnClass(int c) {
       return getValueAt(0, c).getClass();
    }
    

    3) or you can to hardcode that

        @Override
        public Class<?> getColumnClass(int colNum) {
            switch (colNum) {
                case 0:
                    return Integer.class;
                case 1:
                    return Double.class;
                case 2:
                    return Long.class;
                case 3:
                    return Boolean.class;
                case 4:
                    return String.class;
                case 5:
                    return Icon.class;
                default:
                    return String.class;
            }
        } 
    

    4) or override RowSorter (notice crazy code)

    enter image description here enter image description here

    import com.sun.java.swing.Painter;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics2D;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class JTableSortingIconsForNimbus extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JTable table;
        private JTable table1;
        private static Icon ascendingSortIcon;
        private static Icon descendingSortIcon;
    
        public JTableSortingIconsForNimbus() {
            Object[] columnNames = {"Type", "Company", "Shares", "Price"};
            Object[][] data = {
                {"Buy", "IBM", new Integer(1000), new Double(80.50)},
                {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)},
                {"Sell", "Apple", new Integer(3000), new Double(7.35)},
                {"Buy", "Nortel", new Integer(4000), new Double(20.00)}
            };
            DefaultTableModel model = new DefaultTableModel(data, columnNames) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Class getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
            table = new JTable(model) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                    Component c = super.prepareRenderer(renderer, row, column);
                    int firstRow = 0;
                    int lastRow = table.getRowCount() - 1;
                    if (row == lastRow) {
                        ((JComponent) c).setBackground(Color.red);
                    } else if (row == firstRow) {
                        ((JComponent) c).setBackground(Color.blue);
                    } else {
                        ((JComponent) c).setBackground(table.getBackground());
                    }
                    return c;
                }
            };
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane, BorderLayout.NORTH);
            table1 = new JTable(model) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                    Component c = super.prepareRenderer(renderer, row, column);
                    int firstRow = 0;
                    int lastRow = table1.getRowCount() - 1;
                    if (row == lastRow) {
                        ((JComponent) c).setBackground(Color.red);
                    } else if (row == firstRow) {
                        ((JComponent) c).setBackground(Color.blue);
                    } else {
                        ((JComponent) c).setBackground(table1.getBackground());
                    }
                    return c;
                }
            };
            table1.setPreferredScrollableViewportSize(table1.getPreferredSize());
            JScrollPane scrollPane1 = new JScrollPane(table1);
            //UIDefaults nimbusOverrides = new UIDefaults();
            //nimbusOverrides.put("Table.ascendingSortIcon", ascendingSortIcon);
            //nimbusOverrides.put("Table.descendingSortIcon", descendingSortIcon);
            //table1.putClientProperty("Nimbus.Overrides", nimbusOverrides);
            //UIManager.getLookAndFeelDefaults().put("Table.ascendingSortIcon", ascendingSortIcon);
            //UIManager.getLookAndFeelDefaults().put("Table.descendingSortIcon", descendingSortIcon);
            UIManager.getLookAndFeelDefaults().put("TableHeader[Enabled].ascendingSortIconPainter",
                    new FillPainter1(new Color(255, 255, 191)));
            UIManager.getLookAndFeelDefaults().put("TableHeader[Enabled].descendingSortIconPainter",
                    new FillPainter1(new Color(191, 255, 255)));
    
    
            SwingUtilities.updateComponentTreeUI(table1);
            add(scrollPane1, BorderLayout.SOUTH);
            TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel()) {
    
                @Override
                public void toggleSortOrder(int column) {
                    if (column >= 0 && column < getModelWrapper().getColumnCount() && isSortable(column)) {
                        List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
                        if (!keys.isEmpty()) {
                            SortKey sortKey = keys.get(0);
                            if (sortKey.getColumn() == column && sortKey.getSortOrder() == SortOrder.DESCENDING) {
                                setSortKeys(null);
                                return;
                            }
                        }
                    }
                    super.toggleSortOrder(column);
                }
            };
            table.setRowSorter(sorter);
            table1.setRowSorter(sorter);
        }
    
        static class FillPainter1 implements Painter<JComponent> {
    
            private final Color color;
    
            public FillPainter1(Color c) {
                color = c;
            }
    
            @Override
            public void paint(Graphics2D g, JComponent object, int width, int height) {
                g.setColor(color);
                g.fillRect(0, 0, width - 1, height - 1);
            }
        }
    
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                ascendingSortIcon = UIManager.getLookAndFeelDefaults().getIcon("Table.ascendingSortIcon");
                descendingSortIcon = UIManager.getLookAndFeelDefaults().getIcon("Table.descendingSortIcon");
                UIManager.getLookAndFeelDefaults().put("TableHeader[Enabled].ascendingSortIconPainter",
                        new FillPainter1(new Color(127, 255, 191)));
                UIManager.getLookAndFeelDefaults().put("TableHeader[Enabled].descendingSortIconPainter",
                        new FillPainter1(new Color(191, 255, 127)));
            } catch (Exception fail) {
            }
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JTableSortingIconsForNimbus frame = new JTableSortingIconsForNimbus();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have A jtable. (tablesummary). one of it's column is EXPIRY. i want to
I would like to sort JTable rows based on one hidden column. Say I
I have a JTable consisting of multiple rows and columns. I want to make
I have a JTable with a number of columns. I want a particular column
I have a JTable where I want to highlight some rows using a custom
I have a JTable . I want to know which row and column are
I have a Jtable with a checkbox in first column. I want to strikethrough
I have a JTable which I want to have left-click and right-click JPopupMenu on
I have a JTable that I want to use to display some data (a
Suppose you have a JTable and for each cell you want to display three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.