0

I open case about it but i dont understand adviced articles. Because i dont find good example about it. If someone write a sample code for below scenario very helpful for me. Let me explain my problem;

I have SQL table in MSSQL DB like;

Column 1: "Mach" (varchar)
Column 2: "ID" (int)
Column 3: "Status" (varchar)
Column 4: "IsActive" (bit) (values 1 or 0 under db table) //this one must show as checkbox in jTable

I want create a jTable with Abstract Table Model. When table created with this model, table shows 4th column as checkbox. I have a few trying but all of shows "true" or "false" on 4th column in jTable.

Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Black White
  • 700
  • 3
  • 11
  • 31
  • 2
    Start by taking a look at [How to use tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html), understand how [Cell editors and renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) work and understand the importance of the [`TabelModel#getColumnClass`](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html#getColumnClass(int)) method and how it effects what is rendered – MadProgrammer Mar 05 '14 at 01:56
  • Really i read How to use tables articles about 4-5 days. But articles always show creation model with default values. I understand the logic basically but i am stuck when i need retrieve data from resultset. – Black White Mar 05 '14 at 03:42
  • 1
    See the section on [Editors and Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) which clearly defines the requirements for the `TableModel` in order to use the default editors/renderers. Then take a look at the linked [demo](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableDemoProject/src/components/TableDemo.java) which clear demonstrates the concepts discussed. Asking for code is not how this forum works – MadProgrammer Mar 05 '14 at 03:45
  • 2
    For [example](http://stackoverflow.com/q/7137786/230513), [example](http://stackoverflow.com/a/7920159/230513), and [example](http://stackoverflow.com/a/4528604/230513). – trashgod Mar 05 '14 at 13:46
  • I have dowload [Simple Table Demo](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SimpleTableDemoProject/src/components/SimpleTableDemo.java) and run it. But this one "Vegetarian" column showing "true" or "false" in my desktop machine. Is it normal? – Black White Mar 05 '14 at 16:27
  • Hmm okay [Table Demo](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableDemoProject/src/components/TableDemo.java) this one shown Vegetarian column as checkbox because define in Abstract Table Model. I can investigate difference between them. – Black White Mar 05 '14 at 16:58
  • Really i am stuck at this point. I cant use "getValueAt" method correctly i think. I cant retrieve data from database in right format. If we can enter data manual like [Table Demo](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableDemoProject/src/components/TableDemo.java) its not problem. – Black White Mar 05 '14 at 21:18

1 Answers1

0

Finally i solve the issue. Let share my code;

Table Model Class;

class MyTableModel extends AbstractTableModel {


    private String[] columnNames = {"Mach",
                                    "ID",
                                    "Status",
                                    "IsActive"};

    private Vector<Vector<Object>> data = new Vector<Vector<Object>>();

    public MyTableModel() {try {
        fetchDB();
        } catch (Exception ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
     }

    public void fetchDB ()throws Exception{

        ResultSet rs;
        Statement stmt;
        ResultSetMetaData rsmtd;

         String query = "select Mach,ID,Status,IsActive from Configs\n" +
        "order by Machine,ID";
        try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String connectionUrl = "jdbc:sqlserver://192.168.100.100;" + "databaseName=DBST;" + "user=" + "user1" + ";" + "password=" + "user1pass" + ";"; 
        Connection con = DriverManager.getConnection(connectionUrl);
        stmt = con.createStatement();
        rs = stmt.executeQuery(query);
        rsmtd = rs.getMetaData();


        int columnCount = rsmtd.getColumnCount();
         while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);

          }

        }

        catch(SQLException se){
        se.printStackTrace();
        } 
        catch (ClassNotFoundException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
  }


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

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        return data.elementAt(rowIndex).elementAt(columnIndex);  
    }



    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }




}

Main Gui;

jTable1 = new javax.swing.JTable (new MyTableModel());
Black White
  • 700
  • 3
  • 11
  • 31