2017-11-27 5 views
-1

私が取り組んでいるこのAccount Managerプロジェクトのすべての現在のアカウントのリストをドロップダウンする必要があります。私はこのJComboBoxを使用しようとしていますが、文字列の配列をハードコードしても問題ありませんが、ファイルから読み込んで配列に配置すると機能しません。私はすでに配列に何かがあったかどうかを調べるためにテストしました。JComboBoxに文字列配列を使用する

FileReader file = new FileReader("data"); 
BufferedReader reader = new BufferedReader(file); 

String line; 
int num_lines = 0; 
while((line = reader.readLine()) != null) 
{ 
num_lines++; 
} 

String [] accountData = new String[num_lines]; 

for(int i = 0; i < num_lines; i++) 
{ 
accountData[i] = reader.readLine(); 
} 


JComboBox comboBox = new JComboBox(accountData); 
+0

だから、あなたがそこにあるどのように多くの行数を取得するためにすべての行を読んで、その後、あなたが実際のデータを取得するために読み続けますか?あなたには意味がありますか?確かに私にはない! – John3136

+0

しかし、私はそれを配列accountDataに割り当てて、JComboBoxでそれを使用しています。少なくとも私はそれをやっていると思いますか? –

+0

申し訳ありませんが、私はJavaにとって非常に新しいです。私はWindow Builderを使用していて、余分なコードを書くことになっています。しかし、私は読んでいるtxtファイルを持っています。 –

答えて

-1

このメイトを試してみてください。

FileReader file = new FileReader("data"); 
BufferedReader reader = new BufferedReader(file); 
JComboBox comboBox = new JComboBox(); 
String line; 
int num_lines = 0; 
while((line = reader.readLine()) != null) 
{ 
    num_lines++; 
} 

String [] accountData = new String[num_lines]; 

for(int i = 0; i < num_lines; i++){ 
    accountData[i] = reader.readLine(); 
    comboBox.addItem(accountData[i]); 
} 
+0

動作しませんでした:/ –

関連する問題