在swing中,是否有可能在表头上添加一个按钮。需要执行上述帖子的摆动。提前致谢。将JButton添加到JTable中
1
A
回答
4
是的,这是可能的。您可以简单地将按钮添加到表头。唯一要知道的是JTableHeader没有布局,所以你需要设置一个。
下面是这种简单的演示代码:
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();
}
});
}
}
相关问题
- 1. 将JButton添加到JTable单元格+ DB
- 2. 无法使用CustomTableModel将JButton添加到JTable中
- 3. 使用JTable将JButton添加到框架中
- 4. 将KeyListener添加到JTable中
- 5. 将行添加到JTable中
- 6. 将jRadioButton添加到jTable中
- 7. 将听众添加到JButton
- 8. 将JButton添加到JTextPane
- 9. 将JButton添加到JPanel
- 10. 添加一个JButton向JTable细胞
- 11. 将按钮添加到jtable
- 12. 将JCombo列添加到JTable
- 13. 将按钮添加到jtable
- 14. 将RadioButton组添加到JTable
- 15. 将JComboBox添加到JTable
- 16. 将JTable添加到JScrollPane
- 17. 将行添加到jTable
- 18. 将JTable添加到JTabbedPane?
- 19. 将动作事件添加到JButton中
- 20. 通过使用JButton的actionListener添加一行到JTable
- 21. 如何将JTable添加到JTabbedPane中?
- 22. 将JTable添加到分层平面中
- 23. 将JTable添加到JTabbedPane中的JScrollPane
- 24. 将行添加到JTable中时出错
- 25. 用AbstractTableModel将行添加到JTable中
- 26. 将条目添加到JTable中
- 27. 将组件添加到JTable中
- 28. 将SQL对象添加到JTable中
- 29. JButton ActionListener和JTable
- 30. JTable和JButton
可能的[此]的复制(http://stackoverflow.com/questions/2026965/can-i-add-a-button-to -a-jtable-column-header) –