0

In my table. I want to put some buttons into each row that I can press. But I do not know how to do it

public static DefaultTableModel buildTableModel(ResultSet rs)
        throws SQLException {
    java.sql.ResultSetMetaData metaData = rs.getMetaData();

    // names of columns
    Vector<String> columnNames = new Vector<String>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }

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

        data.add(vector);
    }
    return new DefaultTableModel(data, columnNames);
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Yeah it is how to put buttons in my JTable – user1880497 Dec 09 '12 at 15:51
  • 3
    Have a look here: [How to make a JButton in a JTable cell click-able?](http://stackoverflow.com/questions/5555938/how-to-make-a-jbutton-in-a-jtable-cell-click-able) for several good suggestions. You can search this site for other similar questions and answers. – Hovercraft Full Of Eels Dec 09 '12 at 15:55
  • 1
    Ok. So you want buttons in each table cell... And how is your code related with it? – user219882 Dec 09 '12 at 15:57
  • 2
    Also please have a look at Rob Camick's blog article titled [Table Button Column](http://tips4java.wordpress.com/2009/07/12/table-button-column/). His blog is a treasure chest full of Swing explanations and examples and other Java gems. – Hovercraft Full Of Eels Dec 09 '12 at 16:00

1 Answers1

1

For doing this you have to first look at,

  1. Editors and Renderers

  2. How to render a cell in JTable as Button example.

Not only the above stated example, you have many sources which shows how to add a button in a JTable cell.

Amarnath
  • 8,736
  • 10
  • 54
  • 81