How the JOptionPane works

Posted by DevAno1 on Stack Overflow See other posts from Stack Overflow or by DevAno1
Published on 2010-05-29T02:11:36Z Indexed on 2010/05/29 2:12 UTC
Read the original article Hit count: 307

Filed under:
|
|
|

How can I control what happens with window after clicking JOPtionPane buttons ? I'm trying to implement simple file chooser. In my frame I have 3 buttons (OK, Cancel, Browse). Browse button opens file search window, and after picking files should return to main frame. Clicking OK will open a frame with the content of the file. Now porblem looks this way. With the code below, I can choose file but directly after that a new frame is created, and my frame with buttons dissapears :
alt text
alt text

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.awt.*;
import javax.swing.*;
import java.io.*;   

public class Main {

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                show("Window");
            }
        });
    }

    public static void show(String frame_name){
        JFrame frame = new JFrame(frame_name);
        frame.setPreferredSize(new Dimension(450, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel top = new JPanel();
        top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));

        JFileChooser fc = new JFileChooser(new File("."));

        JPanel creator = new JPanel();
        creator.setLayout(new BoxLayout(creator, BoxLayout.Y_AXIS));
        creator.add(top);

        String[] buttons = {"OK", "Cancel", "Browse"};

        int rc = JOptionPane.showOptionDialog(
                   null,
                   creator,
                   frame_name,
                   JOptionPane.DEFAULT_OPTION,
                   JOptionPane.PLAIN_MESSAGE,
                   null,
                   buttons,
                   buttons[0]
                 );

        String approveButt = "";

        switch(rc){
            case 0:
                break;
            case 1:
                break;
            case 2:
        approveButt = buttons[rc];
        int retVal = fc.showDialog(null, approveButt);
        if (retVal == JFileChooser.APPROVE_OPTION)
          System.out.println(approveButt + " " + fc.getSelectedFile());
                break;
        }   
        frame.pack();
    frame.setVisible(true);   
    }
}

With the second code I can return to my menu, but in no way I am able to pop this new frame, which appeared with first code. How to control this ? What am I missing ?

public class Main {

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                show("Window");
            }
        });
    }

    public static void show(String frame_name){
        JFrame frame = new JFrame(frame_name);
        frame.setPreferredSize(new Dimension(450, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel top = new JPanel();
        top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
        JFileChooser fc = new JFileChooser(new File("."));
        JPanel creator = new JPanel();
        creator.setLayout(new BoxLayout(creator, BoxLayout.Y_AXIS));
        creator.add(top);

        String[] buttons = {"OK", "Cancel", "Browse"};
        String approveButt = "";

        Plane m = null;

        int rc = -1;
        while (rc != 0) {
          rc = JOptionPane.showOptionDialog(
                       null,
                       creator,
                       frame_name,
                       JOptionPane.DEFAULT_OPTION,
                       JOptionPane.PLAIN_MESSAGE,
                       null,
                       buttons,
                       buttons[0]
                     );

        switch (rc) {
        case 0:
            m = new Plane();
        case 1:
            System.exit(0);
        case 2:
            approveButt = buttons[rc];
            int retVal = fc.showDialog(null, approveButt);
            if (retVal == JFileChooser.APPROVE_OPTION)
                System.out.println(approveButt + " " + fc.getSelectedFile());
            break;
        default:
            break;
        }
        }    
                addComponents(frame.getContentPane(), m);

        frame.pack();
        frame.setVisible(true);
    }

    private static void addComponents(Container c, Plane e) {
        c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
        c.add(e);
    }
}

class Plane extends JPanel {

    public Plane(){
    }

    @Override
    public void paint(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, 400, 250);
    }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about gui