我创建了两个 JPanel。一个 JPanel 有一个带有节点的 JTree。另一个 JPanel 有一个 SQL 填充的 JTable。我正在尝试从 JTree 获取节点选择,以使用来自 JTree 的输入重新运行 JTable 的查询,为 JTable 提供部分过滤器。你能提供指导吗?我是 Java 新手。这是问题的代码:
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class SCCEEforTreeAndTableInteraction {
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
private static ResultSetMetaData meta;
private static int columnCount = 0;
private static int rowCount = 0;
private static Object[][] result;
private static String[] columnNames;
private static JTree tree;
public static Hashtable<Object, Integer> treeData;
public int tableTopicNodeID;
public static void main(String[] args) {
//Create and setup window
JFrame frame = new JFrame("Tree and Table Interaction");
JPanel panelMain = new JPanel(new BorderLayout());
JPanel treePanel = new JPanel(new BorderLayout());
JPanel tablePanel = new JPanel(new BorderLayout());
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePanel, tablePanel);
split.setOneTouchExpandable(true);
myJTree tree = new myJTree();
myJTable table = new myJTable();
frame.add(panelMain);
treePanel.add(tree);
tablePanel.add(table);
panelMain.add(split);
frame.setVisible(true);
panelMain.setVisible(true);
treePanel.setVisible(true);
tablePanel.setVisible(true);
frame.pack();
}
private static class myJTable extends JPanel {
//Getting the data from the database in order to fill the table
public myJTable() {
try {
conn = DriverManager.getConnection("jdbc:derby://localhost:1527/FlamingDartDB;create=true;user=user;password=password");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("select count(*) from FLAMINGDART.TOPICDATA where NODEID =" + *Start with a default, then get data from the listener below*); //NodeID should be different every time I select a node
rs.beforeFirst();
while (rs.next()) {
rowCount = rs.getInt(1);
}
rs = stmt.executeQuery("select * from FLAMINGDART.TOPICDATA where NODEID =3");//NodeID should be different every time I select a node
meta = rs.getMetaData();
columnCount = meta.getColumnCount(); //The first 2 column are not for display to the user
columnNames = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
columnNames[i] = meta.getColumnName(i + 1);
}
result = new Object[rowCount][columnCount];
rs.beforeFirst();
for (int i = 0; i < rowCount; i++) {
rs.next();
for (int j = 0; j < columnCount; j++) {
result[i][j] = rs.getObject(j + 1);
}
}
stmt.close();
conn.close();
} catch (SQLException sqlExcept) {
sqlExcept.printStackTrace();
}
DefaultTableModel tableModel = new DefaultTableModel(result, columnNames);
JTable table = new JTable(tableModel);
JScrollPane topicTableScrollPane = new JScrollPane(table);
topicTableScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(topicTableScrollPane);
}
}
private static class myJTree extends JPanel implements TreeSelectionListener {
public myJTree() {
//This is sample tree data with the node ID that is used to refine the table query.
treeData = new Hashtable();
treeData.put("Node 1", new Integer(1));
treeData.put("Node 2", new Integer(2));
treeData.put("Node 3", new Integer(3));
treeData.put("Node 4", new Integer(4));
treeData.put("Node 5", new Integer(5));
treeData.put("Node 6", new Integer(6));
treeData.put("Node 7", new Integer(7));
treeData.put("Node 8", new Integer(8));
treeData.put("Node 9", new Integer(9));
tree = new JTree(treeData);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);
this.add(tree);
}
@Override
public void valueChanged(TreeSelectionEvent tse) {
DefaultMutableTreeNode nodeSelected = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (nodeSelected == null) //Nothing is selected.
{
System.out.println("Selected Node is Null");
return;
}
Integer n = treeData.get(nodeSelected.getUserObject());//Returns the integer value associated with the key from the node table
if (n != null) {
System.out.println("Start a new query with " + n + " as a filter for the query.");
*//This is supposed to set the node id that helps populate the table with a new query. Something along the lines of table.SetTableModel(n) that I'm having trouble implementing.*
}
}
}
}