Java 从最后一个JComboBox中删除JComboBox中选定元素的总和

Java 从最后一个JComboBox中删除JComboBox中选定元素的总和,java,swing,jcombobox,Java,Swing,Jcombobox,我对Java基本上是新手,刚开始学习UI,并在一个个人项目中工作,该项目要求我有一个JTable,每行包含4个JComboBox对象 每个JComboBox将包含0到10的值。我在这里试图实现的是将前3个JComboBoxes中选择的值相加,减去10,然后从最后一个JComboBox菜单中删除结果。例如: itemToRemove = ((JComboBox1Value + JComboBox2Value + JComboBox3Value) - 10) 如果itemToRemove的值介于-

我对Java基本上是新手,刚开始学习UI,并在一个个人项目中工作,该项目要求我有一个
JTable
,每行包含4个
JComboBox
对象

每个
JComboBox
将包含0到10的值。我在这里试图实现的是将前3个
JComboBox
es中选择的值相加,减去10,然后从最后一个
JComboBox
菜单中删除结果。例如:

itemToRemove = ((JComboBox1Value + JComboBox2Value + JComboBox3Value) - 10)
如果
itemToRemove
的值介于
-10
10
之间,则将其从第四个
JComboBox
中删除,忽略正负号

这就是我到目前为止所做的:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


public class MyFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private String[] coulmnsNames;
    private Object[][] rowData;
    private String[] boxData;
    private DefaultTableModel model;
    private MyTableCellEditor editor;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MyFrame() {
        setSize(1000, 500); // sets the size of the frame
        setLocationRelativeTo(null); // positions the frame in the center of the
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        coulmnsNames = new String[]{"col1", "col2", "col3", "col4", "col5"};
        rowData = new Object[][]{{"Row1"}, {"Row2"}, {"Row3"}};
        boxData = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
        editor = new MyTableCellEditor(boxData);
        model = new DefaultTableModel(rowData, coulmnsNames);
        JTable table = new JTable(model);
        JScrollPane scroll = new JScrollPane(table);

        for(int col = 0; col < coulmnsNames.length; col++){
            TableColumn tc = table.getColumnModel().getColumn(col);
            if(col!= 0){
                tc.setCellEditor(editor);
            }
        }
        contentPane.add(scroll);
    }

}

我为我的英语和我的发帖方式道歉,这是我第一次问关于堆栈溢出的问题。任何帮助都将不胜感激。谢谢大家。

您可以计算一行的值,如果值不太高,可以将其删除。如果将以下代码添加到
MyTableCellEditor.getTableCellEditorComponent
方法中,我认为它应该可以工作:

// [existing code]

int result = -10;
for (int columnIndex = 1; columnIndex < 4; columnIndex++) {
    final String cellValue = (String) table.getValueAt(row, columnIndex);
    if (cellValue != null) {
        result += Integer.parseInt(cellValue);
    }
}
result = Math.abs(result);

if (result <= 10) {
    model.removeElement(Integer.toString(result));
    System.out.println("Removed item " + result);
}

// [existing code]
/[现有代码]
int结果=-10;
对于(int columnIndex=1;columnIndex<4;columnIndex++){
最终字符串cellValue=(字符串)table.getValueAt(行,列索引);
if(cellValue!=null){
结果+=整数.parseInt(cellValue);
}
}
结果=Math.abs(结果);

if(result您可以计算一行的值,如果值不太高,可以将其删除。如果您将以下代码添加到
MyTableCellEditor.getTableCellEditorComponent
方法中,我认为它应该可以工作:

// [existing code]

int result = -10;
for (int columnIndex = 1; columnIndex < 4; columnIndex++) {
    final String cellValue = (String) table.getValueAt(row, columnIndex);
    if (cellValue != null) {
        result += Integer.parseInt(cellValue);
    }
}
result = Math.abs(result);

if (result <= 10) {
    model.removeElement(Integer.toString(result));
    System.out.println("Removed item " + result);
}

// [existing code]
/[现有代码]
int结果=-10;
对于(int columnIndex=1;columnIndex<4;columnIndex++){
最终字符串cellValue=(字符串)table.getValueAt(行,列索引);
if(cellValue!=null){
结果+=整数.parseInt(cellValue);
}
}
结果=Math.abs(结果);
如果(结果(1)“删除产品”什么产品?您没有乘以任何值。(2)“如果
itemToRemove
的值介于
-10
10
”之间,如果没有?(3)问题是什么?(1)“删除产品”什么产品?您没有乘以任何值。(2)“如果
itemToRemove
的值介于
-10
10
之间,如果不介于两者之间,该怎么办?(3)问题是什么?”?