Java Source code to add Row and Column in JTable dynamically
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
package com.example; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Vector; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.WindowConstants; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; public class JTableSample { private JFrame mainFrame ; private JTable table; private JCheckBox col1; private JCheckBox col2; private JCheckBox col3; private TableColumnModel tableColumnModel; private JPanel panel; private String[] columnNamesArr; private ArrayList<String> columnNamesList; private JScrollPane scrollPane; private String[][] data; private DefaultTableModel defaultTableModel; private JButton addButton; private JButton deleteButton; private JPanel panelButton; public JTableSample() { mainFrame = new JFrame("JTableSample"); col1 = new JCheckBox("col1"); col2 = new JCheckBox("col2"); col3 = new JCheckBox("col3"); col1.setSelected(true); col2.setSelected(true); col3.setSelected(true); columnNamesList = new ArrayList<String>(); columnNamesList.add("col0"); columnNamesList.add("col1"); columnNamesList.add("col2"); columnNamesList.add("col3"); data = new String[1][columnNamesList.size()]; columnNamesArr = new String[columnNamesList.size()]; for(int i=0;i<columnNamesList.size();i++) { columnNamesArr[i] = columnNamesList.get(i); data[0][i]=""; } defaultTableModel = new DefaultTableModel(data, columnNamesArr); table = new JTable(defaultTableModel); tableColumnModel = table.getColumnModel(); for(int i=0;i<columnNamesList.size();i++) { tableColumnModel.getColumn(i).setPreferredWidth(columnNamesList.get(i).length()); } table.setPreferredScrollableViewportSize(table.getPreferredSize()); scrollPane = new JScrollPane(table); col1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(!col1.isSelected()) { TableColumn toRemove = table.getColumn("col1"); table.removeColumn(toRemove); table.validate(); } else { TableColumn toAdd = new TableColumn(); toAdd.setHeaderValue("col1"); toAdd.setIdentifier("col1"); toAdd.setPreferredWidth("col1".length()); table.addColumn(toAdd); table.validate(); } } }); col2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(!col2.isSelected()) { TableColumn toRemove = table.getColumn("col2"); table.removeColumn(toRemove); table.validate(); } else { TableColumn toAdd = new TableColumn(); toAdd.setHeaderValue("col2"); toAdd.setIdentifier("col2"); toAdd.setPreferredWidth("col2".length()); table.addColumn(toAdd); table.validate(); } } }); col3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(!col3.isSelected()) { TableColumn toRemove = table.getColumn("col3"); table.removeColumn(toRemove); table.validate(); } else { TableColumn toAdd = new TableColumn(); toAdd.setHeaderValue("col3"); toAdd.setIdentifier("col3"); toAdd.setPreferredWidth("col3".length()); table.addColumn(toAdd); table.validate(); } } }); addButton = new JButton("Add"); deleteButton = new JButton("Delete"); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Vector rowData = null; defaultTableModel.addRow(rowData); table.validate(); } }); deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Vector rowData = null; int rowCount = defaultTableModel.getRowCount(); if(rowCount>0) { defaultTableModel.removeRow(rowCount-1); table.validate(); } } }); panel = new JPanel(); panel.add(col1); panel.add(col2); panel.add(col3); panelButton = new JPanel(); panelButton.add(addButton); panelButton.add(deleteButton); mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); mainFrame.add(scrollPane, BorderLayout.CENTER); mainFrame.add(panel, BorderLayout.NORTH); mainFrame.add(panelButton,BorderLayout.SOUTH); mainFrame.pack(); mainFrame.setLocation(150, 150); mainFrame.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JTableSample jts = new JTableSample(); } }); } } |
The code has error, also where is the link to download source code as the source code link redirects to some other page .
can you specify the error you are getting
agree. your code have bunch of errors. did you test this before posting?
Thank you for pointing out the errors. I have corrected the errors.
Please let me know in case of any issues.
Don’t forget to rate it.(if you find it helpful)
Happy coding!
Hi Nikhil,
To download the code, you simple need copy-paste it. When you hover your mouse on the code section, a drop-down appears at the top in which the button on second to rightmost allows you to copy the code.
Hope it helps.