gpt4 book ai didi

java - 通过 jFileChoser 添加文件到 jList

转载 作者:行者123 更新时间:2023-11-29 03:49:08 24 4
gpt4 key购买 nike

由于我是 java swing 的新手,我发现在将 JFileChooser 与 JList 集成时遇到了一些困难。我的目标是从对话框(JFileChooser)中选择一个文件并单击“添加”,以便它自动添加到 JList 中,并使用与“删除”相同的机制。我试着浏览了一些教程和一些提示,但它没有用。如果你们中的任何人能帮助我完成这一步,那就太好了。提前致谢..!!

    package examples;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JSplitPane;
//import javax.swing.SwingConstants;
class SplitPane extends JFrame
{

private static final long serialVersionUID = 1L;
private JSplitPane splitPaneV;
private JSplitPane splitPaneH;
private JLayeredPane panel1;
private JPanel panel2;
private JPanel panel3;
private JButton add;
private JButton remove;
private JScrollBar scrollBar;
private JList list;


public SplitPane()
{
setTitle("AdditionalLoaderInformation");
setBackground(Color.blue);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
topPanel.setPreferredSize(new Dimension(700, 500));
getContentPane().add(topPanel);

// Create the panels
createPanel1();
createPanel2();
createPanel3();

// Create a splitter pane
splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
topPanel.add(splitPaneV, BorderLayout.CENTER);
splitPaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPaneH.setLeftComponent(panel1);
splitPaneH.setRightComponent(panel2);
splitPaneV.setLeftComponent(splitPaneH);
splitPaneV.setRightComponent(panel3);

scrollBar = new JScrollBar();
scrollBar.setOrientation(JScrollBar.HORIZONTAL);
panel3.add(scrollBar, BorderLayout.SOUTH);


list = new JList();
panel3.add(list, BorderLayout.CENTER);
}
public void createPanel1()
{
panel1 = new JLayeredPane();
panel1.setLayout(new BorderLayout());

}


public void createPanel2()
{
panel2 = new JPanel();

add = new JButton("ADD");
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
************************************

}
});


panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel2.add(add);

remove = new JButton("REMOVE");
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeActionPerformed(e);
}

private void removeActionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
});
panel2.add(remove);



}

public void createPanel3()
{
panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.setPreferredSize(new Dimension(400, 100));
panel3.setMinimumSize(new Dimension(100, 50));
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
//fileChooser.showOpenDialog(fileChooser);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser .setDialogTitle("OPEN");
panel3.add(fileChooser, BorderLayout.NORTH);

//fileChooser.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent e)
//{




// }
//});
}

public static void main(String args[]) {
// Create an instance of the test application
SplitPane mainFrame = new SplitPane();
mainFrame.pack();
mainFrame.setVisible(true);
}
}

最佳答案

当您在选择器的 Action 监听器中获得新文件名时,显示为 here , 将其添加到(或从中删除)列表的模型,如本 example 所示.

附录:要在JList 中显示文件的内容,您需要创建一个合适的renderer。使用 text components 之一.

关于java - 通过 jFileChoser 添加文件到 jList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556830/

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