gpt4 book ai didi

java - 如何根据jTable中的jComboBox选择更新相邻字段?

转载 作者:行者123 更新时间:2023-11-30 07:53:13 25 4
gpt4 key购买 nike

我有一个场景,其中在 jTable 中选择颜色的 jComboBox 更改了 jTable 同一行中具有所选颜色名称的另一个字段。

enter image description here

当选择 jComboBox 时,相邻字段会相应更改,但是当我单击另一个 jComboBox 而不是更改相邻字段时,它会更改先前选择的行。

enter image description here

这是我到目前为止的代码:

import javax.swing.DefaultCellEditor;
import javax.swing.table.*;

public class TestJTable extends javax.swing.JFrame {

public TestJTable() {
initComponents();

String[] columnNames = {"Choose Colour", "Colour Chosen"};
Object[][] data =
{
{"Red", new String("Red Colour")},
{"Blue", new String("Blue Colour")},
{"Green", new String("Green Colour")},
{"Yellow", new String("Yellow Colour")}
};

DefaultTableModel model = new DefaultTableModel(data, columnNames);

jTable.setModel(model);

jTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(jCBColour));



}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jCBColour = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
jTable = new javax.swing.JTable();

jCBColour.setEditable(true);
jCBColour.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Red", "Blue", "Green", "Yellow" }));
jCBColour.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCBColourActionPerformed(evt);
}
});

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Choose Colour", "Colour Chosen"
}
));
jScrollPane1.setViewportView(jTable);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(113, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(14, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(178, 178, 178))
);

pack();
}// </editor-fold>

private void jCBColourActionPerformed(java.awt.event.ActionEvent evt) {

System.out.println(evt.getActionCommand());
System.out.println(jCBColour.getSelectedIndex());

if (jCBColour.getSelectedIndex() != -1) {

switch (jCBColour.getSelectedIndex()){
case 0: jTable.setValueAt("Red Colour", jTable.getSelectedRow(), 1);
break;
case 1: jTable.setValueAt("Blue Colour", jTable.getSelectedRow(), 1);
break;
case 2: jTable.setValueAt("Green Colour", jTable.getSelectedRow(), 1);
break;
case 3: jTable.setValueAt("Yellow Colour", jTable.getSelectedRow(), 1);
break;
default:
break;
}
}

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestJTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestJTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestJTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestJTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestJTable().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JComboBox jCBColour;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable;
// End of variables declaration
}

任何帮助将不胜感激:)

最佳答案

我无法重复您的问题。对于我在 Windows 7 上使用 JDK8_u45 来说,它工作得很好。

话虽如此,您不应该尝试从组合框中的 ActionListener 更新模型。

相反,您应该使用自定义 TableModel,只要第一列发生更改,该模型就会更新第二列。像这样的东西:

DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
@Override
public void setValueAt(Object value, int row, int column)
{
super.setValueAt(value, row, column);

if (column == 0)
{
String color = value.toString();

switch (column)
{
case "Red": setValueAt("Red Color", row, 1); break;
case "Blue": setValueAt("Blue Color", row, 1); break;
...
}

}
}
};

无论 TableModel 是通过 JTable 还是直接更新 TableModel,这都将确保数据正确。

关于java - 如何根据jTable中的jComboBox选择更新相邻字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33053551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com