Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » why the text diplayed on screen is not the text.getText()??
why the text diplayed on screen is not the text.getText()?? [message #441990] Fri, 27 August 2004 08:37 Go to next message
Eclipse UserFriend
Originally posted by: wxbpp.hotmail.com

import java.util.StringTokenizer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TestText {

private Shell shell;

public TestText(Shell aShell) {
shell = aShell;
shell.setText("test");
shell.setLayout(new GridLayout(1,true));
shell.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
shell.setVisible(false);
}

public void start(){
showLogContent("start");
}

private void showLogContent(String str){
createContent(str);
createButtons();

shell.setSize(400,250);
shell.setVisible(true);
}

private void createContent(String str){
Label label = null;
Text text = null;
label = new Label(shell,SWT.NONE | SWT.WRAP);
label.setText("No: ");
text = new Text(shell,SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setEditable(true);
text.setText(str);// why the text diplayed on screen is not the
text.getText()
text.update();
System.out.println("input parameter is "+str+" source is
"+text.getText());
}

private void createButtons(){
Button preBtn = new Button(shell,SWT.PUSH);
preBtn.setText("test");
preBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
preBtn.addSelectionListener(new SelectionListener(){

public void widgetSelected(SelectionEvent e) {
showLogContent("runtime");
}

public void widgetDefaultSelected(SelectionEvent e) {

}
});
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
TestText win = new TestText(shell);
win.start();
while (! shell.isDisposed()) {
if (! display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
Re: why the text diplayed on screen is not the text.getText()?? [message #441997 is a reply to message #441990] Fri, 27 August 2004 12:22 Go to previous message
Eclipse UserFriend
Originally posted by: friederich.kupzog.de

Hello,

the problem is that any time you call showLogContent you create new
widgets. After 10 calls your shell has 10 labels, 10 text widgets and 10
buttons. You should call shell.pack() to see them, but i do not think
that is what you intend to to.

The solution (as shown below) is to create the content only once. YOu
can keep a reference to the text widget as class member variable and set
the text using this reference anytime you whish. a text.update() is not
neccessary.

Hope that helped

Friederich


import java.util.StringTokenizer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TestText {

private Shell shell;
private Text text; // you need to safe a reference to the text field

public TestText(Shell aShell) {
shell = aShell;
shell.setText("test");
shell.setLayout(new GridLayout(1,true));
shell.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
createContent(); // create the widgets only once!
createButtons();
shell.setVisible(false);
}

public void start(){
showLogContent("start");
}

private void showLogContent(String str){
shell.setSize(400,250);
shell.setVisible(true);
text.setText(str);
}

private void createContent(){
Label label = new Label(shell,SWT.NONE | SWT.WRAP);
label.setText("No: ");
text = new Text(shell,SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setEditable(true);
}

private void createButtons(){
Button preBtn = new Button(shell,SWT.PUSH);
preBtn.setText("test");
preBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
preBtn.addSelectionListener(new SelectionListener(){

public void widgetSelected(SelectionEvent e) {
showLogContent("runtime");
}

public void widgetDefaultSelected(SelectionEvent e) {

}
});
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
TestText win = new TestText(shell);
win.start();
while (! shell.isDisposed()) {
if (! display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}


--
Friederich Kupzog
Elektronik & Software
Neusser Str. 5-7
50670 Köln
Tel 0241 160696-1
Fax 0221 726670
www.kupzog.de/fkmk
Previous Topic:how to make composite resizable
Next Topic:Image-Buttons on PocketPC
Goto Forum:
  


Current Time: Fri Apr 19 12:20:43 GMT 2024

Powered by FUDForum. Page generated in 0.02611 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top