How to define the JTable's Column's data type the same as MySql database?

Arroiz :

I have created a graphical interface in Netbeans where I inserted a JTable inside a FORM JFrame. I have a MySql database and it's columns are:

Id: Integer

Name: String

Active: Boolean

However, when I use: jTable.setModel (DbUtils.resultSetToTableModel ()) (method of the library RS2XML.jar), the JTable is filled all as String.

How do I make JTable filled with the correct data types just like the database (Integer, String and Boolean)?

camickr :

the JTable is filled all as String.

The model should contain data of the proper type. The problem is you have mot defined what the data type should be for each column, so by default it is treated as Object and the default renderer will just invoke the toString() method on the object.

You can override the getColumnClass(...) method of the JTable.

Something like:

JTable table = new JTable(model)
{
    //  Returning the Class of each column will allow different
    //  renderers and editors to be used based on Class

    @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;
    }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to update JTable data with MySQL database properly?

How to select with the mouse the JTable's column name

Convert column(s) data type temporally to run a new query on the temporal column data type - MySQL

How to read excel and validate one column's data in the database?

How to identify a piece of data in an SQLite database's column?

How do mysql order by when column's alias and column's name are the same?

How would you define a type in typescript that's both an object and a function at the same time?

How to define an interface that ensures it's children and parent are of the same type as the defined interface

How to define an alias in Agda's type delaration?

How can I change a column's data type?

How to update edited JTable cell's records into Database

How to select two table's parrticular column data in mySQL

What is the correct Doctrin's column type for MySql's YEAR type

How to fill data in a JTable with database?

Java Swing: Insert JTable column into mysql database

Compare same column's data in Oracle SQL

Iteration boundaries same as data type's

How to create a new column based on matching ID's and string's in names of other columns in the same data frame?

Database column data type

How to define data type?

Asserting column(s) data type in Pandas

What's the best data type for DATE and TIME in MySQL database to display time RELATIVE to the user?

What data type and index to use for a searchable userName column in a MySQL database?

Django - how to define a MySQL CHAR data type in models?

Save edited cell/s from JTable to database

How to define an S3 generic with the same name as a primitive function?

How to map a property that's a list of the same type

Header of JTable's column won't show

How to change or find column type in JTable