[英]Conditional JComboBox inside JTable - How to Automatically change values
I have a table filled with comboboxes, What I want to do is the folowing: If an element in a combobox is selected, then automatically change the selection on another combo (the reciprocal). 我有一个充满组合框的表,我想要做的是下面的内容:如果选择了组合框中的元素,则自动更改另一个组合的选择(倒数)。 I can't figureout a way to do it. 我无法找到一种方法来做到这一点。 Here I post an image describing what I want to do. 在这里,我发布了描述我想要做的事情的图像。
The code of my table is the following: 我的表的代码如下:
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));
}
}
}
use code example from official Oracle tutorial How to use Table - Using a Combo Box as an Editor , in model is stoere only selected value from JComboBox, not JComboBox 使用官方Oracle教程中的代码示例如何使用表 - 使用组合框作为编辑器 ,在模型中只能从JComboBox中选择值,而不是JComboBox
you have to override setValueAt
, 1st part inside setValueAt
is about to store integer value
from current JComboBox as CellEditor
to XxxTableModel
, second part is about to set value to (another JTables cell) another cell in XxxTableModel 你必须重写setValueAt
,内部第一部分setValueAt
即将存储integer value
从当前JComboBox as CellEditor
至XxxTableModel
,第二部分是在XxxTableModel值设置为(另一个JTable中小区)的另一小区
I'd be 我会
to use DefaultTableModel
使用DefaultTableModel
there is to override getColumnClass
for JComboBox
(to have to contains integer to avoiding parsing) 有覆盖JComboBox
getColumnClass
(必须包含整数以避免解析)
isCellEditable
You can make ActionListener that listen to ComboBox state changed and change other ComboBox value. 您可以使侦听ComboBox状态的ActionListener更改并更改其他ComboBox值。 Also you can try PopupMenuEvent for this. 您也可以尝试使用PopupMenuEvent。
I solved it with an Array for the reciprocal pairwise comparison (to compare and then set the other combo with the correct value), a Listener for the combobox and a setValueAt() instead of a setSelectedItem(). 我用一个数组解决了它的倒数成对比较(比较然后用正确的值设置另一个组合),组合框的监听器和setValueAt()而不是setSelectedItem()。 The code is the following: 代码如下:
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));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.