ggrace
asked on
OpenFileDialog Topmost in Java applet
I have written a simple applet in J++ to resize an image and save it to the local HD.
The image is selected using the OpenFileDialog, which works well the first time. Any further attempts, however , the OpenFileDialog loads but is not topmost.
I have used SetWindowPos in VB, but am not sure how to implement this in J++ ( and if it's possible).
Any ideas would be greatly appreciated.
Attached is a cut down version of the applet (minus the "save" feature).
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import com.ms.wfc.ui.OpenFileDial og;
import com.ms.wfc.ui.SaveFileDial og;
import com.ms.wfc.ui.DialogResult ;
import java.net.URL;
public class Resizer extends Applet implements ActionListener {
Image img, img2;
int dlgResult;
Button saveButton; // new variable of type Button
Button openButton; // new variable of type Button
String openfname;
OpenFileDialog ofd;
SaveFileDialog sfd;
public void init() {
setLayout(new BorderLayout()); // uses a Border layout for the components
Panel p = new Panel();
p.setLayout( new FlowLayout(FlowLayout.RIGH T));
openButton = new Button(" open "); // constructs a new instance of the Button class
p.add(openButton); // appends our button to the list of components in the applet
openButton.addActionListen er(this); // waits for button events
saveButton = new Button(" save "); // constructs a new instance of the Button class
p.add(saveButton); // appends our button to the list of components in the applet
saveButton.addActionListen er(this); // waits for button events
add(p, "North"); // adds the Panel to the right
}
public void paint(Graphics g) {
if ( openfname.length() > 0 )
{
// Draw the scaled version of the image
g.drawImage(img2, 0, 0, this);
}
}
public void actionPerformed(ActionEven t e) {
if (e.getSource() == openButton ) {
ofd = new OpenFileDialog();
ofd.setDefaultExt("jpg"); //sets the default extension
ofd.setFilter("Image files (*.jpg)|*.jpg|All files (*.*)|*.*"); //sets the file filter
ofd.setFilterIndex(1); //sets the filter that will be initially selected
ofd.setInitialDir("c:\\pro gram files"); //sets the initial directory
int dlgResult = ofd.showDialog();
openfname = ofd.getFileName();
openfname = openfname.replace('\\','/' );
openfname = "file:/" + openfname;
URL urlfname ;
try {
urlfname = new URL(openfname);
try
{
img = getImage(urlfname);
img2 = img.getScaledInstance(400, 300, Image.SCALE_SMOOTH);
}
catch (Exception e1){};
}catch (Exception e2) {};
repaint();
} // closes if
}
}
The image is selected using the OpenFileDialog, which works well the first time. Any further attempts, however , the OpenFileDialog loads but is not topmost.
I have used SetWindowPos in VB, but am not sure how to implement this in J++ ( and if it's possible).
Any ideas would be greatly appreciated.
Attached is a cut down version of the applet (minus the "save" feature).
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import com.ms.wfc.ui.OpenFileDial
import com.ms.wfc.ui.SaveFileDial
import com.ms.wfc.ui.DialogResult
import java.net.URL;
public class Resizer extends Applet implements ActionListener {
Image img, img2;
int dlgResult;
Button saveButton; // new variable of type Button
Button openButton; // new variable of type Button
String openfname;
OpenFileDialog ofd;
SaveFileDialog sfd;
public void init() {
setLayout(new BorderLayout()); // uses a Border layout for the components
Panel p = new Panel();
p.setLayout( new FlowLayout(FlowLayout.RIGH
openButton = new Button(" open "); // constructs a new instance of the Button class
p.add(openButton); // appends our button to the list of components in the applet
openButton.addActionListen
saveButton = new Button(" save "); // constructs a new instance of the Button class
p.add(saveButton); // appends our button to the list of components in the applet
saveButton.addActionListen
add(p, "North"); // adds the Panel to the right
}
public void paint(Graphics g) {
if ( openfname.length() > 0 )
{
// Draw the scaled version of the image
g.drawImage(img2, 0, 0, this);
}
}
public void actionPerformed(ActionEven
if (e.getSource() == openButton ) {
ofd = new OpenFileDialog();
ofd.setDefaultExt("jpg"); //sets the default extension
ofd.setFilter("Image files (*.jpg)|*.jpg|All files (*.*)|*.*"); //sets the file filter
ofd.setFilterIndex(1); //sets the filter that will be initially selected
ofd.setInitialDir("c:\\pro
int dlgResult = ofd.showDialog();
openfname = ofd.getFileName();
openfname = openfname.replace('\\','/'
openfname = "file:/" + openfname;
URL urlfname ;
try {
urlfname = new URL(openfname);
try
{
img = getImage(urlfname);
img2 = img.getScaledInstance(400,
}
catch (Exception e1){};
}catch (Exception e2) {};
repaint();
} // closes if
}
}
Hi ggrace,
instead of using thease
>>import com.ms.wfc.ui.OpenFileDial og;
>>import com.ms.wfc.ui.SaveFileDial og;
>>import com.ms.wfc.ui.DialogResult ;
why dont u go for FileDialog class in java? It works similar to the one which ur using here.
If u get object of FileDialog class and use
void setDirectory(String dir) ;
void setFile(String file) ;
void setFilenameFilter(Filename Filter filter) ;
void setMode(int mode) ;
methods u will get correct results. Using this will give u FileDialog all the time topmost.
instead of using thease
>>import com.ms.wfc.ui.OpenFileDial
>>import com.ms.wfc.ui.SaveFileDial
>>import com.ms.wfc.ui.DialogResult
why dont u go for FileDialog class in java? It works similar to the one which ur using here.
If u get object of FileDialog class and use
void setDirectory(String dir) ;
void setFile(String file) ;
void setFilenameFilter(Filename
void setMode(int mode) ;
methods u will get correct results. Using this will give u FileDialog all the time topmost.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thankyou very much. Works just fine now.
Is there no way then to set the modality of the mfc class?
ASKER