我試圖用兩個組合框在每一行中創建一個Jtable。我檢查了教程,發現我可以在組合框中添加靜態數據。但是,我怎樣才能將動態數據加載到組合框中。即使每當用戶從該行中選擇組合框1,那麼基於該組合框2,組合框2將被更新。jtable裏面的動態jcombobox物品
任何人可以幫助我在這?
如果我從組合框中輸入removeAllItems()
,那麼組合框2會更新,但我無法添加新條目。
在此先感謝。
我試圖用兩個組合框在每一行中創建一個Jtable。我檢查了教程,發現我可以在組合框中添加靜態數據。但是,我怎樣才能將動態數據加載到組合框中。即使每當用戶從該行中選擇組合框1,那麼基於該組合框2,組合框2將被更新。jtable裏面的動態jcombobox物品
任何人可以幫助我在這?
如果我從組合框中輸入removeAllItems()
,那麼組合框2會更新,但我無法添加新條目。
在此先感謝。
表中有兩列都呈現爲JComboBox.
現在,選擇列-2項目取決於列-1選擇。
import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class ComboBoxExample {
private void createUI() {
JFrame frame = new JFrame();
Object[] columNames = {"Combo-1", "Combo-2"};
Object[][] data = {{"", ""}, {"", ""}, {"", ""}, {"", ""}};
JTable table = new JTable(data, columNames);
table.getColumnModel().getColumn(0).setCellEditor(new CustomComboBoxEditor());
table.getColumnModel().getColumn(1).setCellEditor(new CustomComboBoxEditor());
frame.add(new JScrollPane(table));
frame.setTitle("Column -2 based on Column - 1 ComboBox Selection.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new ComboBoxExample().createUI();
}
};
EventQueue.invokeLater(r);
}
}
class CustomComboBoxEditor extends DefaultCellEditor {
// Declare a model that is used for adding the elements to the `ComboBox`
private DefaultComboBoxModel model;
private List<String> obtainedList;
public CustomComboBoxEditor() {
super(new JComboBox());
this.model = (DefaultComboBoxModel)((JComboBox)getComponent()).getModel();
obtainedList = new ArrayList<String>();
obtainedList.add("One");
obtainedList.add("Two");
obtainedList.add("Three");
obtainedList.add("Four");
obtainedList.add("Five");
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if(column == 0) {
model.removeAllElements();
for(int i = 0; i < obtainedList.size(); i++) {
model.addElement(obtainedList.get(i));
}
} else {
model.removeAllElements();
String selectedItem = (String) table.getValueAt(row, 0);
for(int i = 0; i < obtainedList.size(); i++) {
if(!selectedItem.equals(obtainedList.get(i)))
model.addElement(obtainedList.get(i));
}
} // Close else
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
嘗試這樣的事情,你可以改變數據在這個例子中,並重新粉刷,做電池的渲染:
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;
}
}
後removeAllItems()調用,添加項目在組合框作爲
combobox.addItem("one");
combobox.addItem("two");
車,謝謝你的幫助:)真的對我很有幫助:) – Dinesh 2013-03-08 08:54:45
隨時歡迎。 – Amarnath 2013-03-08 09:59:21
也保存了我,非常感謝 – Aleq 2017-04-27 21:28:27