我有一个简短的应用程序,我遇到了组合框的问题。 应用程序:用户可以从下拉菜单中选择文件夹或创建文件夹(在我的应用程序的主文件夹中)。
我需要什么:用户选择或创建此文件夹后。路径存储在变量中,其余的组合框将显示该新路径中的文件。
我没有使用GUI和Swing,这是我的代码:
public class test {
private JFrame MyFrame;
boolean pathChanged = false;
String xPath = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.MyFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
@SuppressWarnings("unchecked")
private void initialize() {
MyFrame = new JFrame();
MyFrame.setTitle("My Application");
MyFrame.setBounds(100, 100, 500, 400);
MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyFrame.getContentPane().setLayout(null);
//If it's a directory it will be displayed in the main dropdown menu
File fileBtn1 = new File("C:\\ProgramManagement\\");
String[] directories = fileBtn1.list(new FilenameFilter() {
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
});
JComboBox<Object> comboBox = new JComboBox<Object>(directories);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Changes top label to name of the program selected
lblProgramName.setText((String) comboBox.getSelectedItem());
//Changes path name to the name of the selected program
xPath = (String) comboBox.getSelectedItem();
pathChanged = true; // Tells comboBoxes the path has changed
System.out.println(xPath); // Just testing path name
}
});
comboBox.setBounds(315, 47, 150, 25);
MyFrame.getContentPane().add(comboBox);
//comboBox_1 and comboBox_2 need to update depending on the xPath variable
JComboBox comboBox_1 = new JComboBox<String>();
comboBox_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
//comboBox_1.setSelectedIndex(0);
/*
* Updating first ComboBox
*/
if(pathChanged){
System.out.println("PATH HAS CHANGED");
System.out.println("NEW PATH IS " + xPath);
File fileStep1 = new File("C:\\ProgramManagement\\" + xPath + "\\step1");
String[] directory1 = fileStep1.list(new FilenameFilter() {
public boolean accept(File current, String name) {
return new File(current, name).isFile();
}
});
comboBox_1.removeAllItems();
for (int i=0; i < directory1.length; i++ ){
String sample = directory1[i];
comboBox_1.addItem(sample);
}
comboBox_1.setBounds(315, 189, 150, 25);
MyFrame.getContentPane().add(comboBox_1);
}else{
System.out.println("Path NOT changed");
comboBox_1.setBounds(315, 189, 150, 25);
MyFrame.getContentPane().add(comboBox_1);
}
}
}
我试图把相关的代码放在这里。我认为我的问题是,一旦应用程序命中initialize(),就会生成组合框及其内容。我怎么能改变呢?非常感谢!
答案 0 :(得分:1)
您应该移动此代码:
if(pathChanged){
System.out.println("PATH HAS CHANGED");
System.out.println("NEW PATH IS " + xPath);
File fileStep1 = new File("C:\\ProgramManagement\\" + xPath + "\\step1");
String[] directory1 = fileStep1.list(new FilenameFilter() {
public boolean accept(File current, String name) {
return new File(current, name).isFile();
}
});
comboBox_1.removeAllItems();
for (int i=0; i < directory1.length; i++ ){
String sample = directory1[i];
comboBox_1.addItem(sample);
}
comboBox_1.setBounds(315, 189, 150, 25);
MyFrame.getContentPane().add(comboBox_1);
}else{
System.out.println("Path NOT changed");
comboBox_1.setBounds(315, 189, 150, 25);
MyFrame.getContentPane().add(comboBox_1);
}
进入这里:
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Changes top label to name of the program selected
lblProgramName.setText((String) comboBox.getSelectedItem());
//Changes path name to the name of the selected program
xPath = (String) comboBox.getSelectedItem();
pathChanged = true; // Tells comboBoxes the path has changed
System.out.println(xPath); // Just testing path name
//////////////////Move code here////////////////////////////////
}
});
您希望在更改第一个组合框时执行此代码... actionPerformed()