-1

I am trying to have information in my Jtable and reading the info from mysql and it looks like am the era comes from this segment as I can see am stuck , I dont know what to do, Now I have a DefaultTableModel which defines the methods JTable will use and I'm overriding the getColumnClass, I want my code to get colums from the class.

static DefaultTableModel TableModel = new DefaultTableModel(dataInfo, columns){
     public Class getColumnClass(int column) {

           if ((column >= 0) && (column < getColumnCount())) {
                  returnValue = getValueAt(0, column);
     } else {


                  returnValue;
                }


            };
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • *"I want my code to get colums from the class"* - Don't this is a really bad idea. What happens if the value in the row for the column is `null`? Better to know in advance what type of value the column is responsible for. Take a closer look at [JDBC Database Access](http://docs.oracle.com/javase/tutorial/jdbc/) for more details – MadProgrammer May 03 '15 at 03:17
  • So what do i do? now its not null. I am stuck :( – user4858328 May 03 '15 at 03:19
  • Assuming you're loading data from the database, you should use the `ResultSetMetaData` from the `ResultSet` object to get the column types and store that information in the table model, which would allow you to return the correct (expected) type, based on the information from the database – MadProgrammer May 03 '15 at 03:22
  • madprogrammer let me also read more there. – user4858328 May 03 '15 at 03:22

2 Answers2

1

If i understand you clearly , you need to add the .getclass method which helps to get the variable to the left and you also have to return the value of the class which in this case will be done as below. Taking the first entry in every column. Hope it helps. Assuming your column has no null.

static DefaultTableModel TableModel = new DefaultTableModel(dataInfo, columns){
public Class getColumnClass(int column) {
Class returnValue;
// Verifying that the column exists (index > 0 && index < number of columns
if ((column >= 0) && (column < getColumnCount())) {
  returnValue = getValueAt(0, column).getClass();
    //you need to add the .getclass method which 
                  //gets the variable to the left.
 } else {
  // Returns the class for the item in the column   
 returnValue = Object.class;
                }
 return returnValue;
              }
            };
Madona wambua
  • 1,408
  • 1
  • 21
  • 37
  • And what happens when `getValueAt(0, column)` is `null`? To be frank, this is not only stupid, but it's plain down right dangerous. A better solution would be now what the class types for each column are without relying on the data in the table (the data is expected to follow the contract of the column). Based on my "assumptions" of the what the OP is trying to do, they should be extracting this information from the `ResultSetMetaData` – MadProgrammer May 03 '15 at 03:18
  • @syombua let me try this, actually i see where my mistake was at. Give me few seconds al be back to tell you if it worked. – user4858328 May 03 '15 at 03:20
  • now my passed argument have no null. Hey syombua this was good. i appreciate. the independent variable now works. – user4858328 May 03 '15 at 03:29
1

We need to modify the TableModel slightly, so we can control the column Class types...

public class MyTableModel extends DefaultTableModel {

    private List<Class> columnTypes;

    public MyTableModel() {
    }

    public MyTableModel(int rowCount, int columnCount) {
        super(rowCount, columnCount);
    }

    public MyTableModel(Vector columnNames, int rowCount) {
        super(columnNames, rowCount);
    }

    public MyTableModel(Object[] columnNames, int rowCount) {
        super(columnNames, rowCount);
    }

    public MyTableModel(Vector data, Vector columnNames) {
        super(data, columnNames);
    }

    public MyTableModel(Object[][] data, Object[] columnNames) {
        super(data, columnNames);
    }

    public MyTableModel(List<Class> columnTypes) {
        this.columnTypes = columnTypes;
    }

    public MyTableModel(List<Class> columnTypes, Object[] columnNames, int rowCount) {
        super(columnNames, rowCount);
        this.columnTypes = columnTypes;
    }

    public MyTableModel(List<Class> columnTypes, Object[][] data, Object[] columnNames) {
        super(data, columnNames);
        this.columnTypes = columnTypes;
    }

    public MyTableModel(List<Class> columnTypes, Vector data, Vector columnNames) {
        super(data, columnNames);
        this.columnTypes = columnTypes;
    }

    public MyTableModel(List<Class> columnTypes, Vector columnNames, int rowCount) {
        super(columnNames, rowCount);
        this.columnTypes = columnTypes;
    }

    public MyTableModel(List<Class> columnTypes, int rowCount, int columnCount) {
        super(rowCount, columnCount);
        this.columnTypes = columnTypes;
    }

    public void setColumnTypes(List<Class> columnTypes) {
        this.columnTypes = columnTypes;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return columnTypes.get(columnIndex);
    }

}

Then, we you load the data from the database, you can use the ResultSetMetaData from the ResultSet to make determinations about the most appropriate type of object that the column represents (base on the return result of ResultSet#getObject)

MyTableModel tableModel = new MyTableModel();
try (ResultSet rs = ...) {
    ResultSetMetaData rsmd = rs.getMetaData();
    List<Class> columnTypes = new ArrayList<Class>(rsmd.getColumnCount());
    for (int column = 0; column < rsmd.getColumnCount(); column++) {
        String className = rsmd.getColumnClassName(column + 1);
        try {
            columnTypes.add(Class.forName(className));
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            columnTypes.add(Object.class);                  
        }
    }
    tableModel.setColumnTypes(columnTypes);

    // Load data from the ResultSet
} catch (SQLException exp) {
    exp.printStackTrace();
}

See JDBC Database Access for more details...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366