我正在尝试在JFileChooser上将鼠标光标捕捉到批准按钮上,但我无法在之前的任何地方找到任何示例。我尝试过使用硬编码的x,y位置但是当我在不同的PC上运行我的应用程序时,这是没用的。任何帮助将不胜感激,我的代码如下:
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;
}
}
感谢你。
答案 0 :(得分:2)
由于对话框的模态状态,它可能有点棘手,但使用WindowListener
和java.awt.Robot
可以实现
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;
}
}
}
接下来的问题是......你为什么要弄乱我的鼠标?