2017-07-09 4 views
0

dir配列をcomboBoxに割り当てたいとします。私のコードにエラーはありますか?私はdir配列を表示しようとしましたが、値を含んでいますが、comboBoxを割り当てることはできません。ここにコードがあります。あなたが代わりのような配列を取るDefaultComboBoxModelsetModelを使用することができます文字列配列をコンボボックスのjavaに入れます

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); 

    } 
    } 
+0

1)JavaのGUIは異なるOS」、画面サイズ、画面の解像度などで仕事をしなければなりません異なるロケールで異なるPLAFを使用します。したがって、ピクセルの完全なレイアウトには役立ちません。代わりに、レイアウトマネージャや[それらの組み合わせ](http://stackoverflow.com/a/5630271/418556)と[空白](http://stackoverflow.com/a/17874718/)のレイアウトパディングとボーダーを使用してください。 418556)。 2) 'dir'配列は、アクションが検出された後にのみ読み込まれます。しかし、クラスが初期化されたとき(コンボボックスのコンストラクタとして使用されます(空のままでも)。 –

+0

Itemsを追加する前に 'comboBox.setVisible(true);'を使う理由と 'comboBox.setVisible(false);' afterをどのような目的のために使うのですか? –

答えて

1

comboBox.setModel(new DefaultComboBoxModel(dir)); 
+0

私はあなたの解決策を試しましたが、エラー –

+1

@AkshayNaikに何のエラーが出ますか? –

1

掲載コードのほとんどを尋ねた質問には関係ありませんので、削除する必要があります(MCVEを参照してください)。 レビュー以下、コメントの点に注意してください。レイアウトマネージャを使用して

public class ExpenseManager { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        new ExpenseManager(); 
       } catch (Exception e) { e.printStackTrace(); } 
      } 
     }); 
    } 

    public ExpenseManager() { initialize(); } 

    private void initialize() { 

     JFrame frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); //better use a layout manger 

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

     JButton btn = new JButton("Populate combo"); 
     btn.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 

       //populate combo 
       String[] dir = new String[5]; 

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

       comboBox.setModel(new DefaultComboBoxModel<>(dir)); 
       comboBox.setVisible(true); 
       btn.setEnabled(false); //disable button 
      } 
     }); 
     btn.setBounds(23, 68, 89, 23); 
     frame.getContentPane().add(btn); 
     frame.setVisible(true); 
    } 
} 

はEDIT実装:

private void initialize() { 

     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JComboBox<String> comboBox = new JComboBox<>(); 
     comboBox.setVisible(false); 
     frame.add(comboBox, BorderLayout.SOUTH); //using BorderLayout which is the default 

     JButton btn = new JButton("Populate combo"); 
     btn.setPreferredSize(new Dimension(150,35)); 
     btn.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 

       //populate combo 
       String[] dir = new String[5]; 

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

       comboBox.setModel(new DefaultComboBoxModel<>(dir)); 
       comboBox.setVisible(true); 
       btn.setEnabled(false); //disable button 
       frame.pack(); //resize fram to fit the preferred size and layouts 
      } 
     }); 
     frame.getContentPane().add(btn, BorderLayout.NORTH); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
関連する問題