Versuche gerade die Struktur eines JTrees im nachhinein zu ändern. Dies gestaltet sich jedoch schwieriger als gedacht. Das Programm möchte ich benutzen, um MySQL Datenbanken zu visualisieren. Funktioniert auch schon im seperaten Fenster, dass die Struktur dargestellt wird. Jetzt möchte ich jedoch die Strukur in meinen dafür vorgesehenen Tree bekommen - wie mache ich das?
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import com.mysql.jdbc.Driver;
import java.sql.*;
import java.lang.Class;
/**
*
* Beschreibung
*
* @version 1.0 vom 23.02.2007
* @author
*/
public class test extends JFrame {
// Anfang Variablen
private JTextField datenbank = new JTextField();
private JTextField user = new JTextField();
private JTextField host = new JTextField();
private JPasswordField pass = new JPasswordField();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JButton connect_button = new JButton();
private javax.swing.tree.DefaultMutableTreeNode root =
new javax.swing.tree.DefaultMutableTreeNode("root");
private JTree struktur = new JTree(root);
private JTable jTable1 = new JTable(5, 5);
// Ende Variablen
private Connection conn;
public test(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); }
});
int frameWidth = 800;
int frameHeight = 600;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2 ;
setLocation(x, y);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
setLayout(null);
datenbank.setBounds(585, 537, 105, 24);
datenbank.setText("");
cp.add(datenbank);
user.setBounds(208, 537, 105, 24);
user.setText("");
cp.add(user);
host.setBounds(40, 537, 121, 24);
host.setText("");
cp.add(host);
jLabel1.setBounds(8, 540, 28, 16);
jLabel1.setText("Host");
jLabel1.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
cp.add(jLabel1);
jLabel2.setBounds(174, 540, 29, 16);
jLabel2.setText("User");
jLabel2.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
cp.add(jLabel2);
jLabel3.setBounds(327, 540, 55, 16);
jLabel3.setText("Passwort");
jLabel3.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
cp.add(jLabel3);
jLabel4.setBounds(513, 540, 66, 16);
jLabel4.setText("Datenbank");
jLabel4.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
cp.add(jLabel4);
connect_button.setBounds(704, 537, 75, 24);
connect_button.setFont (new Font("MS Sans Serif", Font.PLAIN, 13));
connect_button.setText("OK");
cp.add(connect_button);
connect_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
connect_buttonActionPerformed(evt);
}
});
pass.setBounds(392, 537, 105, 24);
pass.setText("");
cp.add(pass);
struktur.setBounds(8, 8, 177, 521);
root.add(new javax.swing.tree.DefaultMutableTreeNode("node"));
cp.add(struktur);
/*struktur.getSelectionModel().addTreeSelectionListener(
new TreeSelectionListener()
{
public void valueChanged( TreeSelectionEvent e )
{
TreePath path = e.getNewLeadSelectionPath();
System.out.println( path );
}
}
);*/
jTable1.setBounds(200, 8, 576, 520);
cp.add(jTable1);
// Ende Komponenten
setResizable(false);
setVisible(true);
}
// Anfang Ereignisprozeduren
public void connect_buttonActionPerformed(ActionEvent evt){
try
{
Class.forName("com.mysql.jdbc.Driver");
char[] password = pass.getPassword();
try {
conn = DriverManager.getConnection("jdbc:mysql://" + host.getText() +
"/" + datenbank.getText() + "?" +
"user=" + user.getText() +
"&password=" + new String(password));
System.out.println("SQL meldet: Verbindung erfolgreich");
getDatenbank();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
catch(ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
// Ende Ereignisprozeduren
public void getDatenbank(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//Den Datenbankbaum erstellen
DefaultMutableTreeNode root2 = new DefaultMutableTreeNode(datenbank.getText());
try{
Statement stmt = conn.createStatement();
Statement stmt2 = conn.createStatement();
String query = "SHOW tables FROM "+datenbank.getText();
ResultSet rs = stmt.executeQuery( query );
while ( rs.next() ){
DefaultMutableTreeNode node = new DefaultMutableTreeNode(rs.getString(1));
root2.add( node );
String query2 = "SHOW FIELDS FROM " + rs.getString(1);
ResultSet rs2 = stmt2.executeQuery( query2 );
while ( rs2.next() ){
node.add( new DefaultMutableTreeNode(rs2.getString(1)));
}
rs2.close();
}
stmt2.close();
rs.close();
stmt.close();
}
catch ( SQLException e )
{
e.printStackTrace();
return;
}
// struktur.setModel(root2);
JTree tree = new JTree( root2 );
frame.add( new JScrollPane( tree ) );
frame.pack();
frame.setVisible( true );
tree.getSelectionModel().addTreeSelectionListener(
new TreeSelectionListener()
{
public void valueChanged( TreeSelectionEvent e )
{
TreePath path = e.getNewLeadSelectionPath();
System.out.println( path );
}
}
);
}
public static void main(String[] args) {
new test("test");
}
}