Results 1 to 13 of 13
  1. #1
    Arno is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default code jumps out of For loop and picks up later.. why?

    Hi, i'm pretty new to Java, but have been working with the basics for a little time now.
    Does anyone has an idea of why my code jumps out and back in a for loop...?

    I have left most of the code gone, but left the essentials to see what's happening (it's actually a lot of API language, but structured in Java).

    => It goes like this; "Btn8_CmdListener" is pressed (a menubutton) so then "Btn_callback();" is executed, then "pdfDia.CreateandShow();" which creates a dialog with another button. that will execute some code and also "Macros()". In this last void, there is a For loop and only the first line in this for loop is executed. Then the "Btn8_CmdListener" finishes (so writes in Log "end Jlink Button") and only then will the For loop proceed to finish the rest of the loop. this can not be normal right? any reason why this is happening?

    Java Code:
    public class PDFexport {
    	
    	public static JCheckBox chbx[]=new JCheckBox [30];
    	public static int iii=0;
    	public static StringBuffer sb= new StringBuffer();	
    	
    	public PDFexport() {
    	
    		[B]public static void Btn_callback()[/B] throws Exception {
    			// when ProE JLink button is pushed		
    		PDFexport.writeLog("btn callback succeeded");
    		}
    	public static void Macros() throws Exception {
    
    		if (iii==nrRows+1){
    			//means Jdialog box is manually closed and abort the export operation
    		}
    		else{
    				[B]for(int i=0;i<iii;i++){
    					writeLog(Integer.toString(iii) + Integer.toString(i));
    					writeLog("test");[/B]
    					}			
    			}
    		}
    		
    		public static class Btn8_CmdListener extends DefaultUICommandActionListener {
    			// Jlink button actionlistener
    			public void OnCommand() {
    				try {
    					Btn_callback();
    					pdfDia.CreateandShow();	
    				}
    				catch (Exception e) {
    					PDFexport.writeLog("Exception throw by Btn1_callback method: " + e.toString());
    				}
    				writeLog("end jlink button");
    			}
    		}
    		public static class pdfDia extends JDialog {
    
    			static pdfDia dialog;
    			static JButton butt;
    			
    		        public static void CreateandShow() {
    				
    							butt=new JButton("Export to PDF");
    							dialog=new pdfDia();
    							dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
    							dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    							dialog.pack();
    							dialog.setVisible(true);
    						
    			}
    			public pdfDia() {
    				setBounds(100, 100, 450, 300);
    				getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    				
    				for (int a=0;a<nrRows;a++){
    					getContentPane().add(chbx[a]);
    				}
    				
    				butt.addActionListener(new ActionListener() {
    					
    					public void actionPerformed(ActionEvent arg0) {
    						String [] chbxText=new String[iii];
    						PDFexport.writeLog("btn clicked");		
    						iii=iii-1;
    						for (int ia=0;ia<nrRows;ia++){
    								if (chbx[ia].isSelected()==true){
    									chbxText[ia]=chbx[ia].getText();
    									PDFexport.writeLog("get text" + chbxText[ia]);
    							}
    							else{
    								chbxText[ia]="";
    							}	
    							}
    						dialog.dispose();
    						try {
    							PDFexport.Macros();
    						} catch (Exception e) {
    							PDFexport.writeLog("could not exe macros :" +e.toString());
    						}
    					}
    				});
    				getContentPane().add(butt);
    			}
    		}
    }
    Last edited by Arno; 03-27-2013 at 05:44 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: code jumps out of For loop and picks up later.. why?

    Please post your code inside [code] tags [/code].
    Unformatted code is hard to follow.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Arno is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: code jumps out of For loop and picks up later.. why?

    I was looking for that, I edited it (forgive me for my first post ^^)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: code jumps out of For loop and picks up later.. why?

    What's DefaultUICommandActionListener ?
    How does that function?
    Is it coming into that code via another thread?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: code jumps out of For loop and picks up later.. why?

    Quote Originally Posted by Arno View Post
    Hi, i'm pretty new to Java, ...
    Thread moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Arno is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: code jumps out of For loop and picks up later.. why?

    Well, the application is a synchronous application, created with an API that is part of a CAD program (Pro/E). When it starts, a menubutton is created with a UICommand. and this is done
    Java Code:
    UICommand Btn8_cmd = session.UICreateCommand("Export", new Btn8_CmdListener());
    but that all should work fine, since it works well in other applications i've written. So when the menubutton is pushed, Btn8_CmdListener.OnCommand() is being executed

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: code jumps out of For loop and picks up later.. why?

    I'm still trying to figure out the flow.

    You push a button which executes the OnCommand.
    This creates a dialog with a button.
    Do you then push that button before the OnCommand finishes execution?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Arno is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: code jumps out of For loop and picks up later.. why?

    Well that's where i don't know the details of how the code behaves, but it seems like it creates the dialog with the button and then waits until the button in the dialog is pushed. It does not seem that the OnCommand finishes while the dialog is still visible. I have to use the Modal type to get it that way. The application can not handle multiple threads

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: code jumps out of For loop and picks up later.. why?

    Well, it doesn't have multiple threads.
    The GUI runs on a single thread.

    If you pop open a dialog on a button press then it tends to wait for the dialog to be finished with before continuing.

    If you ave multiple, non-GUI, things to do then you have to launch your own thread to do them on.
    That's what SwingWorkers are for.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Arno is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: code jumps out of For loop and picks up later.. why?

    So, it works like it has too for that part, but why does it not completely complete the For Loop in "Macros()" before ending Btn8_CmdListener?
    you can see i write a log to see what's happening and the result on the end is like this: (example for a for loop of 2 times)
    1) writeLog(Integer.toString(2) + Integer.toString(0));
    2) writeLog("end jlink button"); this is the weird one
    3) writeLog("test");
    4) writeLog(Integer.toString(2) + Integer.toString(1));
    5) writeLog("test");

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: code jumps out of For loop and picks up later.. why?

    How does writeLog work?

    TO be honest I really can't follow the flow in the way the code is written.
    It's quite possible the OnCommand is actually in its own thread.
    I don't know the software you are using, so couldn't say what it is doing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Arno is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: code jumps out of For loop and picks up later.. why?

    Java Code:
    log = new FileWriter(programName + ".log");
    newline = System.getProperty("line.separator");
    
    public static void writeLog(String msg) {
    		try {
    			if (log == null) { return; }
    			log.write (msg + newline);
    			log.flush();
    		}
    		catch (Exception e) {
    			// ignore
    		}
    	}
    hmmm that's a bummer, i was hoping it was some kind of noob Java code mistake

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: code jumps out of For loop and picks up later.. why?

    Well, the structure of your code means it's hard to see why.
    And not knowing (as I said) what the framework you are using actually does it's impossible to say.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JFreeChart XYLinePlot curve jumps horizontally
    By getName() in forum Advanced Java
    Replies: 9
    Last Post: 09-09-2010, 12:06 AM
  2. Scrollbar jumps back after setting thumb
    By swtpete in forum SWT / JFace
    Replies: 0
    Last Post: 11-02-2009, 01:42 PM
  3. .nextLine(); only picks up first word
    By ethanemc505 in forum New To Java
    Replies: 1
    Last Post: 10-08-2009, 08:30 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
  •