📄 musiclistview.java~118~
字号:
package musiclistview;
import java.io.*;
import java.util.Observable;
import java.util.Observer;
import java.util.Enumeration;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
import javax.swing.event.*;
import com.borland.jbcl.layout.XYConstraints;
//import musicmgrsys.MusicModel;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import musicmgrsys.*;
import musicitemview.*;
import javax.swing.JTextArea;
import java.awt.Toolkit;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
public class MusicListView extends JScrollPane implements Observer,
Serializable {
DefaultMutableTreeNode rootNode;
DefaultTreeModel treeModel;
Toolkit toolkit = Toolkit.getDefaultToolkit();
JTree jTree1;
int x = 0;
int y = 0;
String nodeStr = null;
MainView mv = null;
JPopupMenu jPopupMenu1 = new JPopupMenu();
JPopupMenu jPopupMenu2 = new JPopupMenu();
JPopupMenu jPopupMenu3 = new JPopupMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
public MusicListView(MainView mv) {
this.mv = mv;
rootNode = new DefaultMutableTreeNode("您所有的音乐集");
treeModel = new DefaultTreeModel(rootNode);
treeModel.addTreeModelListener(new MyTreeModelListener(this));
jTree1 = new JTree(treeModel);
jTree1.setAutoscrolls(true);
jTree1.setEditable(true);
getTreeSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
jTree1.setShowsRootHandles(true);
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
try
{
x = e.getX();
y = e.getY();
nodeStr = jTree1.getLastSelectedPathComponent().toString();
if (jTree1.getPathForLocation(x, y).getPathCount() == 3) {
String title = jTree1.getLastSelectedPathComponent().toString();
String setName = jTree1.getPathForLocation(x, y).getParentPath().
getLastPathComponent().toString();
String tabname = null;
String DRIVER = "com.borland.datastore.jdbc.DataStoreDriver";
String URL = "jdbc:borland:dslocal:";
String FILE = "musicmgrsys.jds";
try {
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL + FILE,
"SYSDBA", "masterkey");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"select TABNAME from MUSICSET where NAME='" +
setName + "'");
while (rs.next()) {
tabname = rs.getString("TABNAME");
}
Statement stmt1 = con.createStatement();
ResultSet rs1 = stmt1.executeQuery("select * from " +
tabname + " where TITLE='" + title + "'");
JTextArea jta = mv.getMusicItemView().getTextArea();
while (rs1.next()) {
jta.setText("");
jta.append("格式: " + rs1.getString("FORMAT") + "\n");
jta.append("种类: " + rs1.getString("GERUE") + "\n");
jta.append("等级: " + rs1.getString("RATING") + "\n");
jta.append("评价: " + rs1.getString("EVALUATION") + "\n");
jta.append("标题: " + rs1.getString("TITLE") + "\n");
jta.append("原唱: " + rs1.getString("DIRECTOR") + "\n");
jta.append("年份: " + rs1.getString("YEAR") + "\n");
jta.append("注释: " + rs1.getString("COMMENTS") + "\n");
jta.append("路径: " + rs1.getString("LOCATION") + "\n");
}
rs1.close();
rs.close();
stmt1.close();
stmt.close();
con.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.toString());
}
}
}
catch(Exception exception)
{
}
}
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger()) {
try {
if (jTree1.getPathForLocation(x, y).getPathCount() == 3) {
jPopupMenu1.show(e.getComponent(), e.getX(), e.getY()); //弹出右键菜单
} else if (jTree1.getPathForLocation(x, y).getPathCount() == 2) {
jPopupMenu2.show(e.getComponent(), e.getX(), e.getY());
} else {
jPopupMenu3.show(e.getComponent(), e.getX(), e.getY());
}
}
catch(Exception ex)
{
jPopupMenu3.show(e.getComponent(), e.getX(), e.getY());
}
}
}
};
jTree1.addMouseListener(ml);
String DRIVER = "com.borland.datastore.jdbc.DataStoreDriver";
String URL = "jdbc:borland:dslocal:";
String FILE = "musicmgrsys.jds";
try
{
Class.forName(DRIVER);
Connection con = DriverManager.getConnection(URL + FILE, "SYSDBA", "masterkey");
Statement stmt = con.createStatement();
Statement stmt1 = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from MUSICSET");
ResultSet rs1 = null;
DefaultMutableTreeNode musicset = null;
DefaultMutableTreeNode music = null;
String name = null;
String tabname = null;
while(rs.next())
{
name = rs.getString("NAME");
tabname = rs.getString("TABNAME");
musicset = new DefaultMutableTreeNode(name);
rs1 = stmt1.executeQuery("select TITLE from " + tabname);
while(rs1.next())
{
music = new DefaultMutableTreeNode(rs1.getString("TITLE"));
musicset.add(music);
}
rootNode.add(musicset);
}
if(rs1 != null)
{
rs1.close();
}
rs.close();
stmt.close();
stmt1.close();
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.toString());
}
jMenuItem1.setText("Edit...");
jMenuItem2.setText("Delete");
jMenuItem3.setText("Delete");
jMenuItem4.setText("Add new musicset...");
this.getViewport().add(jTree1, new XYConstraints(0, 0, -1, -1));
jPopupMenu1.add(jMenuItem1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu2.add(jMenuItem3);
jPopupMenu3.add(jMenuItem4);
}
public DefaultTreeModel getTreeModel(){
return (DefaultTreeModel)jTree1.getModel();
}
public DefaultMutableTreeNode getRootNode(){
return (DefaultMutableTreeNode)getTreeModel().getRoot();
}
public TreeSelectionModel getTreeSelectionModel(){
return jTree1.getSelectionModel();
}
public JTree getTree()
{
return this.jTree1;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public String getNodeStr()
{
return this.nodeStr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -