jTable sorts everything as String












0















I download data from MySQL (JDBC) database and display it in jTable:



PreparedStatement pstmt = (PreparedStatement) connection.prepareStatement(query); 
pstmt.setInt(1,idUser);
result = pstmt.executeQuery();

jTable.setModel(DbUtils.resultSetToTableModel(result));


I use Netbeans, on the jTable in properties I set autoCreateRowSorter option to true. Sorting works, however, some of the columns should be sorted as int value, but e.g. user ID is sorted as String. I don't know how to initialize a table in a way that allows me to choose a column type. Then sorting should work properly. Probably it can be done in this line of code (below) that's why I'm asking for advice from someone more experienced.



jTable.setModel(DbUtils.resultSetToTableModel(result)); 


I can also add how the columns in my table look like:



UserID(int) / name(String) / surname(String) / phone number(int) / 
dateTime(e.g. 2018-11-03 19:02:45 - may remain in the programme as String)









share|improve this question




















  • 1





    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.

    – Joe C
    Nov 24 '18 at 16:31
















0















I download data from MySQL (JDBC) database and display it in jTable:



PreparedStatement pstmt = (PreparedStatement) connection.prepareStatement(query); 
pstmt.setInt(1,idUser);
result = pstmt.executeQuery();

jTable.setModel(DbUtils.resultSetToTableModel(result));


I use Netbeans, on the jTable in properties I set autoCreateRowSorter option to true. Sorting works, however, some of the columns should be sorted as int value, but e.g. user ID is sorted as String. I don't know how to initialize a table in a way that allows me to choose a column type. Then sorting should work properly. Probably it can be done in this line of code (below) that's why I'm asking for advice from someone more experienced.



jTable.setModel(DbUtils.resultSetToTableModel(result)); 


I can also add how the columns in my table look like:



UserID(int) / name(String) / surname(String) / phone number(int) / 
dateTime(e.g. 2018-11-03 19:02:45 - may remain in the programme as String)









share|improve this question




















  • 1





    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.

    – Joe C
    Nov 24 '18 at 16:31














0












0








0








I download data from MySQL (JDBC) database and display it in jTable:



PreparedStatement pstmt = (PreparedStatement) connection.prepareStatement(query); 
pstmt.setInt(1,idUser);
result = pstmt.executeQuery();

jTable.setModel(DbUtils.resultSetToTableModel(result));


I use Netbeans, on the jTable in properties I set autoCreateRowSorter option to true. Sorting works, however, some of the columns should be sorted as int value, but e.g. user ID is sorted as String. I don't know how to initialize a table in a way that allows me to choose a column type. Then sorting should work properly. Probably it can be done in this line of code (below) that's why I'm asking for advice from someone more experienced.



jTable.setModel(DbUtils.resultSetToTableModel(result)); 


I can also add how the columns in my table look like:



UserID(int) / name(String) / surname(String) / phone number(int) / 
dateTime(e.g. 2018-11-03 19:02:45 - may remain in the programme as String)









share|improve this question
















I download data from MySQL (JDBC) database and display it in jTable:



PreparedStatement pstmt = (PreparedStatement) connection.prepareStatement(query); 
pstmt.setInt(1,idUser);
result = pstmt.executeQuery();

jTable.setModel(DbUtils.resultSetToTableModel(result));


I use Netbeans, on the jTable in properties I set autoCreateRowSorter option to true. Sorting works, however, some of the columns should be sorted as int value, but e.g. user ID is sorted as String. I don't know how to initialize a table in a way that allows me to choose a column type. Then sorting should work properly. Probably it can be done in this line of code (below) that's why I'm asking for advice from someone more experienced.



jTable.setModel(DbUtils.resultSetToTableModel(result)); 


I can also add how the columns in my table look like:



UserID(int) / name(String) / surname(String) / phone number(int) / 
dateTime(e.g. 2018-11-03 19:02:45 - may remain in the programme as String)






java mysql swing jtable columnsorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 16:36









camickr

276k16127239




276k16127239










asked Nov 24 '18 at 16:27









Kamil KrólikowskiKamil Królikowski

33




33








  • 1





    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.

    – Joe C
    Nov 24 '18 at 16:31














  • 1





    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.

    – Joe C
    Nov 24 '18 at 16:31








1




1





Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.

– Joe C
Nov 24 '18 at 16:31





Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.

– Joe C
Nov 24 '18 at 16:31












1 Answer
1






active

oldest

votes


















0














You need to override the getColumnClass(...) of the JTable to tell the table what type of data is in each column so the table can use the appropriate render/editor and Compartor for each column.



Something like:



@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);

if (o != null)
{
return o.getClass();
}
}

return Object.class;
}





share|improve this answer
























  • Problem solved, thx.

    – Kamil Królikowski
    Nov 27 '18 at 14:58











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460134%2fjtable-sorts-everything-as-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to override the getColumnClass(...) of the JTable to tell the table what type of data is in each column so the table can use the appropriate render/editor and Compartor for each column.



Something like:



@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);

if (o != null)
{
return o.getClass();
}
}

return Object.class;
}





share|improve this answer
























  • Problem solved, thx.

    – Kamil Królikowski
    Nov 27 '18 at 14:58
















0














You need to override the getColumnClass(...) of the JTable to tell the table what type of data is in each column so the table can use the appropriate render/editor and Compartor for each column.



Something like:



@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);

if (o != null)
{
return o.getClass();
}
}

return Object.class;
}





share|improve this answer
























  • Problem solved, thx.

    – Kamil Królikowski
    Nov 27 '18 at 14:58














0












0








0







You need to override the getColumnClass(...) of the JTable to tell the table what type of data is in each column so the table can use the appropriate render/editor and Compartor for each column.



Something like:



@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);

if (o != null)
{
return o.getClass();
}
}

return Object.class;
}





share|improve this answer













You need to override the getColumnClass(...) of the JTable to tell the table what type of data is in each column so the table can use the appropriate render/editor and Compartor for each column.



Something like:



@Override
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);

if (o != null)
{
return o.getClass();
}
}

return Object.class;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 16:31









camickrcamickr

276k16127239




276k16127239













  • Problem solved, thx.

    – Kamil Królikowski
    Nov 27 '18 at 14:58



















  • Problem solved, thx.

    – Kamil Królikowski
    Nov 27 '18 at 14:58

















Problem solved, thx.

– Kamil Królikowski
Nov 27 '18 at 14:58





Problem solved, thx.

– Kamil Królikowski
Nov 27 '18 at 14:58




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460134%2fjtable-sorts-everything-as-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin