从最后一个JComboBox中删除JComboBox-es中所选元素的总和

时间:2015-12-27 11:29:34

标签: java swing jcombobox

我是Java的新手,刚刚开始学习UI并开始研究个人项目,这需要我拥有一个JTable,每行包含4个JComboBox个对象。

每个JComboBox将包含0到10之间的值。我在此尝试实现的是将前3个JComboBox中的选定值相加,减去10,然后删除结果从上一个JComboBox菜单。例如:

itemToRemove = ((JComboBox1Value + JComboBox2Value + JComboBox3Value) - 10)

如果itemToRemove的值介于-1010之间,则将其从第4个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类:

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

/**
 * @author Ahmed Abouelela, A00951769
 * @version Dec 14, 2015 8:02:08 AM
 */
public class MyTableCellEditor extends AbstractCellEditor implements
        TableCellEditor {

    private JComboBox<Object> menu;
    private String[] masterValues;
    private List<Integer> vals;
    private static final long serialVersionUID = 1L;
    private int value = 0;

    public MyTableCellEditor(String[] masterValues) {

        this.menu = new JComboBox<Object>();
        this.masterValues = masterValues;
        vals = new ArrayList<Integer>();

    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.CellEditor#getCellEditorValue()
     */
    @Override
    public Object getCellEditorValue() {
        return menu.getSelectedItem();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing
     * .JTable, java.lang.Object, boolean, int, int)
     */
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        DefaultComboBoxModel<Object> model = new DefaultComboBoxModel<Object>(
                masterValues);

        for (int tRow = 0; tRow < table.getRowCount(); tRow++) {

                    if(tRow != row){
                        String str = (String) table.getValueAt(tRow, column);
                        model.removeElement(str);
                }
        }
        menu.setModel(model);
        menu.setSelectedItem(value);
        return menu;
    }
}

我为我的英语和我发布的方式道歉,这是我第一次在Stack Overflow上提问。任何帮助都感激不尽。谢谢你们。

1 个答案:

答案 0 :(得分:0)

您可以计算一行的值,如果行不太高则将其删除。如果您将以下代码添加到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]

现有方法中的一些代码已经在修改组合框model:删除了为同一列中的其他行选择的值。您可以将上面的代码添加到现有代码中(或者替换它)。