Results 1 to 6 of 6
  1. #1
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default text box listeners and returning multiple strings from methods

    I've created a class which renders a shell, the widgets inside it (3 x buttons, 3 x text boxes and 1 x group). Within this class, is a method which should return 3 strings which have been received from the text boxes.

    The only problem is, I don't know how to retreave the contents of the text boxes once I click "login" (button[0]).

    What I want is, once the username, password and database name have been put in, and the 'login' button is pressed, I want the username, password and database that's entered to be returned. I know how to do this in C, but in Java things are a little different.

    Also, when the login screen is initially rendered, I want the 'login' button to be button[0].isEnabled(false);, but once all three boxes have text in them, I want the button to be enabled. So once text is entered into the third (database) text box, I want the button[0].setEnabled(true);

    I've got user to return individual chars so far, not actual strings. And I know I need to put part of the code for the text[0] listener in the: public void widgetSelected(SelectionEvent event) { <IN HERE> }. But I'm not sure which part.

    Thanks in advance!

    Java Code:
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.*;
    import org.eclipse.swt.widgets.*;
    
    public class LoginWidgets {
    
    	final Button[] button = new Button[3];
    	Text[] text = new Text[3];
            final Display display = new Display();
    	public final Shell shell = new Shell(display);
    	final Group group = new Group(shell, SWT.SHADOW_IN);
    	final Label[] label = new Label[3];
    	String user, pass, datab;
    	short counter;
    	
    	String RenderWidgets() {
    		try {
    			shell.setLocation(250, 250);
    			shell.setSize(350, 175);
    			shell.setText("Bike Ordering System Login");
    			shell.open();
      		
    			group.setText("Database Login");
    			group.setBounds(20, 12, 230, 125);
    			
    			button[0] = new Button(shell, SWT.PUSH);
    			button[0].setText("Login");
    			button[0].setSize(70, 25);
    			button[0].setLocation(263, 18);
    			button[0].setEnabled(false);
    			
    			button[1] = new Button(shell, SWT.PUSH);
    			button[1].setText("Cancel");
    			button[1].setSize(70, 25);
    			button[1].setLocation(263, 45);
    						
    			button[2] = new Button(shell, SWT.PUSH);
    			button[2].setText("Databases");
    			button[2].setSize(70, 25);
    			button[2].setLocation(263, 111);
    			
    			label[1] = new Label(group, SWT.CENTER);
    			label[1].setText(" Username: ");
    			label[1].setLocation(5, 27);
    			label[1].setSize(70, 20);
    			
    			label[0] = new Label(group, SWT.CENTER);
    			label[0].setText(" Password: ");
    			label[0].setLocation(5, 57);
    			label[0].setSize(70, 20);
    			
    			label[2] = new Label(group, SWT.CENTER);
    			label[2].setText(" Database: ");
    			label[2].setLocation(5, 87);
    			label[2].setSize(70, 20);
    			
                  <PROBLEMS HERE!!!>
    			text[0] = new Text(group, SWT.BORDER);
    			text[0].setLocation(75, 25);
    			text[0].setSize(130, 20);
    			text[0].setData("Type your Username here");
    			
    <THE LISTENER ONLY RETURNS \n terminated chars>
    			text[0].addVerifyListener (new VerifyListener () {
    				public void verifyText (VerifyEvent event) {
    					text[0].getSelection();
    					user =(event.text);
    					System.out.println(user);
    				}
    			});
    
    							
    			text[1] = new Text(group, SWT.BORDER | SWT.PASSWORD);
    			text[1].setLocation(75, 55);
    			text[1].setSize(130, 20);
    			//text[1].setEchoChar('*');
    			pass = text[1].getText();
    					
    			text[2] = new Text(group, SWT.BORDER);
    			text[2].setLocation(75, 85); 
    			text[2].setSize(130, 20);
    			datab = text[2].getText();
    			
    			button[0].addSelectionListener(new SelectionListener() {
    
    		        public void widgetSelected(SelectionEvent event) {
    		          
    		        }
    
    		        public void widgetDefaultSelected(SelectionEvent event) {
    		          
    		        }
    		      	});
    				} catch(Exception E){E.getCause();}
    					while (!shell.isDisposed()) {
    						if (!display.readAndDispatch())
    							display.sleep();
    					}
    					return user;		
    	}
    }
    Last edited by int80; 07-17-2008 at 02:21 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Not familiar with any of the classes you are using, so can't help.
    Can this be rewritten in Swing?

  3. #3
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Not familiar with any of the classes you are using, so can't help.
    Can this be rewritten in Swing?
    I'm a major Java newbie, so most of that could be really badly written for all I know.

    As far as I know, that should be possible to write in swing.

    I've managed to work it out.

    I just use a for loop and getText() to retreave the text from the text boxes, then, return loginDetails;.

    Java Code:
    public void widgetSelected(SelectionEvent event) {
    		for(int x = 0; x!= 3; x++){
    			loginDetails[x] = text[x].getText();					
    					}
    				}

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    The classes I was refering to are probably in the org.eclipse.swt package.
    Do you have a reason for using those classes instead of the Swing classes?

  5. #5
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The classes I was refering to are probably in the org.eclipse.swt package.
    Do you have a reason for using those classes instead of the Swing classes?
    I don't know why, I just don't like the look of swing that much, and I've _heard_ with complex GUIs it does perform too well (probably not true). So I just decided to use SWT instead. I'm going to have to learn how to use SWING though, as alot of the course work I've been given uses swing.

    Do you know if you can do everything in SWT that you can in SWING, and vice versa?

    Give me C and GTK/QT any day of the week. :D

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    There might not be as many people here familiar with SWT as with Swing. I'm not a GUI person myself, I've used AWT for my simple interfaces.

Similar Threads

  1. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 10:41 AM
  2. Replies: 2
    Last Post: 02-18-2008, 04:24 AM
  3. text Strings to produce letters
    By dc2acgsr99 in forum New To Java
    Replies: 7
    Last Post: 01-29-2008, 08:08 PM
  4. Multiple listeners per event
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-03-2008, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •