I'm trying to snap my mouse cursor over the approve button by default on a JFileChooser but I cannot find any examples anywhere where this has been done before. I have tried using hard coded x,y positions but this is useless when I run my application on a different pc. Any help would be appreciated, my code is as follows:
FileOpenDialog fileChooser = new FileOpenDialog(index);
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
}
});
// Need to snap mouse cursor to OPEN button here somehow or within overidden
// method of showOpenDialog???
int returnVal = fileChooser.showOpenDialog(mainFrame);
System.out.println("Return Value is " + returnVal);
if (returnVal == FileOpenDialog.APPROVE_OPTION) {
setFileIndex(index);
setInputFile(fileChooser.getSelectedFile());
}
class FileOpenDialog extends JFileChooser {
public String fileName;
public String dialogTitle;
public FileOpenDialog(int index) {
initComponent(index);
}
private void initComponent(int index) {
setBackground(Color.lightGray);
setAcceptAllFileFilterUsed(false);
CustomFileFilter myFilter = new CustomFileFilter();
setFileFilter(myFilter);
switch (index) {
case 0:
setFileName("\\MelbCupHorses.txt");
setDialogTitle("Please Choose Horses File");
break;
case 1:
setFileName("\\MelbCupEntrants.txt");
setDialogTitle("Please Choose Employees File");
break;
}
System.out.println(getCurrentDirectory().toString() + fileName);
File file = new File(getCurrentDirectory().toString() + fileName);
setSelectedFile(file);
}
/**
* @return the dialogTitle
*/
@Override
public String getDialogTitle() {
return dialogTitle;
}
/**
* @param dialogTitle the dialogTitle to set
*/
@Override
public void setDialogTitle(String dialogTitle) {
this.dialogTitle = dialogTitle;
}
}
Thanking you.
Because of the modal state of the dialog, it can be a little tricky, but with the use of a
WindowListener
andjava.awt.Robot
, it can be achievedThe next question is...why are you messing with my mouse?