본문 바로가기
→ My Meta+IT/JAVA Source

Swing table에 combo box 설정

by DigitalJobs 2021. 12. 15.

Swing table에 combo box 설정

 

 


This is an example for developers starting java. Hope this helps you.


 

 

아래 코드를 삽입하고 Table에

suffixTable.getColumn("COMBOBOX가 들어갈 컬럼").setCellRenderer(comboBoxTableCellRenderer);
suffixTable.getColumn("COMBOBOX가 들어갈 컬럼").setCellEditor(new comboDefaultCellEditor());

 

아래 내용 삽입!!

 

 DefaultTableCellRenderer comboBoxTableCellRenderer = new DefaultTableCellRenderer() {
  public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean isSelected, boolean hasFocus, int arg4, int arg5) {
   JComboBox comboBox = new JComboBox();
   comboBox.addItem(arg1);
   return comboBox;
  }
 };

 

 

 class comboDefaultCellEditor extends DefaultCellEditor {
  public comboDefaultCellEditor() {
   super(new JComboBox());
  }

  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
   JComboBox editor = (JComboBox) super.getTableCellEditorComponent(table, value, isSelected, row, column);

   Object [] tmp = (Object []) table.getModel(). getValueAt(row, 3);
   editor.removeAllItems();
   for (Object string : tmp) {
    editor.addItem(string);
   }

   if (value!= null)
    editor.setSelectedItem(value);
   return editor;
  }
 }

 

댓글