从JDialog查询数据库内容填充到 JFarme主窗口的JTree
主窗口已经获取到值 但填充到JTree 无变化
下面两个主窗口的构造方法
第1个是没有加载数据的JTree只有一根根结点
第2个是从JDialog 得到数据后 重载的方法,已经能打印数据出来 为什么主窗口不刷新树
public mainFrame() {
initialize();
root = new DefaultMutableTreeNode("Database");
db_tree = new JTree(root);
left_db_pane.add(db_tree);
db_tree.doLayout();
}
//////获得数据后刷新树
public mainFrame(Vector table) {
this.table = table;
//left_db_pane.remove(db_tree);
//root = new DefaultMutableTreeNode(table);
//DefaultTreeModel roots = new DefaultTreeModel(root);
root = new DefaultMutableTreeNode();
for(int i=0;i<table.size();i++){
System.out.println("table ="+table.elementAt(i).toString());
root.add(new DefaultMutableTreeNode(table.elementAt(i).toString()) );
}
db_tree = new JTree(root);
left_db_pane=new JPanel();
left_db_pane.add(db_tree);
left_tpane = new JTabbedPane();
left_tpane.add(left_db_pane);
db_tree.doLayout();
initialize();
}
///////JDialog中的构造方法中传入 主窗口的引用
public class mydialog extends JDialog {
private mainFrame mf = null;
public mydialog (mainFrame mf){
this.mf=mf;
}
.......
////// 得到数据后
mf = new mainFrame(sql);
.........
}
如何改能正确刷新tree的内容?
重新设置一下tree的model就行了
mainframe
[code="java"]
public void flushTree(DefaultTreeModel model) {
jTree0.setModel(model);
}
[/code]
dialog
[code="java"]
private void jButton1MouseMouseClicked(MouseEvent event) {
FrameTree parent = (FrameTree) getParent();
DefaultMutableTreeNode node0 = new DefaultMutableTreeNode("DialogTree");
{
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("people");
{
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("man");
node1.add(node2);
}
{
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("women");
node1.add(node2);
}
node0.add(node1);
}
DefaultTreeModel model = new DefaultTreeModel(node0);
parent.flushTree(model);
}
[/code]
整个的
[code="java"]
package com;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
//VS4E -- DO NOT REMOVE THIS LINE!
public class FrameTree extends JFrame {
private static final long serialVersionUID = 1L;
private JTree jTree0;
private JScrollPane jScrollPane0;
private JButton jButton0;
private JButton jButton1;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public FrameTree() {
initComponents();
}
private void initComponents() {
setLayout(new GroupLayout());
add(getJScrollPane0(), new Constraints(new Leading(37, 150, 10, 10), new Leading(16, 200, 10, 10)));
add(getJButton0(), new Constraints(new Leading(205, 12, 12), new Leading(29, 10, 10)));
add(getJButton1(), new Constraints(new Leading(217, 10, 10), new Leading(79, 10, 10)));
setSize(320, 240);
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("AddLeaf");
jButton1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
jButton1MouseMouseClicked(event);
}
});
}
return jButton1;
}
private JButton getJButton0() {
if (jButton0 == null) {
jButton0 = new JButton();
jButton0.setText("showDialog");
jButton0.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
jButton0MouseMouseClicked(event);
}
});
}
return jButton0;
}
private JScrollPane getJScrollPane0() {
if (jScrollPane0 == null) {
jScrollPane0 = new JScrollPane();
jScrollPane0.setViewportView(getJTree0());
}
return jScrollPane0;
}
private JTree getJTree0() {
if (jTree0 == null) {
jTree0 = new JTree();
DefaultTreeModel treeModel = null;
{
DefaultMutableTreeNode node0 = new DefaultMutableTreeNode("JTree");
{
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("colors");
{
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("blue");
node1.add(node2);
}
{
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("violet");
node1.add(node2);
}
node0.add(node1);
}
treeModel = new DefaultTreeModel(node0);
}
jTree0.setModel(treeModel);
}
return jTree0;
}
private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage());
}
}
/**
* Main entry of the class. Note: This class is only created so that you can
* easily preview the result at runtime. It is not expected to be managed by
* the designer. You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
FrameTree frame = new FrameTree();
frame.setDefaultCloseOperation(FrameTree.EXIT_ON_CLOSE);
frame.setTitle("FrameTree");
frame.getContentPane().setPreferredSize(frame.getSize());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private void jButton0MouseMouseClicked(MouseEvent event) {
JDialog d = new HitDialog(this);
d.setVisible(true);
}
public void addNode(String name) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
TreeModel model = jTree0.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(name);
DefaultTreeModel treeModel = (DefaultTreeModel) jTree0.getModel();
treeModel.insertNodeInto(childNode, root, root.getChildCount());
}
public void flushTree(DefaultTreeModel model) {
jTree0.setModel(model);
}
public DefaultMutableTreeNode addObject(Object child) {
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = jTree0.getSelectionPath();
TreeModel model = jTree0.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
if (parentPath == null) {
// There is no selection. Default to the root node.
parentNode = root;
} else {
parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent());
}
return addObject(parentNode, child, true);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) {
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
DefaultTreeModel treeModel = (DefaultTreeModel) jTree0.getModel();
treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
// Make sure the user can see the lovely new node.
if (shouldBeVisible) {
jTree0.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}
private void jButton1MouseMouseClicked(MouseEvent event) {
addObject("Here is");
}
}
[/code]
[code="java"]
package com;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
//VS4E -- DO NOT REMOVE THIS LINE!
public class HitDialog extends JDialog {
private static final long serialVersionUID = 1L;
private JButton jButton0;
private JButton jButton1;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public HitDialog() {
initComponents();
}
public HitDialog(Frame parent) {
super(parent);
initComponents();
}
public HitDialog(Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
public HitDialog(Frame parent, String title) {
super(parent, title);
initComponents();
}
public HitDialog(Frame parent, String title, boolean modal) {
super(parent, title, modal);
initComponents();
}
public HitDialog(Frame parent, String title, boolean modal, GraphicsConfiguration arg) {
super(parent, title, modal, arg);
initComponents();
}
public HitDialog(Dialog parent) {
super(parent);
initComponents();
}
public HitDialog(Dialog parent, boolean modal) {
super(parent, modal);
initComponents();
}
public HitDialog(Dialog parent, String title) {
super(parent, title);
initComponents();
}
public HitDialog(Dialog parent, String title, boolean modal) {
super(parent, title, modal);
initComponents();
}
public HitDialog(Dialog parent, String title, boolean modal, GraphicsConfiguration arg) {
super(parent, title, modal, arg);
initComponents();
}
public HitDialog(Window parent) {
super(parent);
initComponents();
}
public HitDialog(Window parent, ModalityType modalityType) {
super(parent, modalityType);
initComponents();
}
public HitDialog(Window parent, String title) {
super(parent, title);
initComponents();
}
public HitDialog(Window parent, String title, ModalityType modalityType) {
super(parent, title, modalityType);
initComponents();
}
public HitDialog(Window parent, String title, ModalityType modalityType, GraphicsConfiguration arg) {
super(parent, title, modalityType, arg);
initComponents();
}
private void initComponents() {
setFont(new Font("Dialog", Font.PLAIN, 12));
setBackground(Color.white);
setForeground(Color.black);
setLayout(new GroupLayout());
add(getJButton0(), new Constraints(new Leading(121, 10, 10), new Leading(51, 10, 10)));
add(getJButton1(), new Constraints(new Leading(116, 10, 10), new Leading(112, 10, 10)));
setSize(320, 240);
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("flushTree");
jButton1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
jButton1MouseMouseClicked(event);
}
});
}
return jButton1;
}
private JButton getJButton0() {
if (jButton0 == null) {
jButton0 = new JButton();
jButton0.setText("Add");
jButton0.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
jButton0MouseMouseClicked(event);
}
});
}
return jButton0;
}
private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage());
}
}
/**
* Main entry of the class. Note: This class is only created so that you can
* easily preview the result at runtime. It is not expected to be managed by
* the designer. You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
HitDialog dialog = new HitDialog();
dialog.setDefaultCloseOperation(HitDialog.DISPOSE_ON_CLOSE);
dialog.setTitle("HitDialog");
dialog.setLocationRelativeTo(null);
dialog.getContentPane().setPreferredSize(dialog.getSize());
dialog.pack();
dialog.setVisible(true);
}
});
}
private void jButton0MouseMouseClicked(MouseEvent event) {
FrameTree parent = (FrameTree) this.getParent();
parent.addNode("" + new Date());
}
private void jButton1MouseMouseClicked(MouseEvent event) {
FrameTree parent = (FrameTree) getParent();
DefaultMutableTreeNode node0 = new DefaultMutableTreeNode("DialogTree");
{
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("people");
{
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("man");
node1.add(node2);
}
{
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("women");
node1.add(node2);
}
node0.add(node1);
}
DefaultTreeModel model = new DefaultTreeModel(node0);
parent.flushTree(model);
}
}
[/code]