如何在JDialog中显示JTree?

时间:2017-12-04 16:22:45

标签: java swing jtree jdialog

我想在JTree中放置一个JDialog,以便用户可以选择树层次结构上的项目,然后单击确定按钮确认选择。用户可以通过单击CANCEL按钮取消选择。

在Java Swing应用程序中获取该对话框的最简单方法是什么?

1 个答案:

答案 0 :(得分:0)

试试这个: 首先像这样创建一个App

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class App {

    private JFrame frame;
    private JTextField textField;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    App window = new App();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public App() {
        initialize();
    }

    public void setValue(Object obj) {
        textField.setText(obj.toString());
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton btnOpen = new JButton("Open");
        btnOpen.setBounds(100, 57, 177, 43);
        btnOpen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new MyDialog(App.this).setVisible(true);
            }
        });
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(btnOpen);

        textField = new JTextField();
        textField.setBounds(100, 126, 177, 19);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
    }
}

然后在同一个包中创建MyDialog

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;

public class MyDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTree tree = null;


    public MyDialog(App ref) {
        setModalityType(ModalityType.APPLICATION_MODAL);
        setTitle("MyDialog");
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new BorderLayout(0, 0));

        {
            tree = new JTree();
            contentPanel.add(tree);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                        if (node == null)
                            return;
                        Object userObject = node.getUserObject();

                        ref.setValue(userObject);
                        MyDialog.this.dispose();
                    }
                });
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        MyDialog.this.dispose();
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }
}

应用程序将使用树来删除对话框。如果您选择一个树节点并点击" ok"它将关闭对话框并在App的文本字段中显示该值。 "取消"将中止整个过程。它对我有用。

希望有所帮助