1

I have display the Jtable depends on the particular column from database and final two column in my jtable is image fields.How can i change the column dynamically without affect the last two column.please advise me.how to do.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
kanna
  • 39
  • 3
  • 4
  • dupllicate? http://stackoverflow.com/questions/2192764/how-to-fill-data-in-a-jtable-with-database?rq=1 – keuleJ Aug 14 '12 at 05:42

2 Answers2

4

Use Vector from java.util.Collection

See this example from http://www.roseindia.com

import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableColumn;

public class JTableResultSet {
    public static void main(String[] args) {
        Vector columnNames = new Vector();
        Vector data = new Vector();
        JPanel panel = new JPanel();   //
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "root");
            String sql = "Select name,address from user";
            Statement statement = con.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columns = metaData.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(metaData.getColumnName(i));
            }
            while (resultSet.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement(resultSet.getObject(i));
                }
                data.addElement(row);
            }
            resultSet.close();
            statement.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        JTable table = new JTable(data, columnNames);
        TableColumn column;
        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            column.setMaxWidth(250);
        }
        JScrollPane scrollPane = new JScrollPane(table);        panel.add(scrollPane);               
        JFrame frame = new JFrame();
        frame.add(panel);         //adding panel to the frame
        frame.setSize(600, 400); //setting frame size
        frame.setVisible(true);  //setting visibility true
    }
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
2

TableModel.setValueAt(Objet value, int row, int col)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366