问题
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.
回答1:
Because of the modal state of the dialog, it can be a little tricky, but with the use of a WindowListener
and java.awt.Robot, it can be achieved
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class FileChooserExample {
public static void main(String[] args) {
new FileChooserExample();
}
public FileChooserExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
FileOpenDialog dialog = new FileOpenDialog(0);
dialog.showOpenDialog(null);
}
});
}
class FileOpenDialog extends JFileChooser {
public String fileName;
public String dialogTitle;
public FileOpenDialog(int index) {
initComponent(index);
}
@Override
protected JDialog createDialog(Component parent) throws HeadlessException {
JDialog dialog = super.createDialog(parent);
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
JDialog dialog = (JDialog) e.getWindow();
JButton button = dialog.getRootPane().getDefaultButton();
Point pos = button.getLocationOnScreen();
Dimension size = button.getSize();
pos.x += (size.width / 2);
pos.y += (size.height / 2);
try {
Robot bot = new Robot();
bot.mouseMove(pos.x, pos.y);
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
return dialog;
}
private void initComponent(int index) {
setBackground(Color.lightGray);
setAcceptAllFileFilterUsed(false);
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;
}
}
}
The next question is...why are you messing with my mouse?
来源:https://stackoverflow.com/questions/24134978/how-do-you-put-the-mouse-cursor-over-the-open-button-on-a-jfilechooser