سؤال

وأريد أن أضع JComboBoxes فرد في كل خلايا من JTable. بمعنى آخر. محتوى JComboBox ليست متطابقة لكل خلية.

وأنا أساسا ترغب في أن تكون قادرة على مجرد دعوة البرمجية التالية لإضافة صف من JComboBox في JTable. أي شخص لديه أي فكرة؟ بفضل

JComboBox cb1 = new JComboBox(...);
JComboBox cb2 = new JComboBox(...);
model.addRow(new Object[] {"Row name", cb1, cb2} );

JComboBox cb3 = new JComboBox(...);
JComboBox cb4 = new JComboBox(...);
model.addRow(new Object[] {"Row name 2", cb3, cb4} );

وأقرب مثال التعليمة البرمجية يمكنني العثور على النحو التالي. ولكن كان للمحتوى حيث JComboBox مطابق للعمود فردي. ليس الحل أحتاج.

TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyComboBoxEditor(values));

حيث

public class MyComboBoxEditor extends DefaultCellEditor {
    public MyComboBoxEditor(String[] items) {
        super(new JComboBox(items));
    }
}
هل كانت مفيدة؟

المحلول

وأسهل طريقة لتنفيذ الخاص بك <لأ href = "http://java.sun.com/javase/6/docs/api/javax/swing/table/TableModel.html" يختلط = "نوفولو noreferrer" > TableModel

public class MyModel extends AbstractTableModel {
    List rows;

    public int getRowCount() {
        return rows.size();
    }

    public int getColumnCount() {
         return 4;
    }

    public Object getValueAt(int row, int column) {
        return rows.get(row).getCol(col);  //assuming your row "Object" has a getCol()
    }

    public Class<?> getColumnClass(int col) {
        return Boolean.class;
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        rows.get(rowIndex).getCol(columnIndex).setValue(aValue);
    }

}

وتحميل هذا لك في JTable. إذا لم تكن قد حلت محل العارض خلية الافتراضي للومنطقية، سيتم تقديم جميع الخلايا لكم وخانات الاختيار الشكر لكم تنفيذ getColumnClass (). يتم جمع كل إدخال المستخدم لهذه خانات الاختيار مع setValueAt لدينا ().

نصائح أخرى

وتوسيع JTable مع هذا الرمز:

@Override
public TableCellEditor getCellEditor(int row, int column) {
   Object value = super.getValueAt(row, column);
   if(value != null) {
      if(value instanceof JComboBox) {
           return new DefaultCellEditor((JComboBox)value);
      }
            return getDefaultEditor(value.getClass());
   }
   return super.getCellEditor(row, column);
}

وهذا سيخلق محرر خلية JComboBox فريد لكل مربع التحرير والسرد تحصل على قيمة ل.

وأنا متأكد من أن هذا سوف يحل مشكلتك. أذكر في أي عمود تحتاج إلى تعيين مربع التحرير والسرد في .getColumn (العمود كثافة العمليات)

private void addComboToTable(JComboBox combo) {
    TableColumn gradeColumn = YourTable.getColumnModel().getColumn(0);
    JComboBox comboBox = combo;
    comboBox.removeAllItems();
    try {
        comboBox.addItem("Item 1");
        comboBox.addItem("Item 2");
        comboBox.addItem("Item 3");
    } catch (NullPointerException e) {
    } catch (Exception e) {
        e.printStackTrace();
    }
    gradeColumn.setCellEditor(new DefaultCellEditor(comboBox));
}

وتحتاج إلى تجاوز:

Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

... في TableCellEditor. قيمة تمريره إلى هذا الأسلوب هو ما يمكنك وضعها في JComboBox الخاص بك. وهذا يعني أن "القيمة" لذلك خلية معينة يجب أن يكون شيئا يمكن أن تترجم إلى مجموعة. ومن المحتمل أن تكون مجرد قائمة من الأشياء أو أنه يمكن أن يكون POJO مع الحقول التي يمكن أن يتم في JComboBox.

وحتى مجرد تعديل MyComboBoxEditor لتجاوز تلك الطريقة وتغيير النموذج الخاص بك للسماح لأحد الكائنات التي تمثل في الواقع عدة أشياء أخرى.

ومحتوى JComboBox هو تقديم متطابقة لكل اختيار التوالي بسبب وJTable لا تقدم القدرة على أن يكون رئيس تحرير أكثر من واحد في العمود. لديك لتوسيع الطبقة JTable لدعم اختيار إضافية للصفوف.

وهذه المادة توضح ذلك بشكل جيد جدا: http://www.javaworld.com/javaworld/javatips/jw-javatip102.html

وبالإضافة إلى cellEditor فمن الضروري للقيام cellRenderer لطلاء منسدل في الخلية، والنظر في هذا:

 public void example(){  

      TableColumn tmpColum =table.getColumnModel().getColumn(1);
      String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" };
      JComboBox comboBox = new JComboBox(DATA);

      DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
      tmpColum.setCellEditor(defaultCellEditor);
      tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
      table.repaint();
   }


/**
   Custom class for adding elements in the JComboBox.
*/
class CheckBoxCellRenderer implements TableCellRenderer {
        JComboBox combo;
        public CheckBoxCellRenderer(JComboBox comboBox) {
            this.combo = new JComboBox();
            for (int i=0; i<comboBox.getItemCount(); i++){
                combo.addItem(comboBox.getItemAt(i));
            }
        }
        public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            combo.setSelectedItem(value);
            return combo;
        }
    }
@Override
public TableCellEditor getCellEditor(int row, int column) {
   Object value = super.getValueAt(row, column);
   if(value != null) {
      if(value instanceof JComboBox) {
           return new DefaultCellEditor((JComboBox)value);
      }
            return getDefaultEditor(value.getClass());
   }
   return super.getCellEditor(row, column);
}

وبعد ذلك، تجاوز أسلوب toString من JComboBox.

هذه الصفحة قد تساعدك ، على الرغم من أنه يبدو أنت مقيد إلى وجود نفس منسدل في جميع الخلايا في العمود.

وتحتاج إلى إنشاء فئة فرعية من JTable إلى تجاوز أسلوب TableCellEditor getCellEditor (الصف كثافة العمليات، العمود كثافة العمليات).

وهذا يتيح لك لتعيين المحررين خلية التعسفي لأي مزيج الصفوف والأعمدة. الطريقة الافتراضية هي لتعيين محرر خلية للعمود بأكمله.

و(يمكنك أيضا تعيين عارضين الخلايا الفردية عن طريق تجاوز getCellRenderer).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top