2008年8月13日

Swing JComboBox within a JTable


import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.InputMap;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.TableCellEditor;

public class Main extends javax.swing.JFrame {

public Main() {
initComponents();
startEditingWithAltDown(jTable1);
startEditingAction(jTable1);
jTable1.changeSelection(0, 0, false, false);
}

private TableCellEditor createComboBoxCellEditor() {
JComboBox comboBox = new JComboBox(new Object[]{"A", "B"});
comboBox.setBorder(BorderFactory.createEmptyBorder());
return new DefaultCellEditor(comboBox);
}

private void startEditingWithAltDown(JTable table) {
KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.ALT_MASK);
InputMap inputMap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(keystroke, "startEditing");
}

private void startEditingAction(final JTable table) {
Action action = new javax.swing.AbstractAction() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
table.editCellAt(row, column);
TableCellEditor editor = table.getCellEditor();
if (editor == null) {
return;
}
Object v = table.getValueAt(row, column);
if (v == null) {
v = "";
}
Component editorComponent = editor.getTableCellEditorComponent(table, v, true, row, column);
if (editorComponent instanceof JComboBox) {
JComboBox ec = ((JComboBox)editorComponent);
if (!ec.isPopupVisible()) {
ec.requestFocus();
ec.showPopup();
}
}
}
};
table.getActionMap().put("startEditing", action);
}

@SuppressWarnings("unchecked")
//
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null}
},
new String [] {
"Title 1", "Title 2"
}
));
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getColumn(0).setCellEditor(createComboBoxCellEditor());
jTable1.getColumnModel().getColumn(1).setCellEditor(createComboBoxCellEditor());

getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

jPanel1.setLayout(new java.awt.BorderLayout());

jLabel1.setText("ComboBox showPopup: F2 or Alt+Down");
jPanel1.add(jLabel1, java.awt.BorderLayout.CENTER);

getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

pack();
}//


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration

}

沒有留言:

網誌存檔