Java JFileChooser线程&;标签类
我正在创建一个解析器,这就是我使用JFileChooser的原因。 当我使用JFileChooser选择一个文件时,我希望有一个JLabel,上面写着:“正在解析”或类似的smth。 当它完成时:“解析完成” (我的第一个目标是使用进度条,但现在对我来说有点复杂) 类将获取文件数组并为每个文件创建可调用的文件。如果有5个文件:将调用5个线程。我使用Callable是因为我需要为每个线程返回数据字符串,并将其写入同一个csv文件中 嗯,当我单击JFileChooser上的Cancel时,JLabel会在正确的时刻正确显示,但是当我为解析函数选择files/file时,JLabel会等待调用的整个执行过程,然后出现“processing”(但当它已经结束时) 我无法在线程开始时显示处理 注意:我现在调用了CardLayout,但它还没有被使用 这是我的密码:Java JFileChooser线程&;标签类,java,jlabel,jfilechooser,callable,Java,Jlabel,Jfilechooser,Callable,我正在创建一个解析器,这就是我使用JFileChooser的原因。 当我使用JFileChooser选择一个文件时,我希望有一个JLabel,上面写着:“正在解析”或类似的smth。 当它完成时:“解析完成” (我的第一个目标是使用进度条,但现在对我来说有点复杂) 类将获取文件数组并为每个文件创建可调用的文件。如果有5个文件:将调用5个线程。我使用Callable是因为我需要为每个线程返回数据字符串,并将其写入同一个csv文件中 嗯,当我单击JFileChooser上的Cancel时,JLabe
public class Main {
private static final String CARD_MAIN = "Card Main";
private static final String CARD_FILE = "Card File";
public static void main(String[] args) throws IOException {
createGUI();
}
public static void createGUI(){
// the JFrame
final JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setTitle("TMG Parser - Thales");
window.setSize(400, 100);
// the buttonPanel ( one to open JFileChooser & one to quit )
JPanel container = new JPanel();
JPanel buttonPanel = new JPanel();
final JButton fileButton = new JButton("Choose File");
fileButton.setBackground(Color.BLACK);
fileButton.setForeground(Color.WHITE);
final JButton quitButton = new JButton("Quit");
quitButton.setBackground(Color.RED);
quitButton.setForeground(Color.WHITE);
// adding buttons to panel
buttonPanel.add(fileButton);
buttonPanel.add(quitButton);
// the status label that says : processing or done
final JLabel status = new JLabel();
container.add(status);
fileButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser dialogue = new JFileChooser(new File("."));
dialogue.setMultiSelectionEnabled(true) ;
if (dialogue.showOpenDialog(null)==
JFileChooser.APPROVE_OPTION) {
status.setText("Processing");
File[] fichiers=dialogue.getSelectedFiles();
for( int i = 1; i<fichiers.length; ++i){
fichiers[i].getName();
fichiers[i].getAbsolutePath();
}
// calling my execution function (threads)
ReadFile readProgram = new ReadFile(fichiers);
}
else{status.setText("Action cancelled");}
}
});
quitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
window.add(container, BorderLayout.CENTER);
window.add(buttonPanel, BorderLayout.PAGE_END);
window.setVisible(true);
}
}
公共类主{
私有静态最终字符串CARD_MAIN=“CARD MAIN”;
私有静态最终字符串CARD_FILE=“CARD FILE”;
公共静态void main(字符串[]args)引发IOException{
createGUI();
}
公共静态void createGUI(){
//JFrame
最终JFrame窗口=新JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(空);
setTitle(“TMG解析器-泰雷兹”);
设置窗口大小(400100);
//按钮面板(一个打开JFileChooser,一个退出)
JPanel容器=新的JPanel();
JPanel buttonPanel=新的JPanel();
final JButton fileButton=新JButton(“选择文件”);
fileButton.setBackground(颜色:黑色);
fileButton.setForeground(颜色:白色);
最终JButton quitButton=新JButton(“退出”);
退步按钮。退步背景(颜色。红色);
quitButton.setForeground(颜色:白色);
//向面板添加按钮
buttonPanel.add(文件按钮);
按钮面板。添加(退出按钮);
//表示:正在处理或已完成的状态标签
最终JLabel状态=新JLabel();
容器。添加(状态);
addActionListener(新建ActionListener())
{
已执行的公共无效行动(行动事件ae)
{
JFileChooser对话=新建JFileChooser(新文件(“.”);
对话。setMultiSelectionEnabled(正确);
if(dialogue.showOpenDialog(null)=
JFileChooser.APPROVE\u选项){
status.setText(“处理”);
File[]fichiers=dialogue.getSelectedFiles();
因为(inti=1;i好,所以@tobias_k是对的!谢谢你,伙计
当我启动ReadFile时,这就冻结了我的swing线程,直到ReadFile完成
我将ReadFile更改为线程(并调用callables),现在它可以完美地工作。
谢谢!当线程运行时,UI是否有响应?或者它是否已冻结?您是否在设置文本后和启动线程之前尝试过手动刷新/重新验证标签?嘿@tobias_k,谢谢您的回答。UI在解析文件时似乎没有响应(当我按quit或想单击Choose File时:它等待第一个解析函数的结束,然后退出或打开新的JFileChooser)当您说:手动重新刷新/revalidate时,您的意思是在status.setText(“”)之后调用status.revalidate();.repaint()?如果是,则完成了:相同的问题似乎是您使用这些线程的方式有问题。也许您应该在ReadFile
类中发布(部分)代码。@tobias_k我已上载了我的ReadFile&Task代码。试图澄清:在Main上,我得到了文件[],我的ReadFile为每个文件启动可调用任务。在任务文件>中,我为一个任务启动了2个可调用任务(1个用于获取特定元,1个用于获取文件中的特定游标),因此对于每个文件,我有2个线程>一个用于游标,一个用于元。返回字符串。我为任务返回字符串数组