का लाइव सॉर्टिंग मैंने यह पता लगाया है कि JTable
को ठीक तरह से सॉर्ट करने के लिए कैसे प्राप्त किया जाए, लेकिन मैं यह नहीं समझ सकता कि तालिका तालिका बदलते समय सॉर्ट ऑर्डर को स्वचालित रूप से अपडेट करने के लिए इसे कैसे प्राप्त किया जाए। अभी, मेरे पास यह (स्वीकार्य रूप से लंबा) कोड है, जो ज्यादातर जावा ट्यूटोरियल के How to Use Tables पर आधारित है। मैंने // ADDED
के साथ किए गए परिवर्तनों को हाइलाइट किया है। इस मामले में, नए जोड़े गए मान सही तरीके से क्रमबद्ध होते हैं, लेकिन जब मैं किसी मूल्य को संपादित करने के लिए जाता हूं, तो मुझे लगता है कि यह fireTableCellUpdated
पर कॉल नहीं करता है?JTable
संक्षेप में, मॉडल में डेटा मान में परिवर्तन होने पर मुझे फिर से क्रमबद्ध करने के लिए तालिका कैसे मिल सकती है?
/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
* See the standard BSD license.
*/
package components;
/*
* TableSortDemo.java requires no other files.
*/
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TableSortDemo extends JPanel {
private boolean DEBUG = false;
public TableSortDemo() {
super();
setLayout(new BoxLayout(TableSortDemo.this, BoxLayout.PAGE_AXIS));
final MyTableModel m = new MyTableModel();
JTable table = new JTable(m);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
// ADDED: button to add a value
JButton addButton = new JButton("Add a new value");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
m.addValue(
JOptionPane.showInputDialog(
TableSortDemo.this, "Value?"));
}
});
// ADDED button to change a value
JButton setButton = new JButton("Change a value");
setButton.addActionListener(new ActionListener() {
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
m.setValueAt(
JOptionPane.showInputDialog(
TableSortDemo.this, "Value?"),
Integer.parseInt(
JOptionPane.showInputDialog(
TableSortDemo.this, "Which?")), 0);
}
});
add(addButton);
add(setButton);
}
class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = -7053335255134714625L;
private String[] columnNames = {"Column"};
// ADDED data as mutable ArrayList
private ArrayList<String> data = new ArrayList<String>();
public MyTableModel() {
data.add("Anders");
data.add("Lars");
data.add("Betty");
data.add("Anna");
data.add("Jon");
data.add("Zach");
}
// ADDED
public void addValue(Object v) {
data.add(v.toString());
int row = data.size() - 1;
fireTableRowsInserted(row, row);
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data.get(row) + " " + row;
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class<String> getColumnClass(int c) {
return String.class;
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data.set(row, value.toString());
// ADDED: uncommented this line, despite warnings to the contrary
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data.get(i));
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableSortDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableSortDemo newContentPane = new TableSortDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
इस मामले में छोड़कर, मैं TableModel छंटाई के लिए जिम्मेदार हो नहीं करना चाहती, मैं चाहता हूँ करने के लिए कि दृश्य में सॉर्टर द्वारा किया जाना चाहिए। जैसा कि यह खड़ा है, यह विधि तालिका को ठीक से टाइप करती है, प्रविष्टि को सही तरीके से संभालती है, लेकिन परिवर्तनों को सही तरीके से संभाल नहीं पाती है। –
फिर मॉडल पर सॉर्ट विधि का पर्दाफाश क्यों नहीं करते? –
आईएमओ यह उचित एमवीसी डिजाइन होगा। तालिका मॉडल डेटा का प्रतिनिधित्व करता है और संबंधित संचालन को समाहित करना चाहिए। यह आपको उचित घटनाओं को आग लगाने देगा। इसे सार्वजनिक करके आप बाहर से सॉर्टिंग कॉल कर सकते हैं। –