java - How can I display dishName according to the searched price after sorting in jtable java?
问题描述
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
The above code is the code I have tried in my program. But it can display the corresponding dishName only if the data are sorted previously. If I add dish of lower price, then it displays the dishname of first row. Please do appreciate my request :)
解决方案
我写了一些可能有帮助的代码。看起来你正在使用 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;
}
推荐阅读
- php - 正则表达式选择特定的 html 元素 [Curl / PHP]
- python - Kivy 从单击的 ListItemButton 中获取名称/文本
- java - 如何将滑块拖动到某个字符串
- javascript - 如何将同步 API 与异步子类一起使用
- hyperledger-composer - HyperLedger:启动业务网络时出错
- ember.js - Ember.JS 数据模型:过滤计算属性
- python - Snakemake 与多个 Rule All
- java - CircleImageView 不使用毕加索从 URL 加载图像
- bash - VNC 查看器无法与“通道 3:打开失败:连接失败:没有到主机的路由”建立连接
- javascript - 处理与用户(操作)相关的错误的适当方法是什么?