views:

275

answers:

3

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);
    }

}
A: 

You could have your browse button display FileDialog, as seen in this example.

trashgod
A: 

With the code below, I can choose file but directly after that a new frame is created, and my frame with buttons dissapears :

Yes because as soon as you click a button on the JOptionPane the option pane is closed. I don't really understand what you are trying to do so I can't make a suggestion.

However, in general the design of your program is wrong. You should not be creating and displaying the option pane in the method that creates the GUI. Once this code is executed the user will never be able to select another file because there is no way to redisplay the option pane.

So maybe you need create your JFrame with a button like "Select File". You would then add a simple ActionListener to this button that displays the current option pane. That is you should start the display of you application with a permanent JFrame. Then you use menus and menu items to select a file. This is how most application work. Under the "File" menu you typically have an "Open" menu item. Clicking on the Open would cause a dialog to pop up with all the open options. Then when a file is selected, you display the contents of the file in the main JFrame.

camickr
+2  A: 

Using your code. Tried to make it straightforward :

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));

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

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

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

        int rc=-1;

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

            if( rc == 1){
                System.exit(0);
                break;
            }
            else if(rc == 2){
                int retVal = fc.showDialog(null, "Test");
                if (retVal == JFileChooser.APPROVE_OPTION)
                    System.out.println("File choose" + fc.getSelectedFile());
            }
        } while (rc != 0);

        if( rc == 0){
                frame.setVisible(true);
                frame.pack();
        }
    }
}
owca