簡體   English   中英

將JOptionPane鏈接到JFileChooser中

[英]Swing chaining JOptionPane into JFileChooser

我正在嘗試使用Swing進行文件傳輸通知。 這個想法是,當通過網絡向我的應用程序提供文件時,用戶會收到一個JOptionPane詢問他或她是否要接受所述文件提供,如果他們的回答是,我想打開JFileChooser以便他們可以瀏覽到他們要保存文件的位置。

我遇到的問題是,兩者都可以單獨正常工作,但是當我進行設置時, JOptionPane打開JFileChooser我的應用程序死鎖。

有人知道這里出了什么問題嗎? 我嘗試調試,但是沒有發現任何奇怪的行為來暗示為什么會死鎖。

編輯:下面的示例代碼似乎可以正常工作,使我相信問題可能出在另一個線程上。 更多詳情:

我正在使用應用程序線程,ApplicationLayer線程和“主”線程,主線程基於我定義的字符串生成字節數組,然后將生成的字節數組通過我的ApplicationLayer發送到我的應用程序中。

ApplicationLayer是可觀察的,Application是它的觀察者。

當ApplicationLayer收到所述字節數組時,它將其解析回字符串,並通知其觀察者已這樣做。

然后依次通知應用程序。 在我的應用程序中,我甚至注意到String是一個文件提供,並因此調用了'saveFile'方法,如下面的代碼所示。

編碼:

  package application;

    public class GUI extends JFrame implements ActionListener, ItemListener, Observer {

private JPanel cp = new JPanel();
private JPanel ulp = new JPanel();
private JTextField  myMessage;
private JTextArea   taMessages;
    // Menu Bar Elements
JMenuBar menuBar;
JMenu menu, submenu;

public GUI(){
    super();    
    this.setLayout(new BorderLayout());

    setPreferredSize(new Dimension(800, 600));
    setMinimumSize(new Dimension(800, 600));

    buildBarMenu();
    buildChatMenu();

    addWindowListener(new WindowAdapter() {
        public void windowClosing(final WindowEvent e) {
            e.getWindow().dispose();
        }
        public void windowClosed(final WindowEvent e) {
            System.exit(0);
        }
    }
            );

    pack();
    validate();
    setVisible(true);

}

    public static void main(final String[] args) {
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {
        // If Nimbus is not available, fall back to cross-platform
        try {
                     UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception ex) {

        }
    }
    new GUI();
    }
    private void buildBarMenu(){
    // Create the menu bar
    JMenuBar menuBar = new JMenuBar();

    // Create a menu
    JMenu menu = new JMenu("File");

    menu.setHorizontalTextPosition(SwingConstants.CENTER);
    menu.setVerticalTextPosition(SwingConstants.BOTTOM);

    menuBar.add(menu);

    // SendFile Item
    JMenuItem sendFileItem = new JMenuItem("Send File");
    sendFileItem.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            saveFile(); // Put whatever here
        }
    });
    // Exit item
    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {

            System.exit(0);
        }
    });
    menu.add(sendFileItem);
    menu.add(exitItem);

    this.setJMenuBar(menuBar);
    }

private void buildChatMenu() {

    this.add(cp, BorderLayout.CENTER);
    this.add(ulp, BorderLayout.EAST);

}

/**
 * Method to be called for saving files when a file transfer
 * request is received
 * @return the path to save the file to
 */
public String saveFile(){

    int choice = JOptionPane.showConfirmDialog(GUI.this, "You are being offered a file, accept?", "File Offer",
            JOptionPane.YES_NO_OPTION);

    if (choice == JOptionPane.YES_OPTION){
        System.out.println("yes");
        JFileChooser c = new JFileChooser();

        int rVal = c.showOpenDialog(GUI.this);
        if (rVal == JFileChooser.APPROVE_OPTION) {

        }
        if (rVal == JFileChooser.CANCEL_OPTION) {

        }

    }else{
        System.out.println("no");
    }
    return null;

}

public void save2(){
    JFileChooser c = new JFileChooser();


    int rVal = c.showOpenDialog(GUI.this);
    if (rVal == JFileChooser.APPROVE_OPTION) {
        System.exit(0);

    }
    if (rVal == JFileChooser.CANCEL_OPTION) {
        System.exit(0);

    }
}


@Override
public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}

    @Override
public void update(Observable o, Object arg) {


    if(arg instanceof ChatMessage){
        cp.addMessage(((ChatMessage) arg).getNickname(), ((ChatMessage) arg).getMessage());
    }
    else if(arg instanceof FileOfferMessage){

        cp.addMessage("FILE OFFER", ((FileOfferMessage) arg).getFileName() + " | File Size: " + ((FileOfferMessage) arg).getFileSize() + " bytes");
        saveFile();
    }


}


   }

使用Swing組件的任何代碼都必須在EventDispatchThread中運行。 您的main()方法應調用invokeLater並在傳遞的Runnable執行GUI操作(包括外觀部分)。

PS,當您使用它時, 是在關閉窗口時退出應用程序的首選方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM