lines in a text file in Glabel

edited December 2016 in Library Questions

I have saved three lines in a text file and three Glabel, how to save every life in Glabel ??? That is: line 1 in Glabel 1 line 2 in Glabel 2 line 3 in Glabel3

the easiest it will cycle through "for", but as a change in the cycle variable name Glabel?

for(int i=0; i<+2;i++) { ['label'+і].setText(reader.readLine());} So not working

Answers

  • "reader.readLine()"? ['label'+i]? What are those? Preferably give your full code, if you don't have any smaller working sample.

  • edited December 2016
    >  GLabel tn1;
    > GLabel tn2;
    > GLabel tn3; 
    >   GLabel tn- n number
    > tn1 = new GLabel(this, 150, 200, 90, 40);
    > tn2 = new GLabel(this, 150, 200, 90, 40);
    > tn3 = new GLabel(this, 150, 200, 90, 40);
    > tn- n number
    > 
    > BufferedReader reader;
    > reader = createReader("textFile.txt"); // The text file saved n-number of lines
    > String line =reader.readLine();
    > try {
    >   
    >    tn1.setText(line);
           tn2.setText(line);
           tn3.setText(line);
           tn - n .setText(line);
    >   
    >    
    >     
    >   } catch (IOException e) {
    >     e.printStackTrace();
    >     line = null;
    >   }
    >   if (line == null) {
    >     // Stop reading because of an error or file is empty
    >     noLoop();  
    

    How to write a line in the variable GLabel tn1, tn2, tn3 and so on, tn1.setText(line);, through the cycle? How can the loop to create unique objects such GLabel?

  • @gada94 --

    1. Please format your code (do not use >, instead highlight and CTRL-o to indent four spaces). https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest

    2. You appear to be using @quark 's G4P library and the GLabel obect -- is that correct?

    Can you provide a small working sketch that demonstrates the problem? We cannot run / test this code to help you.

  • /* =========================================================
     * ====                   WARNING                        ===
     * =========================================================
     * The code in this tab has been generated from the GUI form
     * designer and care should be taken when editing this file.
     * Only add/edit code inside the event handlers i.e. only
     * use lines between the matching comment tags. e.g.
    
     void myBtnEvents(GButton button) { //_CODE_:button1:12356:
         // It is safe to enter your event code here  
     } //_CODE_:button1:12356:
    
     * Do not rename this tab!
     * =========================================================
     */
    
    public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:203742:
      println("button1 - GButton >> GEvent." + event + " @ " + millis());
    } //_CODE_:button1:203742:
    
    public void textfield1_change1(GTextField source, GEvent event) { //_CODE_:textfield1:530628:
      println("textfield1 - GTextField >> GEvent." + event + " @ " + millis());
    } //_CODE_:textfield1:530628:
    
    public void textfield2_change1(GTextField source, GEvent event) { //_CODE_:textfield2:636904:
      println("textfield2 - GTextField >> GEvent." + event + " @ " + millis());
    } //_CODE_:textfield2:636904:
    
    public void textfield3_change1(GTextField source, GEvent event) { //_CODE_:textfield3:514330:
      println("textfield3 - GTextField >> GEvent." + event + " @ " + millis());
    } //_CODE_:textfield3:514330:
    
    public void button2_click1(GButton source, GEvent event) { //_CODE_:button2:313121:
      println("button2 - GButton >> GEvent." + event + " @ " + millis());
    } //_CODE_:button2:313121:
    
    
    
    // Create all the GUI controls. 
    // autogenerated do not edit
    public void createGUI(){
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      surface.setTitle("Sketch Window");
      label1 = new GLabel(this, 327, 42, 80, 20);
      label1.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      label1.setText("My label");
      label1.setOpaque(false);
      label2 = new GLabel(this, 218, 42, 80, 20);
      label2.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      label2.setText("My label");
      label2.setOpaque(false);
      label3 = new GLabel(this, 115, 41, 80, 20);
      label3.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      label3.setText("My label");
      label3.setOpaque(false);
      button1 = new GButton(this, 296, 185, 108, 29);
      button1.setText("add new 3 textfield");
      button1.addEventHandler(this, "button1_click1");
      textfield1 = new GTextField(this, 194, 184, 80, 30, G4P.SCROLLBARS_NONE);
      textfield1.setOpaque(true);
      textfield1.addEventHandler(this, "textfield1_change1");
      textfield2 = new GTextField(this, 105, 184, 72, 30, G4P.SCROLLBARS_NONE);
      textfield2.setOpaque(true);
      textfield2.addEventHandler(this, "textfield2_change1");
      textfield3 = new GTextField(this, 10, 185, 76, 30, G4P.SCROLLBARS_NONE);
      textfield3.setOpaque(true);
      textfield3.addEventHandler(this, "textfield3_change1");
      button2 = new GButton(this, 140, 284, 183, 30);
      button2.setText("seve to *.txt");
      button2.addEventHandler(this, "button2_click1");
      label4 = new GLabel(this, 39, 9, 408, 20);
      label4.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      label4.setText("After saving the file to open and display three lines stored in the label");
      label4.setOpaque(false);
    }
    
    // Variable declarations 
    // autogenerated do not edit
    GLabel label1; 
    GLabel label2; 
    GLabel label3; 
    GButton button1; 
    GTextField textfield1; 
    GTextField textfield2; 
    GTextField textfield3; 
    GButton button2; 
    GLabel label4; 
    
  • edited December 2016

    Yes,G4P library and the GLabel obect. Here's a sample window!! Pressing the button should be set up facility + 3 "GTextField" and "GLabel"

  • I believe there is no way to access a variable with a String equal to its name in Java, and that is what you're trying to do.
    You will need to shift from G4P's GUI builder and write a lot of code yourself, using Arrays (or maybe ArrayList),if you want what you said.

  • edited December 2016

    I understand, I ask and how it will be easier to write because arrays or lists?

  • edited December 2016

    Remove the semicolon right after the for bracket. You effectively finish the loop right there. The next part of it has no knowledge of the variable i.

  • GLabel[] label; 
    for(int i=0;i<4;i++){
    label[i]= new GLabel(this, 192+i, 227, 80, 20);
     label[i].setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      label[i].setText("My label");
      label[i].setOpaque(false);}
    
    }
    

    Deleted, no errors and does not work, hangs on startup

  • edited December 2016

    Give us your full code - else we couldn't possibly guess the problem.
    For one, you need to add an event handler to each of the buttons.

  •     /* =========================================================
         * ====                   WARNING                        ===
         * =========================================================
         * The code in this tab has been generated from the GUI form
         * designer and care should be taken when editing this file.
         * Only add/edit code inside the event handlers i.e. only
         * use lines between the matching comment tags. e.g.
    
         void myBtnEvents(GButton button) { //_CODE_:button1:12356:
             // It is safe to enter your event code here  
         } //_CODE_:button1:12356:
    
         * Do not rename this tab!
         * =========================================================
         */
    
        public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:567696:
          println("button1 - GButton >> GEvent." + event + " @ " + millis());
        } //_CODE_:button1:567696:
    
    
    
        // Create all the GUI controls. 
        // autogenerated do not edit
        public void createGUI(){
          G4P.messagesEnabled(false);
          G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
          G4P.setCursor(ARROW);
          surface.setTitle("Sketch Window");
          for(int i=0;i<4;i++){
        label[i]= new GLabel(this, 192+i, 227, 80, 20);
         label[i].setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
          label[i].setText("My label");
          label[i].setOpaque(false);}
          button1 = new GButton(this, 188, 145, 80, 30);
          button1.setText("Face text");
          button1.addEventHandler(this, "button1_click1");
        }
    
        // Variable declarations 
        // autogenerated do not edit
    
        GButton button1; 
        GLabel[] label;
    

    "For one, you need to add an event handler to each of the buttons." How to do it?

  • edited December 2016

    You already did that on line 37 of your full code.

  • In any case, the line 31 will put the labels at nearly the same position (differ by 1 pixel).
    Change 192 + i to 192 + (i * 105). You may later fine tune your values to get the best look.

  • As to the error, does it show some error message when it crashes?
    Is so, post it here, and format it just like code.

  • edited December 2016

    in this line stops: label[i]= new GLabel(this, 192+i, 227, 80, 20); NullPointerException

    Could not run the sketch (Target VM failed to initialize). For more information, read revisions.txt and Help ? Troubleshooting.

  • No, not like that. You need to copy the message in the console (hopefully you know what that is) and paste it in the forum, and then format it like code.

  • Answer ✓

    Why don't you use loadStrings and saveStrings methods to load and save a String array to a file instead of BufferedReader. After that the rest should be easy.

  • ===================================================
       G4P V4.1 created by Peter Lager
    ===================================================
    Could not run the sketch (Target VM failed to initialize).
    For more information, read revisions.txt and Help ? Troubleshooting.
    

    The program hangs. That's all writes to the console

  • That's funny. Then I have no idea.

  • edited December 2016

    Did it work! Error here!- GLabel[] label; , Right to GLabel[] label= new GLabel [10];

  • Does your code run now without the JVM error message?

  • edited December 2016

    Yes, it works well. try use loadStrings and saveStrings methods to load and save a String array to a file instead of BufferedReader.

  • edited December 2016 Answer ✓

    Some things about about GUI Builder that will make your code better.

    1) When a control is added it provides default variable name and a default event handler name. You should change these to make it clearer what they are used for.

    2) If a control doesn't need an event handler e.g. a GTextField then delete the handler name in GUI Builder.

    Sometimes it is useful to be able to have arrays of controls but GUI Builder does not support that. What you can do is create your own arrays AFTER the GUI has been created. I have done this in the code below.

    Main sketch tab

    // Need G4P library
    import g4p_controls.*;
    
    GTextField[] tfields;
    GLabel[] labels;
    
    public void setup() {
      size(480, 200, JAVA2D);
      createGUI();
      customGUI();
      // Place your setup code here
    }
    
    public void draw() {
      background(230);
    }
    
    // Use this method to add additional statements
    // to customise the GUI controls
    public void customGUI() {
      // CReate arrays for existing controls this does not create
      // new controls
      tfields = new GTextField[] { txfLine1, txfLine2, txfLine3 };
      labels = new GLabel[] { lblLine1, lblLine2, lblLine3 };
    }
    

    gui.pde tab

    /* =========================================================
     * ====                   WARNING                        ===
     * =========================================================
     * The code in this tab has been generated from the GUI form
     * designer and care should be taken when editing this file.
     * Only add/edit code inside the event handlers i.e. only
     * use lines between the matching comment tags. e.g.
    
     void myBtnEvents(GButton button) { //_CODE_:button1:12356:
         // It is safe to enter your event code here  
     } //_CODE_:button1:12356:
    
     * Do not rename this tab!
     * =========================================================
     */
    
    public void btnSaveClick(GButton source, GEvent event) { //_CODE_:btnSave:456119:
      String[] lines = new String[3];
      for (int i = 0; i < 3; i++)
        lines [i] = labels[i].getText();
      saveStrings("temp-file.txt", lines);
    } //_CODE_:btnSave:456119:
    
    public void btnLoadClick(GButton source, GEvent event) { //_CODE_:btnLoad:385696:
      String[] lines = loadStrings("temp-file.txt");
      for (int i = 0; i < 3; i++)
        labels[i].setText(lines [i]);
    } //_CODE_:btnLoad:385696:
    
    public void btnCopyToLabels(GButton source, GEvent event) { //_CODE_:btnCopy:788266:
      for (int i = 0; i < 3; i++)
        labels[i].setText(tfields[i].getText());
    } //_CODE_:btnCopy:788266:
    
    
    
    // Create all the GUI controls. 
    // autogenerated do not edit
    public void createGUI(){
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      surface.setTitle("Sketch Window");
      lblLine1 = new GLabel(this, 40, 30, 100, 30);
      lblLine1.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      lblLine1.setText("Line 1");
      lblLine1.setOpaque(false);
      lblLine2 = new GLabel(this, 160, 30, 100, 30);
      lblLine2.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      lblLine2.setText("Line 2");
      lblLine2.setOpaque(false);
      lblLine3 = new GLabel(this, 280, 30, 90, 30);
      lblLine3.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      lblLine3.setText("Line 3");
      lblLine3.setOpaque(false);
      txfLine1 = new GTextField(this, 40, 110, 100, 30, G4P.SCROLLBARS_NONE);
      txfLine1.setOpaque(true);
      txfLine2 = new GTextField(this, 160, 110, 100, 30, G4P.SCROLLBARS_NONE);
      txfLine2.setOpaque(true);
      txfLine3 = new GTextField(this, 280, 110, 90, 30, G4P.SCROLLBARS_NONE);
      txfLine3.setOpaque(true);
      btnSave = new GButton(this, 380, 20, 80, 20);
      btnSave.setText("Save");
      btnSave.addEventHandler(this, "btnSaveClick");
      btnLoad = new GButton(this, 380, 50, 80, 20);
      btnLoad.setText("Face text");
      btnLoad.addEventHandler(this, "btnLoadClick");
      btnCopy = new GButton(this, 40, 70, 330, 30);
      btnCopy.setText("^ ^ ^ ^ ^ ^ ^ COPY ^ ^ ^ ^ ^ ^");
      btnCopy.addEventHandler(this, "btnCopyToLabels");
    }
    
    // Variable declarations 
    // autogenerated do not edit
    GLabel lblLine1; 
    GLabel lblLine2; 
    GLabel lblLine3; 
    GTextField txfLine1; 
    GTextField txfLine2; 
    GTextField txfLine3; 
    GButton btnSave; 
    GButton btnLoad; 
    GButton btnCopy; 
    
  • edited December 2016

    Damn it, I should have noticed something so trivial.
    Though I've never seen something like that cause a crash without a proper error message.
    @quark Excellent idea. G4P is your library, right?

  • Yes, I used the library G4P. @Lord_of_the_Galaxy and @quark thank you for helping me without you I would not be able to do their thesis project.

  • Yes I created G4P.

    @gada94 good luck with your theses.

  • @quark Another question, "saveString" stores each array element with a new line. If redo "saveString" is pre-line again overwritten. How to store the text in the last line and did not remove the previous line?

  • Answer ✓

    I assume you want to append the new lines to lines previously saved. Using the code I gave above modify the btnSaveClick function. It loads the existing file contents then adds the new lines before saving hem.

    public void btnSaveClick(GButton source, GEvent event) { //_CODE_:btnSave:456119:
      // Get existing lines
      String[] orgLines = loadStrings("temp-file.txt");
      // create new lines
      String[] newLines = new String[3];
      for (int i = 0; i < 3; i++)
        newLines [i] = labels[i].getText();
      // Append new lines and then save them
      String[] lines = concat(orgLines, newLines);
      saveStrings("temp-file.txt", lines);
    } //_CODE_:btnSave:456119:
    
Sign In or Register to comment.