gpt4 book ai didi

java - setSelectedFile(File file) 不在 JFileChooser 的 GUI 中呈现选择

转载 作者:行者123 更新时间:2023-11-30 08:07:01 26 4
gpt4 key购买 nike

以下代码会将“pom.xml”打印到控制台。但它不会在 GUI 中标记所选文件,就好像用户是用鼠标完成的一样。

那么如何以编程方式选择并突出显示所选文件?

JFileChooser fc = new JFileChooser();

FileFilter filter = new FileNameExtensionFilter("POM",
new String[] { "xml" });
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(filter);
fc.setCurrentDirectory(new File("./"));

fc.setSelectedFile(new File("pom.xml"));
System.out.println(fc.getSelectedFile());

fc.showOpenDialog(null);

代码如下:

enter image description here

但我想看到选择:

enter image description here

最佳答案

这是一个有趣的问题,它可能还取决于所使用的外观和感觉 (L&F)。我已经在 Windows 上使用 Metal L&F 进行了测试。 JFileChooser 类似乎无法正确处理最初选择的文件的视觉选择。

JFileChooser.ensureFileIsVisible 方法中,更改当前目录(如果需要)并调用 ensureFileIsVisible 方法。在我的案例中,此方法最终调用 sun.swing.FilePane.ensureFileIsVisible 方法,这似乎确保所选文件滚动到 JListJTable 用于显示文件。但是,当我查看调试器发生的情况时,文件列表在那一刻似乎未初始化并且未找到所选文件。否则,这将是在用户界面中选择它的最佳时机。

如果你真的想解决这个问题,也许最好修改一个 L&F 或创建一个新的,但这对我来说听起来工作量很大。下面的代码是我在调查问题时写的一个快速而肮脏的 hack,它似乎也解决了这个问题,但它显然还没有准备好生产,我不想将它用于任何重要的事情 - 在你的自担风险:

import java.awt.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;

public class SelectedFileChooser {
public static void main(String[] arguments) {
SwingUtilities.invokeLater(() -> new SelectedFileChooser().createAndShowGui());
}

private void createAndShowGui() {
JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
JButton openButton = new JButton("Test open");
panel.add(openButton);
frame.getContentPane().add(panel);

openButton.addActionListener(actionEvent -> testOpen());

frame.setVisible(true);
}

private void testOpen() {
String directory = "./.idea/";
String fileName = "compiler.xml";

JFileChooser fc = new JFileChooser();

FileFilter filter = new FileNameExtensionFilter("POM", "xml");
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(filter);
fc.setCurrentDirectory(new File(directory));

fc.setSelectedFile(new File(directory + fileName));
System.out.println(fc.getSelectedFile().getAbsolutePath());

createAndStartMonitorThread(fc, fileName);

fc.showOpenDialog(null);
}

private void createAndStartMonitorThread(JFileChooser fc, String fileName) {
Thread monitorThread = new Thread(){
private int counter;

@Override
public void run() {
while (counter < 20) {
System.out.println("Monitoring (" + counter + ")...");
monitorComponents(fc.getComponents(), "", fileName);
System.out.println("------");
try {
sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter++;
}
}
};

monitorThread.start();
}

private void monitorComponents(Component[] components, String prefix,
String fileName) {
for (Component component : components) {
if (component instanceof Container) {
Container container = (Container) component;
if (container.getComponentCount() > 0) {
monitorComponents(container.getComponents(), prefix + " ",
fileName);
}
}

if (component instanceof JList || component instanceof JTable) {
System.out.println(prefix + "[" + component.getClass().getName()
+ "] " + component);

if (component instanceof JList) {
selectInList((JList) component, fileName);
} else {
selectInTable((JTable) component, fileName);
}
}
}
}

private void selectInList(JList list, String fileName) {
int rowIndexToSelect = -1;
System.out.println("list size: " + list.getModel().getSize());
for (int rowIndex = 0; rowIndex < list.getModel().getSize(); rowIndex++) {
Object value = list.getModel().getElementAt(rowIndex);
System.out.println("Value at [" + rowIndex + ", 0] == " + value);
if (value.toString().endsWith(fileName)) {
rowIndexToSelect = rowIndex;
}
}

System.out.println("Before change - list selection: "
+ list.getSelectionModel().getMinSelectionIndex() + ".."
+ list.getSelectionModel().getMaxSelectionIndex());

if (rowIndexToSelect != -1) {
list.getSelectionModel().setSelectionInterval(rowIndexToSelect,
rowIndexToSelect);

System.out.println("After change - list selection: "
+ list.getSelectionModel().getMinSelectionIndex() + ".."
+ list.getSelectionModel().getMaxSelectionIndex());
}

System.out.println();
}

private void selectInTable(JTable table, String fileName) {
int rowIndexToSelect = -1;
System.out.println("table row count: " + table.getModel().getRowCount());
for (int rowIndex = 0; rowIndex < table.getModel().getRowCount(); rowIndex++) {
Object value = table.getModel().getValueAt(rowIndex, 0);
System.out.println("Value at [" + rowIndex + ", 0] == " + value);
if (value.toString().endsWith(fileName)) {
rowIndexToSelect = rowIndex;
}
}

System.out.println("Before change - table selection: "
+ table.getSelectionModel().getMinSelectionIndex() + ".."
+ table.getSelectionModel().getMaxSelectionIndex());

if (rowIndexToSelect != -1) {
table.getSelectionModel().setSelectionInterval(rowIndexToSelect,
rowIndexToSelect);

System.out.println("After change - table selection: "
+ table.getSelectionModel().getMinSelectionIndex() + ".."
+ table.getSelectionModel().getMaxSelectionIndex());
}

System.out.println();
}
}

关于java - setSelectedFile(File file) 不在 JFileChooser 的 GUI 中呈现选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33945713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com