-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathButtonColumnDemo.java
More file actions
69 lines (59 loc) · 1.93 KB
/
ButtonColumnDemo.java
File metadata and controls
69 lines (59 loc) · 1.93 KB
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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class ButtonColumnDemo
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
String[] columnNames = {"First Name", "Last Name", ""};
Object[][] data =
{
{"Homer", "Simpson", "delete Homer"},
{"Madge", "Simpson", "delete Madge"},
{"Bart", "Simpson", "delete Bart"},
{"Lisa", "Simpson", "delete Lisa"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model );
JScrollPane scrollPane = new JScrollPane( table );
Action delete = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
JTable table = (JTable)e.getSource();
int modelRow = Integer.valueOf( e.getActionCommand() );
Object delete = table.getModel().getValueAt(modelRow, 2);
Window window = SwingUtilities.windowForComponent(table);
int result = JOptionPane.showConfirmDialog(
window,
"Are you sure you want to " + delete,
"Delete Row Confirmation",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
{
// System.out.println( "Deleting row: " + modelRow);
((DefaultTableModel)table.getModel()).removeRow(modelRow);
}
}
};
ButtonColumn buttonColumn = new ButtonColumn(table, delete, 2);
buttonColumn.setMnemonic(KeyEvent.VK_D);
JFrame frame = new JFrame("Table Button Column");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( scrollPane );
frame.setSize(400, 160);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}