1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
package Utilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File ;
//==============================================================================
/**
*
* <b>Positionable version of JFileChooser.</b><p>
*
* (The JFileChooser position isn't set or read by setLocation(...)
* or getLocation.)<p>
*
*
*<pre>
*Dear Mr. Kimball,
*
*This is a known bug:
*
* http://developer.java.sun.com/developer/bugParade/bugs/4390885.html
*
*We are working on a solution for a later release. In the meantime, the
*only workaround I can think of is to subclass JFileChooser and override
*the method showDialog(), adding a call to setLocation().
*
*See example code below.
*
*Regards,
*
*Leif Samuelsson
*
*Sun Microsystems, Inc.
*Java Swing Team
*</pre>
*/
//==============================================================================
public class FileChooser extends JFileChooser {
private JDialog dialog;
private int returnValue = ERROR_OPTION;
private Point Location = new Point(250, 250) ;
private Dimension Size = new Dimension( 600, 250) ;
//------------------------------------------------------------------------------
/**
*
* Constructor is the same as the no-argument version of JFileChooser.<p>
*
* Use setCurrentDirectory to set the initial directory.
*
*/
public FileChooser(){
super() ;
}
//------------------------------------------------------------------------------\/**
/** Replaces showDialog of JFileChooser */
public int showDialog(Component parent, String approveButtonText) {
if (approveButtonText != null) {
setApproveButtonText(approveButtonText);
setDialogType(CUSTOM_DIALOG);
}
Frame frame = parent instanceof Frame ? (Frame) parent
: (Frame)SwingUtilities.getAncestorOfClass(Frame.class, parent);
String title = getUI().getDialogTitle(this);
dialog = new JDialog(frame, title, true);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
returnValue = CANCEL_OPTION;
}
});
dialog.getContentPane().add(this, BorderLayout.CENTER);
dialog.pack();
dialog.setLocation(Location) ;
dialog.setSize(Size) ;
rescanCurrentDirectory();
returnValue = ERROR_OPTION;
dialog.show();
Location = dialog.getLocation() ;
Size = dialog.getSize() ;
return returnValue;
}
//------------------------------------------------------------------------------\/**
/** Replaces approveSelection of JFileChooser */
public void approveSelection() {
returnValue = APPROVE_OPTION;
if (dialog != null) {
dialog.setVisible(false);
}
super.approveSelection();
}
//------------------------------------------------------------------------------\/**
/** Replaces cancelSelection of JFileChooser */
public void cancelSelection() {
returnValue = CANCEL_OPTION;
if (dialog != null) {
dialog.setVisible(false);
}
super.cancelSelection();
}
//------------------------------------------------------------------------------\/**
/** Sets the location of the FileChooser */
public void setLocation(Point P) {
this.Location = P ;
}
//------------------------------------------------------------------------------\/**
/** Sets the size of the FileChooser */
public void setSize(Dimension D) {
this.Size = D ;
}
//------------------------------------------------------------------------------\/**
/** Returns the location of the FileChooser */
public Point getLocation() {
return Location ;
}
//------------------------------------------------------------------------------\/**
/** Returns the size of the FileChooser */
public Dimension getSize() {
return Size ;
}
//------------------------------------------------------------------------------\/**
/** Test program provided by Leif Samuelsson */
public static void main(String[] s) throws Exception {
JFrame frame = new JFrame("FileChooser test");
frame.setVisible(true);
JFileChooser chooser = new FileChooser();
chooser.setLocation(new Point(200,200)) ;
int returnVal = chooser.showOpenDialog(frame);
System.out.println("Position: " + chooser.getLocation()) ;
switch (returnVal) {
case APPROVE_OPTION:
System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
break;
case CANCEL_OPTION:
System.out.println("Cancelled");
break;
case ERROR_OPTION:
System.out.println("An error occurred");
break;
}
System.exit(0);
}
}
//==============================================================================
|