在JTable

时间:2016-09-03 18:37:37

标签: java swing jtable

编辑:解决了问题。我不小心在return语句中添加了if而不是break。在更改DefaultTableModel之前,该函数已退出。

StockNameArr是表格中所有股票代码的数组(按字母顺序排列),按字母顺序排列。

此类表示带有两个可单击列的JTable。第一列目前仅打印到stdout,这样可以正常工作。第二个删除它所在的行,并按JComboBox的字母顺序排列每个股票的名称。据我所知,这也正常。

当点击JComboBox中的某个项目时,它应该删除该项目,创建一个String[8](与8行的其他列匹配),从该项目中插入股票名称单击,然后将数组作为一行插入到先前从中删除的位置。

但实际发生的是,该项目已从JComboBox中删除,而新行永远不会插入到表格中。我试着打电话:

model.insertItemAt(...);
thisTable.setModel(model);

但它没有做任何事情。考虑到可能modelthisTable未正确设置,我将thisTable作为类变量引用,并试图在TableModel中插入一行而不是在内部类中为它创建一个新变量。但是,没有运气。

任何人都可以帮助我吗?

import java.awt.event.Action/Event;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JMainTable extends JTable { 
    JMainTable thisTable = this; //Represents this class in inner classes
    final String[] stockNames;
    DefaultTableModel model;
    JComboBox<String> dropdown;
    ArrayList<String> dropdownStocks = new ArrayList<String> ();
    int rowChangeOffset = 0;
    public JMainTable(DefaultTableModel tableModel, String[] stockNameArr, JComboBox<String> combobox) {
        super(tableModel);
//      thisTable = this;
        stockNames = stockNameArr;
        addMouseListener(new mainTableListener());
        model = tableModel;

        dropdown = combobox;
        dropdown.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
        //^ prevents ActionListener from firing when scrolling through list with arrow keys 

            dropdown.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (!e.getActionCommand().equals(ActionEvent.KEY_EVENT_MASK)){
                       System.out.println("Clicked");
                       JComboBox<String> source = (JComboBox<String>) e.getSource();
                      String stockString = (String) source.getSelectedItem();
                      int index = dropdownStocks.indexOf(stockString);
                      try {
                          source.removeItemAt(index);
                          dropdownStocks.remove(index);
                      } catch(ArrayIndexOutOfBoundsException arrEx) {}
                      int indexInStockNames = 0; //for compiler
                      for(int i = 0; i<stockNames.length; i++) {
                          if(stockNames[i].equals(stockString)) {
                              indexInStockNames = i;
                              return;
                          }//end of If
                      }//End of For
                     String[] replacementRow = new String[8];
                     replacementRow[0] = stockString;
                     //model.insertRow(indexInStockNames, replaceRow);
                     ((DefaultTableModel) thisTable.getModel()).insertRow(indexInStockNames, replacementRow);
                     if(index>=0) {//Prevents offset from being changed 
                                   //when user clicks on empty JComboBox
                         rowChangeOffset--;
                     }

                    }
                }
            });
    }

    class mainTableListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent mouseevent) {
            int column;
            if((column = thisTable.getSelectedColumn()) == 6) {
                int row = thisTable.getSelectedRow();
                System.out.println("Button for " + stockNames[row + rowChangeOffset] + " working.");//TESTING
                //NOIMPLEMEN
            } else if((column = thisTable.getSelectedColumn()) == 7) {
                int row = thisTable.getSelectedRow();
                model.removeRow(row);
                try{
                dropdownStocks.add(stockNames[row + rowChangeOffset]);
                } catch(Exception adsf) {adsf.printStackTrace(); System.out.println(stockNames[row+rowChangeOffset]);}
                int index;
                try {
                    Collections.sort(dropdownStocks);
                    index = dropdownStocks.indexOf(stockNames[row+rowChangeOffset]);
                } catch(Exception ex) {/*dropdownStocks may be empty*/
                    index=0;
                }

                dropdown.insertItemAt(stockNames[row+rowChangeOffset], index);
                rowChangeOffset = rowChangeOffset + 1;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个,它会有助于在你的模型中添加一行链接到你的表

 model.addRow(new Object[]{objec1,object2}); //param to count of columns to table
 jTable1.setModel(modelo);
/*You are not required if in the beginning did something like this
modelo = (DefaultTableModel)jTable1.getModel();*/