Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT program doesn't run(I have written a program for temperature converter)
SWT program doesn't run [message #1708017] Fri, 11 September 2015 13:12 Go to next message
Amritansh Singh is currently offline Amritansh SinghFriend
Messages: 1
Registered: September 2015
Junior Member
Hi, here is the code which I have been trying to run
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TemperatureConverter {
Display display = new Display();
Shell shell = new Shell(display);
Label fahrenheitLabel;
Label celsiusLabel;
Label MessageLabel;
Text fahrenheitValue;
Text celsiusValue;

public TemperatureConverter() {
shell.setText("Temperature Converter");
shell.setLayout(new RowLayout());
fahrenheitLabel = new Label(shell, SWT.NONE);
fahrenheitLabel.setText("Fahrenheit: ");
fahrenheitValue = new Text(shell, SWT.SINGLE | SWT.BORDER);
celsiusLabel = new Label(shell, SWT.NONE);
celsiusLabel.setText("Celsius: ");
celsiusValue = new Text(shell, SWT.SINGLE | SWT.BORDER);
MessageLabel = new Label(shell, SWT.BORDER);
GridData griddata = new GridData(GridData.FILL_BOTH);
griddata.horizontalSpan = 4;
MessageLabel.setLayoutData(griddata);
ModifyListener listener = new ModifyListener() {

@Override
public void modifyText(ModifyEvent E) {
ValueChanged((Text) E.widget);
}
};
fahrenheitValue.addModifyListener(listener);
celsiusValue.addModifyListener(listener);
shell.setBounds(40, 30, 500, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

public void ValueChanged(Text text){
if(!text.isFocusControl())
return;
if(text == fahrenheitValue){
try{
double fvalue = Double.parseDouble(text.getText());
double cvalue = (fvalue - 32) / 1.8;
celsiusValue.setText(Double.toString(cvalue));
MessageLabel.setText("Conversion Performer Successfully.");
} catch(NumberFormatException e){
celsiusValue.setText(" ");
MessageLabel.setText("Invalid Number Format " + text.getText());
}
} else {
try{
Double cvalue = Double.parseDouble(text.getText());
Double fvalue = cvalue * 1.8 + 32;
fahrenheitValue.setText(Double.toString(fvalue));
MessageLabel.setText("Conversion Performed Successfully. ");
} catch(NumberFormatException e){
fahrenheitValue.setText(" ");
MessageLabel.setText("Invalid Number Format " + text.getText());
}
}
}
public static void main(String[] args) {
new TemperatureConverter();
}
}


I can run normal SWT programs like HelloSWT, creating a few buttons etc.
But this time I am getting these errors.

Exception in thread "main" java.lang.ClassCastException: org.eclipse.swt.layout.GridData cannot be cast to org.eclipse.swt.layout.RowData
at org.eclipse.swt.layout.RowLayout.layoutHorizontal(RowLayout.java:251)
at org.eclipse.swt.layout.RowLayout.layout(RowLayout.java:240)
at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1290)
at org.eclipse.swt.widgets.Composite.sendResize(Composite.java:981)
at org.eclipse.swt.widgets.Composite.setBounds(Composite.java:1026)
at org.eclipse.swt.widgets.Decorations.setBounds(Decorations.java:887)
at org.eclipse.swt.widgets.Shell.setBounds(Shell.java:1505)
at org.eclipse.swt.widgets.Control.setBounds(Control.java:3104)
at org.eclipse.swt.widgets.Control.setBounds(Control.java:3100)
at demo1.TemperatureConverter.<init>(TemperatureConverter.java:44)
at demo1.TemperatureConverter.main(TemperatureConverter.java:79)


Please help me out as soon as possible..
Thanks in advance Smile Confused
Re: SWT program doesn't run [message #1708019 is a reply to message #1708017] Fri, 11 September 2015 13:14 Go to previous messageGo to next message
Eric Rizzo is currently offline Eric RizzoFriend
Messages: 3057
Registered: July 2009
Senior Member
Moving this to the SWT forum.
Re: SWT program doesn't run [message #1708122 is a reply to message #1708019] Mon, 14 September 2015 05:40 Go to previous message
Niraj Modi is currently offline Niraj ModiFriend
Messages: 47
Registered: November 2013
Member
Hi Amritansh,
You seems to be mixing RowLayout and GridLayout, suggest you to refer SWT example Snippets @ https://www.eclipse.org/swt/snippets/
Previous Topic:programmatically fire Tab key press event
Next Topic:SWT maps event to wrong control
Goto Forum:
  


Current Time: Sat Dec 16 18:55:20 GMT 2017

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

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