chooser = new JFileChooser();
chooser.setSize(300, 200);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
.......
}
This doesn't work.
Always opens in default size.
Try chooser.setPreferredSize(new Dimension(300, 200))
Maybe a solution would be to extend the JFileChooser class and overload the constructor with the new setSize method. Not sure if this will work. I know you can extend the very basic JDialog to create custom dialogs and calling setSize there worked for me last time I tried it.
Related
I would like to make it so that when you click the X close button on the top right, a new JFrame will appear and the current one will close.
How would I be able to do it?
The question is, can another JFrame open automatically when one JFrame closes. As noted in comments, two possible common solutions include use of a WindowListener or use of a modal dialog (one example of which is as mentioned by Chance Hoard, a JOptionPane, which again is one solution, but certainly not the only solution).
For a WindowListener to work, you can open the new JFrame in the windowClosed() method, for example:
JFrame frame1 = new JFrame("Frame 1");
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.add(Box.createRigidArea(new Dimension(500, 400)));
frame1.pack();
frame1.setLocationByPlatform(true);
frame1.setVisible(true);
frame1.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
JFrame frame2 = new JFrame("Frame 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(Box.createRigidArea(new Dimension(500, 400)));
frame2.pack();
frame2.setLocationByPlatform(true);
frame2.setVisible(true);
}
});
The key here is to set the default close operation to not be JFrame.EXIT_ON_CLOSE so as not to close down the JVM after the initial JFrame window has closed. In the window listener's windowClosed() method, create and display the 2nd JFrame.
To solve this using a modal dialog, I would create the second window, which could be a JFrame, first, to give the dialog an parent application, but then I would write code to display it after displaying the modal dialog. This way, it only shows up once the dialog is no longer visible:
JFrame frame3 = new JFrame("Frame 3");
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.add(Box.createRigidArea(new Dimension(500, 400)));
frame3.pack();
frame3.setLocationByPlatform(true);
// don't display this JFrame yet
// create the dialog, passing in the JFrame and making the dialog "modal"
JDialog dialog1 = new JDialog(frame3, "Dialog 1", ModalityType.APPLICATION_MODAL);
dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog1.add(Box.createRigidArea(new Dimension(500, 400)));
dialog1.pack();
dialog1.setLocationRelativeTo(frame3);
// show the dialog first
dialog1.setVisible(true);
// this is called only after the dialog is no longer visible
frame3.setVisible(true);
Having said all this, I still recommend that you read the The Use of Multiple JFrames, Good/Bad Practice? link and I recommend avoiding both of the above "solutions" and instead recommend that one create and display a single main application window, a JFrame, and instead swap GUI views, usually JPanels, by using a CardLayout.
I've got a JPanel, which I want to respond to a mouse click and then open a JDialog. The JDialog constructor needs an instance of JFrame and not JPanel - how do I work around this?
You should really try to attach the JDialog to a parent Dialog or Frame, especially if you want it modal (by passing a parent Window, the dialog will be attached to your Window and bringing the parent will bring the child dialog as well). Otherwise, the user experience can really go wrong: lost dialogs, blocking windows without seeing the modal dialog, etc...
To find your JPanel parent Window, all you need is this code:
JPanel panel = new JPanel();
Window parentWindow = SwingUtilities.windowForComponent(panel);
// or pass 'this' if you are inside the panel
Frame parentFrame = null;
if (parentWindow instanceof Frame) {
parentFrame = (Frame)parentWindow;
}
JDialog dialog = new JDialog(parentFrame);
...
If you don't know if you are in a Frame or Dialog, make the "instanceof" test for both classes.
Using the parameter free constructor will make the dialog owner-less. I think that the best thing to do would be to make the Frame that owns your Panel the owner of the dialog.
By that, I mean that you should use the getParent() from your JPanel to find its owner and then send this object found as the owner of your JFrame.
A crude code for that would be
java.awt.Container c = myPanel.getParent();
while (!(c instanceof javax.swing.JFrame) && (c!=null)) {
c = c.getParent();
}
if (c!=null) {
JFrame owner=(javax.swing.JFrame) c;
JDialog myDialog=new JDialog(owner);
}
I have not tested this code, but it is good enought for you to understand the idea.
If you decided to go with a JOptionPane, you could add a MouseListener to the JPanel with a mouseAdapter inner class to handle mouseClicked events. You would have to declare the panel final in order to access the panel from within the inner class.
final JPanel testPanel = new JPanel();
testPanel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
JOptionPane.showMessageDialog(testPanel,"Title","InformationMessage",JOptionPane.INFORMATION_MESSAGE);
}});//end of mouseClicked method
There's a constructor that don't need argument:
JDialog dialog = new JDialog();
If what you want is to make the dialog modal, maybe you can get a static reference of you main JFrame, something like:
JDialog dialog = new JDialog(MyMainJFrame.getInstance());
I am trying to put a JFileChooser box on my GUI but if I just do this
JFileChooser filechooser = new JFileChooser ();
then it will just show a huge file selection window on the panel (I do not want that), so I want to make a small box filechooser (with a name for example "choose file") that when I click it, a window will popup, so then I can choose the file.
Use a button to open your file chooser and use setPreferredSize() method to make the file chooser smaller in size:
JButton button = new JButton("Choose a file!");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle( "Choose a file" );
fileChooser.setVisible( true );
fileChooser.setPreferredSize( new Dimension(100, 100) );
}
});
Call
filechooser.setPreferredSize (new java.awt.Dimension (800, 800));
before calling showOpenDialog with whatever Dimension you like.
But I would suggest to either maximize the Dialog, because in the moment I like to open a File, I don't like to watch something else - find a file, and close the dialog, without much scrolling, because somebody thought it looks more nice.
If you like to prevent wasting space, you can precalculate the needed size for the window, which might be a lot of work, but could pay off, if you use the Component frequently.
Is there a way to disable all X on dialog boxes?
I can accomplish this on one by doing:
JOptionPane pane = new JOptionPane("Are you hungry?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
JDialog dialog = pane.createDialog("Title");
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
} });
dialog.setContentPane(pane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
But the problem is thst I have to apply this to other 20-40 dialog boxes which will be time consuming..
So I was wondering just how you can do one line of code to make the fonts bigger on all the Dialog boxes (shown below), is there a way to for the X disable feature.
UIManager.put("OptionPane.messageFont",
new FontUIResource(new
Font("ARIAL",Font.BOLD,30)));
JDialog method setDefaultCloseOperation works just like JFrame's:
http://download.oracle.com/javase/6/docs/api/javax/swing/JDialog.html#setDefaultCloseOperation(int)
Neither will do this for all frames, it will just do it for the instance it is set on. If you want to apply to all your dialogs you need to do one of the following:
Call this every time you construct a dialog.
Extend JDialog and have it set the default for you, then use that new class.
Create a factory method that will construct a dialog with this default set. Then call this method whenever you need a new dialog.
You need to add a windowListener but also set the default close operation for the dialog.
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
if (someConditionIsMet) {
dispose();
}
}
});
Rather than disable a control, you should remove it. Users shouldn't see an "X" button and then have it not behave like they expect.
See:
Dialog.setUndecorated() for a single dialog
JDialog.setDefaultLookAndFeelDecorated() to provide a hint for future dialogs
Why not just create a custom class that extends the JDialog setting your desired close behavior in the constructor?
class NoCLoseDialog extends JDialog {
NoCloseDialog(){
addWindowListener(new WindowAdaptor(){
// ... Close behavior ...
});
}
}
Crtl+F Replace JDialog with NoCloseDialog ...
I wonder if it is possible to get hold of a reference to the (JDialog?) object created by one of those static methods of JOptionPane (e.g. showMessageDialog)? I intend to modify the position where the dialog appears on the screen. More specifically, I want the dialog to appear at the top-left corner of the main app window, instead of the centre of the window by default. So having a reference to the object would enable me to use setLocation to achieve the desired effect...
Any suggestion would be appreciated! Thanks!
The static showXXXDialog() methods are just for convenience. If you look at the source code for JOptionPane, you'll find that in actuality, a JOptionPane object is created based on the options you specify and then JOptionPane.createDialog(...) is called. One method to show your message dialog at a different position is:
JOptionPane pane = new JOptionPane("Message", JOptionPane.WARNING_MESSAGE,
JOptionPane.DEFAULT_OPTION);
JDialog dialog = pane.createDialog("TITLE");
dialog.setLocation(0, 0);
dialog.setVisible(true);
// dialog box shown here
dialog.dispose();
Object selection = pane.getValue();
With a combination of parameters to the JOptionPane constructor, and JOptionPane set methods, you can do anything you would have done with the static methods, plus you have access to the JDialog object itself.
EDITED: (to add example of input dialog for OP)
JOptionPane pane = new JOptionPane("Message", JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION, null, null, null);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(null, "Title");
dialog.setLocation(0, 0);
dialog.setVisible(true);
String str = (String) pane.getInputValue();
The JOptionPane will use the given parentComponent (first method parameter) to determine where to center the dialog (for example in javax.swing.JOptionPane.showMessageDialog(Component, Object))
You could try to pass in a fake component which positions the dialog to another location, for example like this:
JFrame frame = new JFrame("Test");
frame.setLocation(100, 100);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 'Invisible' fake component for positioning
JWindow c = new JWindow();
c.setSize(0, 0);
c.setVisible(true);
Point location = frame.getLocation();
location.translate(200, 100);
c.setLocation(location);
JOptionPane.showInputDialog(c,"Foo");