我有一张装满组合框的桌子, 我想要做的是:如果选择了组合框中的元素,则自动更改另一个组合的选择(倒数)。 我无法找到一种方法来做到这一点。在这里,我发布了描述我想要做的事情的图像。
我的表格代码如下:
public void createCriteriaMatrix(){
jTableCriteria = new JTable();
// COLUMNS
String[] column = new String[problem.getCriteria()+1];
for(int i=0; i<problem.getCriteria()+1; i++){
column [i] = " "+i;
if(i==0){
column [i] = " ";
}
}
// DATA CELLS
String [][] data = new String[problem.getCriteria()][problem.getCriteria()+1];
for(int j=0; j<problem.getCriteria()+1; j++){
for(int i=0; i<problem.getCriteria(); i++){
data [i][j]=" ";
if(j==0){
data [i][j] = " "+(i+1);
}
}
}
//SOME TABLE FORMAT
DefaultTableModel model = new DefaultTableModel(data , column ){
@Override
public boolean isCellEditable(int row, int column) {
return column != 0;
}
};
//Calling to RenderCells() to format cell colors
jTableCriteria.setDefaultRenderer (Object.class, new RenderCells());
jTableCriteria.setModel(model);
jTableCriteria.getTableHeader().setReorderingAllowed(false);
this.placeCombosTable();
}
public void placeCombosTable(){
for(int i=0; i<=problem.getCriteria(); i++){
for(int j=0; j<=problem.getCriteria(); j++){
TableColumn weighting= jTableCriteria.getColumnModel().getColumn(i);
JComboBox comboBox = new JComboBox();
comboBox.addItem("1");
comboBox.addItem("2");
comboBox.addItem("3");
comboBox.addItem("4");
comboBox.addItem("5");
comboBox.addItem("6");
comboBox.addItem("7");
comboBox.addItem("1/2");
comboBox.addItem("1/3");
comboBox.addItem("1/4");
comboBox.addItem("1/5");
comboBox.addItem("1/6");
comboBox.addItem("1/7");
if(i==j){
comboBox.setSelectedIndex(0);
}
weighting.setCellEditor(new DefaultCellEditor(comboBox));
}
}
}
答案 0 :(得分:4)
使用官方Oracle教程How to use Table - Using a Combo Box as an Editor中的代码示例,在模型中只是从JComboBox中选择的值,而不是JComboBox
您必须覆盖setValueAt
,setValueAt
内的第1部分即将integer value
从当前JComboBox as CellEditor
存储到XxxTableModel
,第二部分是将值设置为(另一个JTables单元格)XxxTableModel中的另一个单元格
我会
使用DefaultTableModel
要覆盖getColumnClass
的{{1}}(必须包含整数以避免解析)
JComboBox
答案 1 :(得分:0)
您可以使侦听ComboBox状态的ActionListener更改并更改其他ComboBox值。 你也可以试试PopupMenuEvent。
答案 2 :(得分:0)
我用数组解决了它的倒数成对比较(比较然后将其他组合设置为正确的值),组合框的监听器和setValueAt()而不是setSelectedItem()。 代码如下:
public void placeCombosTable(){
for(int i=0; i<=problem.getCriteria(); i++){
for(int j=0; j<=problem.getCriteria(); j++){
TableColumn weighting = jTableCriteria.getColumnModel().getColumn(i);
JComboBox comboBox = new JComboBox();
comboBox.addItem("1");
comboBox.addItem("2");
comboBox.addItem("3");
comboBox.addItem("4");
comboBox.addItem("5");
comboBox.addItem("6");
comboBox.addItem("7");
comboBox.addItem("1/2");
comboBox.addItem("1/3");
comboBox.addItem("1/4");
comboBox.addItem("1/5");
comboBox.addItem("1/6");
comboBox.addItem("1/7");
if(i==j){
comboBox.setSelectedIndex(0);
}
//SCALE
final String scale[][] = {
{ "1", "2", "3", "4", "5", "6", "7" },
{ "1", "1/2", "1/3","1/4","1/5","1/6","1/7" }
};
//LISTENER
comboBox.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JComboBox combo = (JComboBox)e.getSource();
//LOOK FOR THE RECIPROCAL ON THE SCALE
String item = (String)combo.getSelectedItem();
String itemReciprocal = "";
for (int i = 0; i <= 0; i++) {
for (int j = 0; j < 7; j++) {
if(item.equalsIgnoreCase(scale[i][j])){
itemReciprocal = scale[1][j];
}
}
}
//WITH THE RECIPROCAL I PROCEEDE TO SET THE CORRESPONDING CELL
int row = jTableCriteria.getSelectedRow()+1;
int column = jTableCriteria.getSelectedColumn();
int reciprocalRow = column ;
int reciprocalColumn = row ;
jTableCriteria.getModel().setValueAt(itemReciprocal, reciprocalRow , reciprocalColumn );
}
}
);
weighting.setCellEditor(new DefaultCellEditor(comboBox));
}
}
}