In swing, is there any possible to add a button on the header of the table. Need the swing implementing the above post. Thanks in advance.
Asked
Active
Viewed 2,371 times
1 Answers
4
Yes, it is possible. You can simply add the button to the table header. The only thing to know is that JTableHeader does not have a layout, so you need to set one.
Here is a simple demo code of this:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class TestTable2 {
protected void initUI() {
DefaultTableModel model = new DefaultTableModel();
for (int i = 0; i < 5; i++) {
model.addColumn("Col-" + (i + 1));
}
for (int i = 0; i < 200; i++) {
Vector<Object> row = new Vector<Object>();
for (int j = 0; j < 5; j++) {
row.add("New cell - " + (j + 1));
}
model.addRow(row);
}
JTable table = new JTable(model);
final JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(button, "You have clicked me");
}
});
JTableHeader header = table.getTableHeader();
header.setLayout(new FlowLayout(FlowLayout.TRAILING, 5, 0));
header.add(button);
JFrame frame = new JFrame(TestTable2.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollpane = new JScrollPane(table);
frame.add(scrollpane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTable2().initUI();
}
});
}
}

Guillaume Polet
- 47,259
- 4
- 83
- 117
-
+1 This [Q&A](http://stackoverflow.com/q/7137786/230513) includes a related example and some helpful caveats. – trashgod Dec 07 '12 at 12:06
-
Thanks dude..I need a Jtable having Add Button on the header.When add is clicked..then it must add a row containing textfield,label and delete button. When delete button is clicked, then the corresponding row should deleted.. Thanks in advance.. – deva Dec 10 '12 at 06:25