java - 在java中将字符串数组分配给组合框

标签 java arrays swing combobox

我想将dir数组分配给comboBox。我的代码有什么错误吗?我尝试显示它包含值的 dir 数组,但无法为其分配组合框。这是代码。

import java.awt.EventQueue;


public class ExpenseManager {

private JFrame frame;
private JTextField txtUserName;
private JLabel lblNewUserName; 
private JButton btnDone;
private JButton btnLogin;
private JComboBox<?> comboBox;
private String[] dir = new String[100];
private String[] hello = {"Hii", "Hello"};

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ExpenseManager window = new ExpenseManager();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ExpenseManager() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
//@SuppressWarnings("unchecked")
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewUser = new JButton("New User");
    btnNewUser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            lblNewUserName.setVisible(true);
            txtUserName.setVisible(true);
            btnDone.setVisible(true);
        }
    });
    btnNewUser.setBounds(23, 34, 89, 23);
    frame.getContentPane().add(btnNewUser);

    txtUserName = new JTextField();
    txtUserName.setBounds(240, 63, 134, 20);
    frame.getContentPane().add(txtUserName);
    txtUserName.setVisible(false);
    txtUserName.setColumns(10);

    lblNewUserName = new JLabel("Enter New UserName");
    lblNewUserName.setBounds(240, 38, 134, 14);
    lblNewUserName.setVisible(false);
    frame.getContentPane().add(lblNewUserName);

    btnDone = new JButton("Done");
    btnDone.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newUserName = txtUserName.getText();
            if(!newUserName.isEmpty())
            {
                File f = new File("S:/Expense/" + newUserName);
                if (f.exists() && f.isDirectory()) {
                   JOptionPane.showMessageDialog(null, "User already exists");
                   txtUserName.setText(null);
                }
                else{
                    boolean success = (new File("S:/Expense/" + newUserName)).mkdir();
                    if(!success){
                        JOptionPane.showMessageDialog(null, "Unable to register");
                    }else{
                        JOptionPane.showMessageDialog(null, "User registered Successfully");
                    }
                }
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Kindly enter User Name", "Invalid User Name", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    btnDone.setBounds(240, 103, 89, 23);
    btnDone.setVisible(false);
    frame.getContentPane().add(btnDone);

    btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            File[] directories = new File("S:/Expense/").listFiles();

            for(int i = 0; i < directories.length; i++)
            {
                dir[i] = directories[i].getName();
            }

            comboBox.setVisible(true);

        }
    });
    btnLogin.setBounds(23, 68, 89, 23);
    frame.getContentPane().add(btnLogin);

    comboBox = new JComboBox<Object>(dir);
    comboBox.setBounds(24, 138, 72, 20);
    comboBox.setSelectedIndex(1);
    comboBox.setVisible(false);
    frame.getContentPane().add(comboBox);

     }
  }

最佳答案

您可以使用setModel使用 DefaultComboBoxModel ,它采用一个数组,如下所示:

comboBox.setModel(new DefaultComboBoxModel(dir));

关于java - 在java中将字符串数组分配给组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44993759/

相关文章:

javascript - 拼接删除错误的索引

java - 使用特定目录的文件名填充 JComboBox

java - 在 JScrollPane 内的 JPanel 上绘画不会在正确的位置绘画

java - mod_jk1.2.32 的 JK Status Manager(status worker) 没有在 Web 界面上显示正确的 worker 状态

java - 转换字符串时 org.json.JSON.typeMismatch

java - 我不断收到 java.lang.ArrayIndexOutOfBoundsException : 500

java - 无法将 JOptionPane 设置为不可见然后再次恢复可见

c# - 从 Java 迁移到 C#

java - 使用 AutoValue 时无法将 JSON 转换为 ArrayList

ios - 将自定义坐标设置为函数,Waze Integration