Results 1 to 4 of 4
  1. #1
    karoline is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default call from another class

    Hi, i'm a newbie in JAVA

    I've got an online sample of displaying a directoryBox.

    Java Code:
    public class test {
     
     
    /**
       * Runs the application
       */
      public void run() {
    	
        Display display = new Display();
       
        Shell shell = new Shell(display);
        shell.setText("Directory Browser");
        createContents(shell);
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
      }
     
     
      void createContents(final Shell shell) {
        shell.setLayout(new GridLayout(6, true));
        new Label(shell, SWT.NONE).setText("Directory:");
    ...
    ..


    It works well.
    The question is, how can I call this whole codes out from another menu button?

    I tried this,

    Java Code:
    ...
    public void widgetSelected(SelectionEvent e) {
    	
    				test dialogDir = new test();
    				dialogDir.run();
    ...


    When I ran this part in debug mode, the code seems to have problem when reaching '' Shell shell = new Shell(display); '' in run().

    What's the correct way of calling the working independent test() from a menu of another class?

    Thanks in advance.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by karoline View Post
    When I ran this part in debug mode, the code seems to have problem when reaching '' Shell shell = new Shell(display); '' in run().
    What do you mean "seems to have problem"? Were there error messages (if yes, copy and paste the FULL messages here)? What happened versus what you expected to happen?

  3. #3
    karoline is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    There's no error when I ran this code.

    public class test33 {


    /**
    * Runs the application
    */
    public void run() {
    System.out.println("\n \n pressed run!");
    Display display = new Display();
    But when I clicked on the menu button, nothing came out but only the "pressed run!" display rather than the popup directory box as in test33.

    It seems the codes just stopped there..

  4. #4
    karoline is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    To make it clearer,

    I would like to display a directoryBox and I found a working sample of that online as test33.
    public class test33 {

    public void run() {
    System.out.println("\n \n pressed run!");
    Display display = new Display();

    Shell shell = new Shell(display);
    shell.setText("Directory Browser");
    createContents(shell);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
    display.sleep();
    }
    }
    }

    void createContents(final Shell shell) {
    shell.setLayout(new GridLayout(6, true));
    new Label(shell, SWT.NONE).setText("Directory:");

    final Text text = new Text(shell, SWT.BORDER);
    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    text.setLayoutData(data);


    Button button = new Button(shell, SWT.PUSH);
    button.setText("Browse...");
    button.addSelectionListener(new SelectionAdapter() {

    public void widgetSelected(SelectionEvent event) {

    DirectoryDialog dlg = new DirectoryDialog(shell);

    dlg.setFilterPath(text.getText());
    dlg.setText("SWT's DirectoryDialog");
    dlg.setMessage("Select a directory");

    String dir = dlg.open();
    if (dir != null) {

    text.setText(dir);
    }
    }
    });
    }

    public static void main(String[] args) {
    new test33().run();
    }
    }
    It's working fine as what I want.

    Then I tried to integrate this (test33) into my codes. Where I want to call test33 popup directoryBox from one of the menus. At which if I clicked on the menu item of import external data, the test33 will be called and display the directory box.

    m_miEventBrowse = new MenuItem(listMenu, SWT.CHECK);
    m_miEventBrowse.setText("Import external data");
    m_miEventBrowse.setImage(PlatformUI.getWorkbench() .getSharedImages()
    .getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD ).createImage());
    m_miEventBrowse.addSelectionListener(new SelectionAdapter() {

    public void widgetSelected(SelectionEvent e) {

    test33 dialogDir = new test33();
    dialogDir.run();

    }

    });
    And now the problem is, when I clicked on the import data data menu, the system is only be able to display "pressed run!" as in test33 but nothing more that. It's unable to go though
    Shell shell = new Shell(display);
    seems.

    Thanks.

Similar Threads

  1. how do i call a class from main class
    By revanger in forum New To Java
    Replies: 7
    Last Post: 07-20-2010, 05:32 PM
  2. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  3. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  4. need help to call another class
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 03-08-2010, 10:40 AM
  5. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 03:49 PM

Posting Permissions

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