Java 如何在单击另一个选项卡的按钮后执行一个选项卡中包含的try-catch

Java 如何在单击另一个选项卡的按钮后执行一个选项卡中包含的try-catch,java,database,sqlite,blackberry,user-interface,Java,Database,Sqlite,Blackberry,User Interface,我正在编写下面的代码,在Blackberry的pane manager中创建了3个选项卡。第一个选项卡允许用户根据客户名称选择日期范围,并获取该客户的结果,以网格格式显示在第三个选项卡中 因此,第一个选项卡有两个日期字段和一个客户文本字段,以及一个搜索按钮 单击搜索按钮,它将选择搜索记录并跳转到第三个选项卡,以网格格式显示 第三个选项卡有try-catch语句,用于将表记录元素逐字符串插入网格 现在的问题是,只要我打开应用程序并单击第三个选项卡,它就会显示充满垃圾值的网格。它不会等待单击第一个选

我正在编写下面的代码,在Blackberry的pane manager中创建了3个选项卡。第一个选项卡允许用户根据客户名称选择日期范围,并获取该客户的结果,以网格格式显示在第三个选项卡中

  • 因此,第一个选项卡有两个日期字段和一个客户文本字段,以及一个搜索按钮

  • 单击搜索按钮,它将选择搜索记录并跳转到第三个选项卡,以网格格式显示

  • 第三个选项卡有try-catch语句,用于将表记录元素逐字符串插入网格

  • 现在的问题是,只要我打开应用程序并单击第三个选项卡,它就会显示充满垃圾值的网格。它不会等待单击第一个选项卡的搜索按钮,然后显示结果。如果我碰巧关闭应用程序并重新打开它,我会发现第三个选项卡显示我以前搜索的结果

    代码如下:

       // setup the tab model with 3 tabs
      final PaneManagerModel model = new PaneManagerModel();
      model.enableLooping( true );
    
      // setup the first tab   XXXXXXXXXXXXXXXXXXXX~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       VerticalFieldManager vfm = new VerticalFieldManager( 
          Field.USE_ALL_HEIGHT | Field.USE_ALL_WIDTH |
          Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL );
      LabelField lbl = new LabelField( "Content for tab 1", Field.FOCUSABLE );
      vfm.add( lbl );
    
      TextField1 = new TextField(" Name:            ",null)
         {
                protected boolean keyChar(char ch, int status, int time) 
                {
                if (CharacterUtilities.isLetter(ch) || (ch == Characters.BACKSPACE || (ch == Characters.SPACE))) 
                {
                return super.keyChar(ch, status, time);
                }
               return true;
                }
            };
         vfm.add(TextField1);
    
        SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");//dd/MM/yyyy
         Date now = new Date();
         String strDate = sdfDate.format(now);
    
         TextField2 = new TextField("\n From:             ",strDate);
         vfm.add(TextField2);
    
        // TextField3 = new TextField("TO: ",null);
         //vfm.add(TextField3); 
    
         SimpleDateFormat sdfDate1 = new SimpleDateFormat("dd/MM/yyyy");//dd/MM/yyyy
         Date later = new Date();
         String strDate1 = sdfDate1.format(later);
    
         TextField3 = new TextField("\n To:                 ",strDate1);
         vfm.add(TextField3);
    
         ButtonField showInputButton = new ButtonField("  Search  ",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
          showInputButton.setChangeListener(new FieldChangeListener() 
          {
                public void fieldChanged(Field field,int context) 
                {
                        Dialog.alert(TextField1.getText());
                        try
                      {    
                        //Open or create the database
                        Database db = DatabaseFactory.openOrCreate("database1.db");
                        //Insert onto table
                        Statement statement13 = db.createStatement("INSERT into 
                        Temp4(date,bill,narration) VALUES (('"+TextField3.getText()+"'),(SELECT balance FROM Temp3),('Opening Balance'))");
                        statement13.prepare();
                        statement13.execute(); 
    
                        Statement statement131 = db.createStatement("INSERT INTO Temp4(date,bill,narration,id) select date,amount,narration,id from Bills where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) < substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2) ");
                        statement131.prepare();
                        statement131.execute();      // date INTEGER,bill INTEGER,rec INTEGER,narration TEXT,id INTEGER
                        statement131.close(); 
    
                        Statement statement132 = db.createStatement("INSERT INTO  Temp4(date,rec,narration,id) select date,amount,narration,id from Receipts where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) < substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2) ");
                        statement132.prepare();
                        statement132.execute();       
                        statement132.close(); 
    
                         db.close();
                        }
                        catch( Exception e ) 
                        {         
                            System.out.println( e.getMessage() );
                            e.printStackTrace();
                        }
                        model.getView().jumpTo(2,PaneManagerView.DIRECTION_NONE);
                      }
            }
    
                        );
    
          vfm.add(showInputButton);
          LabelField myLbl = new MyLabelField( "Ledger" );
     NullField nullFld = new NullField( Field.FOCUSABLE );
      HorizontalFieldManager hfm = new HorizontalFieldManager();
      hfm.add( nullFld );
      hfm.add( myLbl );
    
      Pane pane = new Pane( hfm, vfm );
      model.addPane( pane );
    
       //Here ends tab 1 code
    
    我还尝试禁用第三个窗格,并将其添加到第一个选项卡搜索实现中,但由于第三个选项卡是在第一个选项卡之后定义的,因此出现了一个错误。 此外,我还尝试在函数中包含第三个选项卡的try-catch,以便在第一个选项卡的按钮单击时调用它。但这也会导致“表达式的非法启动”错误


    请建议一个解决方案。任何能想到此代码并帮助解决方案的人都将不胜感激。谢谢。

    您有没有看过BlackBerry主屏幕的
    onExposed
    onobscure
    方法

    受保护的void onExposed()

    当屏幕弹出显示此屏幕时调用 显示堆栈。screen的子类应覆盖此方法 用于特殊处理

    恭维回调是onObscured()


    受保护的void onobscure()

    当此屏幕被推到屏幕上的新屏幕遮挡时调用 显示堆栈。screen的子类应覆盖此方法,以便 特殊处理

    恭维回调是onExposed()


    您是否看过BlackBerry主屏幕的
    onExposed
    onobscure
    方法

    受保护的void onExposed()

    当屏幕弹出显示此屏幕时调用 显示堆栈。screen的子类应覆盖此方法 用于特殊处理

    恭维回调是onObscured()


    受保护的void onobscure()

    当此屏幕被推到屏幕上的新屏幕遮挡时调用 显示堆栈。screen的子类应覆盖此方法,以便 特殊处理

    恭维回调是onExposed()

          vfm = new VerticalFieldManager( 
          Field.USE_ALL_HEIGHT | Field.USE_ALL_WIDTH |
          Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL );
    
          myLbl = new MyLabelField( "Daily Report" );
    
          //Adding grid format for fetching from temp4 table----------------
    
    
     final GridFieldManager grid = new GridFieldManager(10,5,0); 
    
        grid.add(new LabelField("Date")
        {
           public void paint(Graphics graphics)
           {
             graphics.setColor(Color.CYAN);
             super.paint(graphics);
           }
         });
        grid.add(new LabelField("Bill")
        {
           public void paint(Graphics graphics)
           {
             graphics.setColor(Color.CYAN);
             super.paint(graphics);
           }
         });
        grid.add(new LabelField("Receipt")
        {
           public void paint(Graphics graphics)
           {
             graphics.setColor(Color.CYAN);
             super.paint(graphics);
           }
         });
        grid.add(new LabelField("Narration")
        {
           public void paint(Graphics graphics)
           {
             graphics.setColor(Color.CYAN);
             super.paint(graphics);
           }
         });
        grid.add(new LabelField("ID")
        {
           public void paint(Graphics graphics)
           {
             graphics.setColor(Color.CYAN);
             super.paint(graphics);
           }
         });
    
        grid.setColumnPadding(20);
        grid.setRowPadding(20);
    //try catch was here
    
    
           try
                   {
    
                      //Open or create the database
                        Database db = DatabaseFactory.openOrCreate("database1.db"); 
    
                        Statement statement55 = db.createStatement("CREATE TABLE IF NOT EXISTS Temp4(date INTEGER,bill INTEGER,rec INTEGER,narration TEXT,id INTEGER)");
                        statement55.prepare();
                        statement55.execute();       
                        statement55.close();
    
                        Statement statement56 = db.createStatement("SELECT date,bill,rec,narration,id FROM Temp4 ORDER BY ROWID DESC");
                        statement56.prepare();
                        statement56.execute();
    
                                Cursor c = statement56.getCursor();
    
                                //Get to the row of grid
                                 for (int i = 1; i < grid.getRowCount(); i++)
                                 {
                                        System.out.println("Inside for first loops");
                                        //Get to the column of grid
                                    for (int j = 0; j < grid.getColumnCount() ; j++)
                                    {
                                       System.out.println("Inside for second loops");
                                       //Get to the row of temp4 table
                                       while(c.next()) 
                                       {
    
                                          System.out.println("Inside while"); 
                                            Row r;
                                            r = c.getRow();
                                            //Get to the column of temp4 table
    
                                            for (int k = 4; k >=0; k--)
                                            {
    
                                                System.out.println("Inside for loops");
                                                //Check for whether column retrieved is date or naraation
                                                if(k==0 || k==3)
                                                {
                                                    System.out.println("Retrieving date or narration");
                                                    grid.insert(new LabelField(r.getString(k))
                                                    {
                                                        public void paint(Graphics graphics)
                                                        {
                                                        graphics.setColor(Color.GOLD);
                                                        super.paint(graphics);
                                                        }
                                                     },i,j);
    
    
                                                }  
                                                else
                                                {   
                                                    //Check for whether column retrieved is bills,rec or id
                                                    System.out.println("Retrieving other values"); 
                                                    String p = "" + r.getObject(k);
    
                                                    //if(r.getString(k) != null)
                                                    //{ 
                                                    grid.insert(new LabelField(p)
                                                    {
                                                        public void paint(Graphics graphics)
                                                        {
                                                        graphics.setColor(Color.GOLD);
                                                        super.paint(graphics);
                                                        }
                                                     },i,j); 
                                                   //  } 
    
    
                                                }   
                                               grid.setBackground(BackgroundFactory.createLinearGradientBackground(Color.MIDNIGHTBLUE,Color.STEELBLUE,Color.MIDNIGHTBLUE,Color.STEELBLUE));
                                               //grid.setBackground(BackgroundFactory.createLinearGradientBackground(Color.GOLD,Color.CHOCOLATE,Color.GOLDENROD,Color.CORAL));
    
                                            } 
                                             System.out.println("Exiting while");                        
                                          }
    
                                          System.out.println("Exiting sec for");
                                          break;
                                      }
                                    System.out.println("Exiting first for");
                                    break;
                                   } 
                                   statement56.close(); 
                                   db.close();
                      }
    
                      catch( Exception e ) 
                      {         
                            System.out.println( e.getMessage() );
                            e.printStackTrace();
                      }  
    
    
    
    
    
        vfm.add(grid);
    
       //----------------grid ends---------------------------------------- 
    
      nullFld = new NullField( Field.FOCUSABLE );
      hfm = new HorizontalFieldManager();
      hfm.add( nullFld );
      hfm.add( myLbl );
    
       pane = new Pane( hfm, vfm );
       model.addPane( pane );
    
          // select the tab to be displayed
      model.setCurrentlySelectedIndex( 0 );    
    
      // setup the rest of the components
      HorizontalTabTitleView titleView = new HorizontalTabTitleView( Field.FOCUSABLE );
      titleView.setNumberOfDisplayedTabs( 3 );
      titleView.setModel( model );
    
      PaneView paneView = new PaneView( Field.FOCUSABLE );
      paneView.setModel( model );
    
      PaneManagerView view = new PaneManagerView( 
              Field.FOCUSABLE  | Manager.NO_VERTICAL_SCROLL | 
              Manager.NO_HORIZONTAL_SCROLL | Manager.USE_ALL_HEIGHT | 
              Manager.USE_ALL_WIDTH, 
              titleView, paneView );
      view.setModel( model );
      model.setView( view );
    
      // configure the Controller
      HorizontalTabController controller = new HorizontalTabController();
      controller.setModel( model );
      controller.setView( view );
      model.setController( controller );
      view.setController( controller );
    
    
      // add the tab manager to the MainScreen
      this.add( view );
    
     }