Unable to find SWT forum, so posting here

Hi,

Has anyone worked on SWT? I am new to it.. and facing this problem.

In my app, I have a login Page and after giving the credentials the next page is displayed. Implementing this in swing is very easy but I have no idea how to implemt in SWT can anyone give me the sample code for the same.

Sorry for posting here.

Pankaj

[355 byte] By [pbhomiaa] at [2007-10-2 20:57:40]
# 1
Since SWT is a third-party Java library it's unlikely to have a forum here. Have you looked at the SWT documentation for how to use it? There are quite a few useful examples listed on their site: http://www.eclipse.org/swt/Hope this helps.
KPSeala at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks Seal,Those links were quite helpful in understanding SWT. But there is nothing related to the problem i'm facing. So if anyone has previously implemented the same thing in SWT, please help me.Pankaj
pbhomiaa at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 3
There are some related snippets, such as those relating to creating a dialog shell with buttons on.Combine that with an example for using a text field and you're most of the way to a login dialog.
KPSeala at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 4

thanks,

I guess i havent specified the problem clearly. Actually its not only about traversing from login page to next page. When user logs in , the next page has table and if user clicks one of the row in the table, it will load the information of the selected row on the same page. Now thats what which i actually want to achieve. So dialog is not an option :( ..

was so easy in swings , but SWT dont know who to achieve :(

Pankaj

pbhomiaa at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 5

Have you created the SWT shell with the table in it yet?

Once you've got that table populated you just need to add a new selection listener to it:

table.addListener(SWT.Selection, new Listener() {

public void handleEvent(Event e) {

for(TableItem item : table.getSelection()) {

System.out.println("Table item selected: " + item);

}

// Load data here...

}

});

How far have you got? Could you post the code you've written so far?

KPSeala at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 6

I am posting a sample code in which from LoginPage i have to go to a TestPage . I guess if i will achieve this, i can achieve the table. Right now, I am getting SWTException : Subclassing not allowed when i press enter key in the password field. Ideally , it should display a TestPage when i press "ENTER" hey in PASSWORD field. here are the files:

//LoginPagenew.java

package com.qwest.bpm.swt.designer;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.KeyAdapter;

import org.eclipse.swt.events.KeyEvent;

import org.eclipse.swt.layout.FormAttachment;

import org.eclipse.swt.layout.FormData;

import org.eclipse.swt.layout.FormLayout;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Composite;

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 LoginPageNew {

private Shell shell;

public LoginPageNew() {

super();

}

public static void main(String[] args) {

new LoginPageNew().initialize();

}

protected void checkSubClass() {

}

private void initialize(){

Display display = new Display();

shell = new Shell(display);

shell.setText("QBPM");

shell.setMaximized(true);

this.setupWidgets(display, shell);

shell.pack();

shell.open ();

while (!shell.isDisposed ()) {

if (!display.readAndDispatch ()) display.sleep ();

}

display.dispose();

}

private void setupWidgets(Display display, Shell shell){

final Display newDisplay = display;

FormLayout formlayout = new FormLayout();

shell.setLayout(formlayout);

Composite gridComp = new Composite(shell, SWT.NONE);

GridLayout gridLayout = new GridLayout();

gridLayout.numColumns = 2;

gridComp.setLayout(gridLayout);

final Group group = new Group(gridComp, SWT.NONE);

group.setText("Credentials");

Label label1 = new Label(group, SWT.NONE);

label1.setText("User Name");

label1.setBounds(60, 60, 120, 20);

Text username = new Text(group, SWT.BORDER);

username.setBounds(200, 60, 120, 20);

Label label2 = new Label(group, SWT.NONE);

label2.setText("Password");

label2.setBounds(60, 90, 120, 20);

Text password = new Text(group, SWT.BORDER);

password.setEchoChar('*');

password.setBounds(200, 90, 120, 20);

password.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

// close the text editor and copy the data over

// when the user hits "ENTER"

if (e.character == SWT.CR) {

System.out.println("Enter is pressed kya");

TestPage form = new TestPage(LoginPageNew.this.shell);

form.show();

}

}});

Label label3 = new Label(group, SWT.NONE);

label3.setText("JNDI Server URL");

label3.setBounds(60, 120, 120, 20);

Text serverurl = new Text(group, SWT.BORDER);

serverurl.setText("t3://localhost:7001");

serverurl.setBounds(200, 120, 120, 20);

Label label4 = new Label(group, SWT.NONE);

label4.setText("Designer Server");

label4.setBounds(60, 150, 120, 20);

Text servername = new Text(group, SWT.BORDER);

servername.setText("com.qwest.bpm.designerserver.DesignerServerRemoteHome");

servername.setBounds(200, 150, 240, 20);

Label label5 = new Label(group, SWT.NONE);

label5.setText("Database");

label5.setBounds(60, 180, 120, 20);

Button database = new Button(group,SWT.RADIO);

database.setBounds(200, 180, 120, 20);

database.setText("MDW");

Button login = new Button(group,SWT.PUSH);

login.setBounds(150, 240, 120, 25);

login.setText("Login");

//login.addSelectionListener(page);

FormData formGrid = new FormData();

formGrid.top = new FormAttachment(0,120);

formGrid.left = new FormAttachment(0,150);

//formGrid.right = new FormAttachment(100,-10);

//formGrid.bottom = new FormAttachment(10);

gridComp.setLayoutData(formGrid);

}

}

