Hi,
I have a class that creates a GUI interface with a text box on it.
I want to be able to add text into the text box from another class but I can't seem to do it.
Does anyone have any examples they can share?
Thanks
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
I have a class that creates a GUI interface with a text box on it.
I want to be able to add text into the text box from another class but I can't seem to do it.
Does anyone have any examples they can share?
Thanks
Post what you've done and where you need help (doing so as an SSCCE will strengthen your chance of getting quicker and more meaningful response).
ok thanks.
So this is my main class where I have created what is now a ListBox and a button. When I click on the button I want to be able to load a set of results from a csv file and add them to the lisbox.
thanks
import org.eclipse.swt.widgets.Display; public class guiwindow { protected Shell shell; /** * Launch the application. * @param args */ public static void main(String[] args) { try { guiwindow window = new guiwindow(); window.open(); } catch (Exception e) { e.printStackTrace(); } } /** * Open the window. */ public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } /** * Create contents of the window. */ protected void createContents() { shell = new Shell(); shell.setSize(450, 300); shell.setText("SWT Application"); List lstItems = new List(shell, SWT.BORDER); lstItems.setBounds(10, 10, 197, 242); Button btnLoadFile = new Button(shell, SWT.NONE); btnLoadFile.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { [B][COLOR="red"]//run import from csv here[/COLOR][/B] } }); btnLoadFile.setBounds(220, 10, 75, 25); btnLoadFile.setText("Load File"); } } import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; public class CSVFileReader { ArrayList <String>storeValues = new ArrayList<String>(); ArrayList <String>CleanList = new ArrayList<String>(); private String fileName; public CSVFileReader(String FileName) { this.fileName=FileName; } public ArrayList<String> ReadFile() { try { //storeValues.clear();//just in case this is the second call of the ReadFile Method./ BufferedReader br = new BufferedReader( new FileReader(fileName)); StringTokenizer st = null; int lineNumber = 0, tokenNumber = 0; while( (fileName = br.readLine()) != null) { lineNumber++; //System.out.println(fileName); storeValues.add(fileName); //break comma separated line using "," st = new StringTokenizer(fileName, ","); while(st.hasMoreTokens()) { //System.out.println("Line # " + st.nextToken()); CleanList.add(st.nextToken()); } //reset token number tokenNumber = 0; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return CleanList; } //mutators and accesors public void displayArrayList() { for(int x=0;x<this.CleanList.size();x++) { System.out.println(CleanList.get(x)); } } }
Last edited by copeg; April 3rd, 2011 at 09:29 AM.
ok to fix it i did the following
public class guiwindow {
protected Shell shell;
public List lstItems;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
guiwindow window = new guiwindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
lstItems = new List(shell, SWT.BORDER);
lstItems.setBounds(10, 10, 197, 242);
Button btnLoadFile = new Button(shell, SWT.NONE);
btnLoadFile.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
//run import from csv here
}
});
btnLoadFile.setBounds(220, 10, 75, 25);
btnLoadFile.setText("Load File");
}
}