gpt4 book ai didi

java - 在 JFileChooser 上使用 JComboBox 列出以组合框第一行文本开头的所有文件

转载 作者:行者123 更新时间:2023-12-01 13:46:23 26 4
gpt4 key购买 nike

我设法在 JFileChooser 上获得删除按钮。我弄清楚了按钮应该放在哪里。在此过程中,我通过弄乱 getComponents() 方法发现,在其中搜索文件的 JTextField 并将其替换为 JComboBox。计划是让它列出以第一行中的文本开头的所有文件,这是唯一可以在 JComboBox 上编辑的文件,否则它是不可编辑的,并且可以选择另一行,它将把第一行的项目文本设置为该项目的文本。 (并且还更新了JCombobBox,尽管我认为我还没有实现该部分,尽管一个简单的方法调用应该可以做到这一点,但是,无论如何,我在Java编程论坛上尝试了posting这个。

它显示了其中的项目,但不让它随着键入而更新。相反,它显示目录中的所有项目。另外,当我选择一个项目时,它会删除第一行并将第一行的文本设置为该项目。然而,它现在使第一行无法编辑,我认为或者做了一些古怪的事情。

它删除了第一行,因为开发 JComboBox 类的杰出人士懒得创建 setItmeAt(Object item, int index) 方法,而只创建 getItemAt(int索引)。因此,我必须获取该项目的文本,将其放入变量中,删除第一行的项目,然后在第一行读取包含所选项目文本的项目。

我担心我的代码可能太长,所以我把它缩短了,尽管今天这样做在 Java 编程论坛上没有得到任何结果,因为问题出在 JComboBox 和文件上类和东西。

package addressbook.gui;

import javax.swing.JFileChooser;
import java.io.File;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.MutableComboBoxModel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicComboBoxEditor;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.plaf.basic.BasicComboPopup;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class FileChooserWithDelete extends JFileChooser
{

private String textFieldString;
private JIntelligentComboBox comboBox;
private DefaultComboBoxModel dcm;

public FileChooserWithDelete()
{
super("./");
dcm = new DefaultComboBoxModel();


java.io.File f = getCurrentDirectory();

java.io.File[] files = f.listFiles();

for (int i =0; i < files.length; i++)
{

dcm.addElement(new Object[] {files[i].getName(), "", 0});

}


JButton delete = new JButton("Delete");
delete.setToolTipText("Delete file");

comboBox = new JIntelligentComboBox(dcm);


addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
JFileChooser chooser = (JFileChooser) evt.getSource();
java.io.File oldDir = (java.io.File) evt.getOldValue();
java.io.File newDir = (java.io.File) evt.getNewValue();

java.io.File curDir = chooser.getCurrentDirectory();
System.out.println(curDir.getName());


dcm.removeAllElements();



java.io.File[] moreFiles = curDir.listFiles();

System.out.println("Obama is a loser!");


for (int i =0; i < moreFiles.length; i++)
{



dcm.addElement(new Object[] {moreFiles[i].getName(), "", 0});


}


comboBox.init();


}
}
});




java.awt.Container cont = (java.awt.Container) (getComponents()[3]);

java.awt.Container cont2 = (java.awt.Container) (cont.getComponents()[3]);
java.awt.Container cont3 = (java.awt.Container) (cont.getComponents()[0]);

cont3.remove(1);

cont3.add(comboBox, 1);





delete.addActionListener(
new ActionListener()
{




public void actionPerformed(ActionEvent e)
{

File f= getSelectedFile();

java.awt.Container cont = (java.awt.Container) (getComponents()[3]);

java.awt.Container cont2 = (java.awt.Container) (cont.getComponents()[3]);
java.awt.Container cont3 = (java.awt.Container) (cont.getComponents()[0]);
//javax.swing.JTextField jtf = (javax.swing.JTextField) (cont3.getComponents()[1]);

String text = (String) comboBox.getItemAt(0);

if (f == null)
f = new File("./" + text);



int option = JOptionPane.showConfirmDialog(null, "Are you sure you wnat to delete?", "Delete file " + f.getName() + "?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if (option == JOptionPane.YES_OPTION)
{

if (!f.exists() || text == null)
{
JOptionPane.showMessageDialog(null, "File doesn't exist.", "Could not find file.", JOptionPane.ERROR_MESSAGE);
cancelSelection();

}

else
{
f.delete();

cancelSelection();
}





}
}});



cont2.setLayout(new java.awt.FlowLayout());


//((java.awt.Container) (getComponents()[3])).add(delete);
cont2.add(delete);

//cont2.add(delete);





}




