Re: NetBeans Application with sortable Table and pre-existing
frame/table code
How do I get this modified code to work.
I modified the working/good code from the attached link to the below code. =
The reason I do not want to use the code exactly on this link is it creates=
a new window and doesn't use the form that I already am using. The pre-exi=
sting form has jPanel1 (swing JPanel) containing jPanel3 (swing JPanel). An=
d, jPanel3 contains jTable1 (swing JTable).
http://www.java-tips.org/java-se-tips/javax.swing/sorting-and-filtering-tab=
les.html
(jTable1 was dragged to the form from the swing control palette. Design vi=
ew shows a table with 4 columns and 4 rows, but the area is blanked out whe=
n I run the project!)
The below code compiles and runs, but the form's jTable1 is not populated w=
ith the data.
Thanks for any help,
....
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
Runnable runner = new Runnable() {
public void run() {
//JFrame frame = new JFrame("Sorting JTable");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rows[][] = {
{"AMZN", "Amazon", 41.28},
{"EBAY", "eBay", 41.57},
{"GOOG", "Google", 388.33},
{"MSFT", "Microsoft", 26.56},
{"NOK", "Nokia Corp", 17.13},
{"ORCL", "Oracle Corp.", 12.52},
{"SUNW", "Sun Microsystems", 3.86},
{"TWX", "Time Warner", 17.66},
{"VOD", "Vodafone Group", 26.02},
{"YHOO", "Yahoo!", 37.69}
};
String columns[] = {"Symbol", "Name", "Price"};
TableModel model =
new DefaultTableModel(rows, columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
//JTable table = new JTable(model);
RowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(model);
jTable1.setRowSorter(sorter);
JScrollPane pane = new JScrollPane(jTable1);
//frame.add(pane, BorderLayout.CENTER);
//frame.setSize(300, 150);
jPanel3.setVisible(true);
jPanel3.add(pane, BorderLayout.CENTER);
jTable1.setVisible(true);
}
};
EventQueue.invokeLater(runner);