Repository navigation
Expand file tree
/
Copy pathC_ButtonEditor.java
More file actions
90 lines (81 loc) · 2.41 KB
/
Copy pathC_ButtonEditor.java
File metadata and controls
90 lines (81 loc) · 2.41 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package Crawler;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.*;
import javax.swing.table.*;
/**
* @version 1.0 11/09/98
*/
public class C_ButtonEditor extends DefaultCellEditor {
// The button displayed in the cell
protected JButton button;
// The label for the button
private String label;
// Used to track if the button was pushed
private String isPushed;
// Constructor: sets up the button and its action for opening URLs
public C_ButtonEditor(JCheckBox checkBox, JTable table, DefaultTableModel dtmb) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Open the URL from the hidden column in the default browser
Desktop.getDesktop()
.browse(new URL((dtmb.getValueAt(table.getSelectedRow(), 1)).toString()).toURI());
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
}
});
}
// Returns the button component for the cell
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (isSelected) {
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
} else {
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
return button;
}
label = (value == null) ? "" : value.toString();
button.setText(label);
isPushed = "";
return button;
}
// Opens the link if the button was pushed (not used in this context)
public Object getCellEditorValue(String link) {
if (isPushed == "") {
try {
Desktop.getDesktop().browse(new URL(link).toURI());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
isPushed = "";
return new String(label);
}
// Resets the push state when editing stops
public boolean stopCellEditing() {
isPushed = "";
return super.stopCellEditing();
}
// Notifies that editing has stopped
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}