Posts

Showing posts with the label jTable

How to add dynamically rows number in jTable - Java

Most of the programming now a days required dynamically create controller such as button, textbox and also table. Today i want to show how to dynamically set rows number in jTable Swing Controls. Firstly is to set the DefaultTableModel and assign to jTable. Here is the example : The DefaultTableModel import javax.swing.table.DefaultTableModel;   : : String colNames[] = {"Name", "Edit", "View"};  DefaultTableModel dtm = new DefaultTableModel(null,colNames);  Do looping to add rows in jTable for(int i=0; i < listOfString .size();i++){             dtm.addRow(new String[3]); } Assign to jTable  jTable2.setModel(dtm); The full Code :  String colNames[] = {"Name", "Edit", "View"};   DefaultTableModel dtm = new DefaultTableModel(null,colNames);  List<String> listOfString = new ArrayList<>();         listOfString.add("1");         listOfString.add("2");         listOfString.add("3");      

How to add button in JTable - Java

Image
How to works with JTable in java and want to insert button in the JTable column. Let say you want to add button in JTable column like in this pic :    First step is to get the column of the button and set the CellRenderer for the column :  jTable2.getColumn("Edit").setCellRenderer(new ButtonRenderer());  jTable2.getColumn("Edit").setCellEditor(new ButtonEditor(new JCheckBox()));              jTable2.getColumn("View").setCellRenderer(new ButtonRenderer());  jTable2.getColumn("View").setCellEditor(new ButtonEditor(new JCheckBox())); The Class ButtonRenderer import java.awt.Component; import javax.swing.JButton; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.table.TableCellRenderer; /**  *  * @author hp1  */ public class ButtonRenderer extends JButton implements TableCellRenderer {     public ButtonRenderer() {         setOpaque(true);     }     @Override     public Component getTableCellRendererComponent(JTable table, O