//TestPage.java

package com.qwest.bpm.swt.designer;

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.layout.GridLayout;

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 TestPage extends Shell{

private Shell shell = null;

private Text firstName = null;

private Display display;

public TestPage(Shell arg0) {

this(arg0, SWT.APPLICATION_MODAL);

}

/**

* @param arg0

* @param arg1

*/

public TestPage(Shell arg0, int arg1) {

super(arg0, arg1);

}

protected void checkSubClass() {

}

public void show(){

shell = new Shell(display);

shell.setText("Edit");

shell.setLayout(new FillLayout());

//new Button(shell,SWT.PUSH).setText("Go");

this.initWidgets();

shell.pack();

shell.open();

//Display display = new Display();

while (!shell.isDisposed()) {

if (!display.readAndDispatch())

display.sleep();

}

}

public void initWidgets(){

Group group = new Group(shell, SWT.NONE);

group.setText("Person");

GridLayout layout = new GridLayout();

layout.numColumns=2;

group.setLayout(layout);

new Label(group, SWT.SHADOW_OUT).setText("FirstName");

firstName = new Text(group, SWT.BORDER);

firstName.setText("Pankaj Bhomia");

}

}

pbhomiaa at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 7

public void keyPressed(KeyEvent e) {

// close the text editor and copy the data over

// when the user hits "ENTER"

if (e.character == SWT.CR) {

System.out.println("Enter is pressed kya");

keyPressCount++;

}

}});

public static void main(String args[]){

if (keyPressCount>0){

// open second win

}

else{

// open firstwin

}

}

evilknighthka at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 8

Hopefully this link will provide some more context on that error:

http://www.awprofessional.com/articles/article.asp?p=354574&seqNum=2&rl=1

Adding the checkSubclass method should clear up the exception you've cited. You'll get another one shortly after but it should be more easily addressed.

Hope this helps.

KPSeala at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 9
I tried putting protected void checkSubClass() {}method in TestPage.java but didn't worked. can u please tell me where to add this method.
pbhomiaa at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 10
oh sry my mistake... spelling incorrect ... extremly sorry.. this worked but still not able to achieve what i want :(
pbhomiaa at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 11

I got a null pointer exception trying your code. It's because you never set TestPage.display to anything.

I changed the constructor as follows as it seems to work for me:

public TestPage(Shell arg0, int arg1) {

super(arg0, arg1);

// added this line

display = arg0.getDisplay();

}

Hope this helps.

KPSeala at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 12

Yeah, I myself noticed this late that display has to be set which I have done. Also I am very close to achieve what i want. Actually in TestPage, instead of creating new Shell , I am using this.getShell() . This is bringing the TestPage on the same Shell.

The change is in show() method of TestPage :

public void show(){

shell = this.getShell();

shell.setText("Edit");

shell.setLayout(new FillLayout());

//new Button(shell,SWT.PUSH).setText("Go");

this.initWidgets();

//shell.pack();

shell.open();

Display display = this.getParent().getDisplay();

while (!shell.isDisposed()) {

if (!display.readAndDispatch())

display.sleep();

}

}

-

If u run the code using this new show() method, it will display on the same Shell but the previous loginPage is not removed yet . I want to clear the pprevious contents of the shell. Does the Shell class provide any way to remove its contents? I looked into SWT api but havent got anything concrete.

By the way, thanks for the pain u taking to solve the issue :)

Pankaj

pbhomiaa at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...
# 13

If you want to change the contents of the shell then you can either hide or dispose of the composite and simply add new ones:

if (e.character == SWT.CR) {

// Remove the old content...

// gridComp.dispose();

// ...or hide it instead if more appropriate

gridComp.setVisible(false);

Label label = new Label(shell, 0);

label.setText("As if by magic, the contents have gone!");

FormData formData = new FormData();

formData.left = new FormAttachment(0, 0);

formData.top = new FormAttachment(0, 0);

label.setLayoutData(formData);

shell.layout();

System.out.println("Enter is pressed kya");

TestPage form = new TestPage(ShellTest.this.shell);

form.show();

}

Hope this helps.

KPSeala at 2007-7-13 23:42:22 > top of Java-index,Desktop,Core GUI APIs...