Search code examples
javaswingjdbcjtabledefaulttablemodel

Taking data from Mysql to JTable in JAVA


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


            };

Solution

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