• Post Reply Bookmark Topic Watch Topic
  • New Topic

Put array of Strings into JList

 
albert sie
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found out the problem in my program. here is my program pls check for me:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;

public class GlossarySearch extends JFrame
{ private JTextField term;
private JList select;
private JTextField definition;
private JButton next;
private JButton previous;
public String[] title=new String[10];
public String[] explain=new String[20];
//private Search search;

public GlossarySearch(String title){
//search=new Search();

buildGUI();
setTitle(title);
pack();
setVisible(true);
}

private void buildGUI(){
String sModel=null;
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
term=new JTextField(10);
definition=new JTextField(100);
next=new JButton("Next Term");
previous=new JButton("Previous Term");
select=new JList(sModel);
for(int i=0;i<10;i++){
sModel.addElement(title[i]);
}
contentPane.add("North",select);
JPanel inputPanel=new JPanel();
inputPanel.add(term);
inputPanel.add(definition);
contentPane.add("Center",inputPanel);
JPanel inputPanel2=new JPanel();
inputPanel2.add(next);
inputPanel2.add(previous);
contentPane.add("South",inputPanel2);

}
public void readText(){
String myFileName = "test.txt";
int a=0;
int b=0;
try{

File myFile = new File(myFileName);
Scanner scanMyFile = new Scanner(myFile);
scanMyFile.useDelimiter("\r\n");
if (scanMyFile.hasNext()) {
title[a]= scanMyFile.next();
a=a+1;

if (scanMyFile.hasNext()) {
explain[b]= scanMyFile.next();
b=b+1;

}
}

} catch(Exception ex) {

System.out.println("exception "+ex.getMessage()+" caught");

}

//System.out.println("file "+myFileName+ " contains "+wordCount+ " words.");

}


public static void main(String args[]){
new GlossarySearch("Glossary Search");
}
}

i want to put what i read from text file into the list but i fail to do that. pls tell me wat the problem is this.thank you

regars,
albert
[ August 12, 2006: Message edited by: Ernest Friedman-Hill ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd get a lot more answers to questions like this if you simply asked "How do I put an array of Strings into a JList?" in the Swing forum, instead of expecting people to slog through a hundred lines of unformatted spaghetti in Java in General (Intermediate) to discover that this is what you're asking.

I'm going to change the subject of this message to "Put array of Strings into JList", and move it to the Swing forum. Perhaps then someone will answer your question.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> i want to put what i read from text file into the list but i fail to do that.

try this
(I've simplified your code to dealing with only the above)

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!