0

私はスイングが初めてだと言って始めたいと思います。JFileChooserを使用してディレクトリを選択する場合、私がやろうとしていることはすべてです。ディレクトリを開いてナビゲートし、選択できます。ダイアログを閉じるボタンを押すと問題が発生します。それを行うと、アプリケーションがフリーズします。ダイアログボックスが戻ってくるパネルだけがフリーズすると、白くなります。デバッガーをステップ実行すると、ダイアログが閉じた直後にハングが発生し、if ステートメントに到達しません。また、それが異なる場合は、Eclipseプラグイン内でこれを行っています。特に、ビュー内でホストされます。以下のコード:

public class TexturePacker extends ViewPart {
    public void createPartControl(Composite parent) {
        Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
        frame = SWT_AWT.new_Frame(composite);
        frame.add(new TexturePackerPanel(frame));
    }
}

public class TexturePackerPanel extends JPanel {
    //This is called from initialize(), which is called in the constructor
    private void initializeConfigPanel() {
        JPanel configPanel = new JPanel();
        JTextBoxk outputDirectory = new JTextField();
        configPanel.add(inputDirectory);
        JButton fileButton = new JButton("Folder");
        fileButton.addMouseListener(new MouseListener(){
            @Override
            public void mouseClicked(MouseEvent arg0) {
                JFileChooser file = new JFileChooser(outputDirectory.getText());
                file.setDialogTitle("Select Output Directory");
                file.setDialogType(JFileChooser.OPEN_DIALOG);
                file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int returnVal = file.showDialog(frame, "Choose");
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    outputDirectory.setText(file.getSelectedFile().getAbsolutePath());
                }
            }
            //Other blank MouseListener methods//       
        });
        configPanel.add(fileButton);
    }
}

システム情報:
Windows 8 64 ビット
Java 7
Eclipse 4.2 SR1 EE Edition

この問題は、Swing が Eclipse 内で正常に動作しないことが原因であると確信しています。SWT Directory Dialog を使用して正常に動作するようになりました。そこで、JPanel 全体を SWT に変換します。みんなの助けに感謝します。Swing がどのように機能するかについて、より多くのことを知ることができました。

4

1 に答える 1

0

このためには、MouseListenerの代わりにActionListenerを使用する必要があります。

それを除けば、これはスレッドの問題だと思います。スイングでの並行性に関するドキュメントを読む必要があります。

JFileChooserパーツをで囲みます

SwingUtilities.invokeLater(new Runnable() {
public void run() {
    \\Your JFileChooser bit
}

編集1:

次の少し編集されたコードを実行しましたが、問題を再現できません

public static void main(final String[] args){

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            final JPanel configPanel = new JPanel();
            final JButton fileButton = new JButton("Folder");
            final JFrame frame = new JFrame();
            frame.setVisible(true);
            frame.add(configPanel);
            //      JButton outputDirectory = new JButton("XX");
            fileButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(final ActionEvent arg0) {
                    final JFileChooser file = new JFileChooser();
                    file.setDialogTitle("Select Output Directory");
                    file.setDialogType(JFileChooser.OPEN_DIALOG);
                    file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

                    final int returnVal = file.showDialog(frame, "Choose");
                    if(returnVal == JFileChooser.APPROVE_OPTION) {
                        // outputDirectory.setText(file.getSelectedFile().getAbsolutePath());
                        System.out.println(returnVal);
                    }

                }
            });
            configPanel.add(fileButton);
        }
    });
}
于 2013-01-09T14:58:10.023 に答える