在jtable java中排序后,如何根据搜索到的价格显示盘名称?

时间:2018-12-30 16:55:15

标签: java jtable

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String dishName = "";
    ArrayList<Integer> st = new ArrayList<Integer>();
    int rowCount = table.getRowCount();
    int fPrice;
    int rowIndex = 0; 
    int colIndex = 4; 
    boolean emptyFlag = false;
    do {
        String price = (String) table.getValueAt(rowIndex, 4);
        fPrice = Integer.parseInt(price);
        if (price != null && price.length() != 0) {
            st.add(fPrice);
            rowIndex++;
        } else {
            emptyFlag = true;
        }
    } while (rowIndex < rowCount && !emptyFlag);
    Collections.sort(st); 
    int key = Integer.parseInt(searchPrice.getText()); 
    int low = 0;
    int high = st.size() - 1;
    int searchResult = MenuInfo.priceSearch(st, low, high, key);
    if(searchResult==-1){
        JOptionPane.showMessageDialog(this, "Could not find the dish of your price!");}
    else{
        dishName =  (String) table.getValueAt(searchResult,1); 
        JOptionPane.showMessageDialog(this, "The price you have searched can afford " + dishName);
    }
} //ends here

上面的代码是我在程序中尝试过的代码。但是,只有在对数据进行排序之前,它才能显示相应的盘名称。如果我添加了较低价格的菜,那么它会显示第一行的菜名。请感谢我的要求:)

Here is the image of my jtable

1 个答案:

答案 0 :(得分:0)

我写了一些可能有帮助的代码。看来您正在使用Swing,所以我使用了Swing。我的代码将找到相应的值,但不对行进行排序。我会分享的,但是如果您真的需要对其进行排序,请告诉我。

public static void main(String[] args) {

    String[] headers = {"Name","Number"};
    String[][] rowData = {{"foo","500"},{"bar","700"}};
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();


    //frame.add(panel);
    //A table is controlled by its model, so we need a variable to access the model
    DefaultTableModel model = new DefaultTableModel(rowData,headers);
    JTable table = new JTable(model);
    JScrollPane scroll = new JScrollPane(table);
    //To allow sorting, we us a TableRowSorter object
    TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
    table.setRowSorter(sorter);

    //We have to set the 2nd column to be sortable
    ArrayList<RowSorter.SortKey> sortKeys = new ArrayList<>();
    //sorting on 2nd column (with index of 1), which is Number
    sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
    sorter.setSortKeys(sortKeys);
    sorter.sort();

    //add new value, then sort
    Object[] newRow = {"baz", "600"};
    model.addRow(newRow);
    sorter.sort();

    panel.add(scroll);
    frame.add(panel);
    frame.setSize(800, 400);
    //show is deprecrated.. I shouldn't be using it!
    frame.show();

    int minIndex = findMin(table, 1);
    //use the row index of the min item in column 1 to get the corresponding value in column 0
    System.out.println("Minimum String: " + table.getValueAt(minIndex, 0));

}

//find min value in the column with index tableColumnIndex and return the row index of that item
public static int findMin(JTable table, int tableColumnIndex) {

    int minIndex = 0;
    String min = table.getValueAt(0, tableColumnIndex).toString();
    for (int i = 1; i < table.getRowCount(); i++) {
        if (table.getValueAt(i, tableColumnIndex).toString().compareTo(min) < 0) {
            min = table.getValueAt(i, tableColumnIndex).toString();
            minIndex = i;
        }
    }
    return minIndex;
}