Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

What the use of TableModel and how to use that properly?

$
0
0
Here I don't understand of how TableModel is working..? I have read @pbl's tutorial about JTable, but still didn't understand.. If to create a simple and static Table (that not dynamic) then no problem.. But I want to create a dynamic Table that the value can change after have user update through a input TextField or the Table only show some column that user want to see..

From the ebook and the tutorial of pbl's I got that, If want a dynamic one then you need to custom a TableModel, by creating an abstract class that extends AbstractTableModel. What I understood of this are : I create a new custom Model and I overwrite some the method so that the method do a specific job that I want.

In tutorial, in this custom model, the number of columns and column's name is pre-define. What I want is it was define by is user choices. E.g.: When user only need column A and C (click a button) then the table only show column A and C, when user need column B, D, and E (click a button) then the Table show column B, D, and E.

Not only that, in tutorial, each rows value is also pre-define. I want the value of each rows came from Database's ResultSet query. so I need a simple method to input each query into Table.

So in my mind I need at least two new method to do what I want. SO I tried to make it. and the result is :

import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;

public class TestTable extends JFrame{
    JTable aTable;
    public static void main(String[] args) {
        //Scanner in = new Scanner(System.in);
        TestTable aTest = new TestTable();
        
    }
    
    public TestTable(){
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        MyTableModel myModel = new MyTableModel();
        
        
        String[] columnNames = {"A", "B"};
        myModel.setColumns(columnNames);
        Object[] rowDatas = {"Test", new Integer(10)};
        myModel.setRowDatas(rowDatas);
        
        aTable = new JTable(myModel);
        
        JScrollPane scrollPane = new JScrollPane(aTable);
        add(scrollPane);
        
        revalidate();
    }
    
}

class MyTableModel extends AbstractTableModel{

    ArrayList<Object[]> rowData = new ArrayList<>();
    String[] columnName;
    
    @Override
    public int getRowCount() {
        return rowData.size();
    }

    @Override
    public int getColumnCount() {
        return columnName.length;
    }

    @Override
    public String getColumnName(int col){
        return columnName[col];
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return rowData.get(rowIndex)[columnIndex];
    }
    
    public void setColumns(String[] columnName){
        this.columnName = columnName;
    }
    
    public void setRowDatas(Object[] datas){
        this.rowData.add(datas);
    }
}




Please examine this simple code, can give me any advices or any correction.... If possible give me explanation so that I can understand of how this JTable works.. thanks

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>