protected class JIntelligentComboBox extends JComboBox {

private List<Object> itemBackup = new ArrayList<Object>();

public JIntelligentComboBox(MutableComboBoxModel aModel) {
super(aModel);
init();
}

private void init() {
this.setRenderer(new searchRenderer());
this.setEditor(new searchComboBoxEditor());
this.setEditable(true);
int size = this.getModel().getSize();
Object[] tmp = new Object[this.getModel().getSize()];
for (int i = 0; i < size; i++) {
tmp[i] = this.getModel().getElementAt(i);
itemBackup.add(tmp[i]);
}
this.removeAllItems();
this.getModel().addElement(new Object[]{"", "", 0});
for (int i = 0; i < tmp.length; i++) {
this.getModel().addElement(tmp[i]);
}
final JTextField jtf = (JTextField) this.getEditor().getEditorComponent();
jtf.addKeyListener(
new KeyAdapter() {

@Override
public void keyReleased(KeyEvent e) {
searchAndListEntries(jtf.getText());
}
});
}

@Override
public MutableComboBoxModel getModel() {
return (MutableComboBoxModel) super.getModel();
}

private void searchAndListEntries(Object searchFor) {
List<Object> found = new ArrayList<Object>();
for (int i = 0; i < this.itemBackup.size(); i++) {
Object tmp = this.itemBackup.get(i);
if (tmp == null || searchFor == null) {
continue;
}
Object[] o = (Object[]) tmp;
String s = (String) o[0];

/*
if (s.matches("(?i).*" + searchFor + ".*")) {
found.add(new Object[]{
((Object[]) tmp)[0], searchFor, ((Object[]) tmp)[2]
});
}
}

*/

if (s.startsWith((String) searchFor))
found.add(new Object[] {((Object[]) tmp) [0], searchFor, ((Object[]) tmp) [2]});}

this.removeAllItems();
this.getModel().addElement(new Object[]{searchFor, searchFor, 0});
for (int i = 0; i < found.size(); i++) {
this.getModel().addElement(found.get(i));
}
this.setPopupVisible(true);
// http://stackoverflow.com/questions/7605995
BasicComboPopup popup =
(BasicComboPopup) this.getAccessibleContext().getAccessibleChild(0);
Window popupWindow = SwingUtilities.windowForComponent(popup);
Window comboWindow = SwingUtilities.windowForComponent(this);

if (comboWindow.equals(popupWindow)) {
Component c = popup.getParent();
Dimension d = c.getPreferredSize();
c.setPreferredSize(d);

}
else {
popupWindow.pack();
}
}

class searchRenderer extends BasicComboBoxRenderer {

@Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (index == 0) {
setText("");
return this;
}
Object[] v = (Object[]) value;
String s = (String) v[0];
String lowerS = s.toLowerCase();
String sf = (String) v[1];
String lowerSf = sf.toLowerCase();
List<String> notMatching = new ArrayList<String>();

if (!sf.equals("")) {
int fs = -1;
int lastFs = 0;
while ((fs = lowerS.indexOf(lowerSf, (lastFs == 0) ? -1 : lastFs)) > -1) {
notMatching.add(s.substring(lastFs, fs));
lastFs = fs + sf.length();
}
notMatching.add(s.substring(lastFs));
}
String html = "";
if (notMatching.size() > 1) {
html = notMatching.get(0);
int start = html.length();
int sfl = sf.length();
for (int i = 1; i < notMatching.size(); i++) {
String t = notMatching.get(i);
html += "<b style=\"color: black;\">"
+ s.substring(start, start + sfl) + "</b>" + t;
start += sfl + t.length();
}
}
this.setText("<html><head></head><body style=\"color: gray;\">"
+ html + "</body></head>");
return this;
}
}

class searchComboBoxEditor extends BasicComboBoxEditor {

public searchComboBoxEditor() {
super();
}

@Override
public void setItem(Object anObject) {
if (anObject == null) {
super.setItem(anObject);
}


else {
Object[] o = (Object[]) anObject;
super.setItem(o[0]);
}


}

@Override
public Object getItem() {
return new Object[]{super.getItem(), super.getItem(), 0};
}
}
}

public JTextField getComboBoxTextField()
{

final JTextField jtf = (JTextField) comboBox.getEditor().getEditorComponent();

return jtf;

}

public static void main(String[] args)
{

FileChooserWithDelete fcwd = new FileChooserWithDelete();

fcwd.showOpenDialog(null);


}
}

Java 编程论坛页面的链接是 here .

最佳答案

您也许能够调整所检查的方法here 。对于通知,您可以利用现有的 JFileChooser 事件,如 here 所示。 。或者,您可以定义自己的 PropertyChangeEvent,如 here 所示。 .

关于java - 在 JFileChooser 上使用 JComboBox 列出以组合框第一行文本开头的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343177/

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