Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

java - Problems with JTable sorting of integer values

Currently I have a JTable that uses RowSorter, but when I click the header that I want it to sort in, it displays the rows in a weird order

  • 1
  • 10
  • 11
  • ...
  • 2
  • 20
  • 21
  • ...
  • 3
  • 30

Yet when I select a certain row, say row 5, it changes the row that's labeled 5. Any reason as to why this is happening and how I can fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can set the column type for a JTable by setting its model explicitly like in the following example

setModel(new DefaultTableModel(new Object[0][], new String[] {
                "SELECT", "WHERE", "FIELD", "TYPE" }) {
            Class[] types = { Boolean.class, Boolean.class, String.class,
                    String.class };
            boolean[] canEdit = { true, false, false, false };

            @Override
            public Class getColumnClass(int columnIndex) {
                return this.types[columnIndex];
            }

            public boolean isCellEditable(int columnIndex) {
                return this.canEdit[columnIndex];
            }
        });

Give your column classes like this (here column one and two are Boolean and the rest String.

 Class[] types = { Boolean.class, Boolean.class, String.class,String.class };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...