Pregunta

I am a student and have been given some source code for a minesweeper game. We have a few different GUI elements to add the way we want to. I am trying to use JTabbedPane. I have the actual game showing in one tab, now I am trying to implement a JFileChooser in another tab. I have made a class called userNames:

public userNames() {            
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
}

and just put in a simple showOpenDialog(null); to see if it works. In the main, where the tabs are, I have added the file chooser:

tp.addTab ("Saved", new userNames());

but this doesn't add it to the 'Saved' tab, it opens a whole new window. Can anyone tell me if what I am trying to do is even possible - add a file chooser inside a tab.

I hope I am explaining myself well enough. :-)

¿Fue útil?

Solución

You can create a JPanel inside of that tab, create a JFileChooser object and then add that object to panel you've created for that tab. So it should look like:

JPanel panel = ...
tp.addTab(title, panel);
JFileChooser fc;
panel.add(fc);

Otros consejos

in your usernames class you are creating a new fileChooser and opening the dialog, you aren't actually displaying it on a panel.

you could try something like

public userNames() extends JPanel {

    public userNames() {
       add(chooser);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top