G4P textarea row selection issue

edited October 2014 in Library Questions

Hello dear Processing community!

I am currently using the G4P tool for GUI creation and I can say it works great (thanks @quark), but I have just one issue with the textarea;

I want to provide the user a row selection; when the user clicks on a row with text it should mark the whole text and return the developer the line of row. But it seems like there is no way to make this, i found this article but its not possible to add this mouselistener to the textarea.

Best Regards!

jtextarea row selection solution

 textArea.addMouseListener(new MouseAdapter() {
         @Override
         public void mouseClicked(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
               return;
            }
            if (e.getClickCount() != 2) {
               return;
            }

            int offset = textArea.viewToModel(e.getPoint());

            try {
               int rowStart = Utilities.getRowStart(textArea, offset);
               int rowEnd = Utilities.getRowEnd(textArea, offset);
               String selectedLine = textArea.getText().substring(rowStart, rowEnd);
               System.out.println(selectedLine);

            } catch (BadLocationException e1) {
               e1.printStackTrace();
            }

         }
      });
Tagged:

Answers

  • edited October 2014 Answer ✓

    When I first read your question I wasn't sure whether it was possible or not, so I tried creating a simple Processing sketch to test out my ideas.

    My ideas worked out so YES IT IS POSSIBLE and the sketch code is shown below.

    BTW Don't try and use JSwing components inside Processing they don't work well because they use different event handing models.

    import g4p_controls.*;
    
    String[] lines = new String[] {
      "Mary had a little lamb", 
      "its fleece as white as snow", 
      "and everywhere Mary went", 
      "the lamb was sure", 
      "Hickory dickory clock", 
      "The mouse ran up the clock.", 
      "The clock struck one,", 
      "The mouse ran down,", 
      "Hickory, dickory, dock.", 
      "Humpty Dumpty sat on a wall.", 
      "Humpty Dumpty had a great fall.", 
      "All the king’s horses and all the king’s men,", 
      "Couldn’t put Humpty together again.",
      "Horsie, horsie don‛t you stop",
      "Just let your feet go clippety clop,",
      "Your tail goes swish and the wheels go round,",
      "Giddy up we‛re homeward bound"
    };
    
    
    GTextArea ta;
    GLabel lbl;
    import java.awt.Color;
    
    String selectedText;
    
    void setup() {
      size(480, 360);
      lbl = new GLabel(this, 40,20,300,20);
      lbl.setOpaque(true);
      lbl.setTextAlign(GAlign.LEFT, GAlign.MIDDLE);
    
      ta = new GTextArea(this, 40, 60, 300, 150, G4P.SCROLLBARS_VERTICAL_ONLY);
      String s = join(lines,"\n");
      ta.setText(s);
    }
    
    void mouseClicked(){
      // If we detect a click and the textarea has focus then 
      // the click was over the textarea
      if(ta.hasFocus()){
        // Get the text insertion point [line no, char no]
        int[] p = ta.getCaretPos(); 
        // Clear any existing styles
        ta.clearStyles();
        // Highlight the line (white text on black backgorund)
        ta.addStyle(G4P.BACKGROUND, Color.BLACK, p[0]);
        ta.addStyle(G4P.FOREGROUND, Color.WHITE, p[0]);
        // Get the text on the highlighted line
        selectedText = ta.getText(p[0]);
        // Display the selected text in a label
        lbl.setText(selectedText);
      }
    }
    
    void draw() {
      background(200, 180, 210);
    }
    
  • edited October 2014

    Thank you @quark, I didn't have time lately to test your code but I will for sure let you know if I managed to get it working. Thanks again for your time and great tool!

    Regards!

  • edited November 2014

    Hey @quark. I implemented your code, that is exactly what I was asking. Its impressive that it also works on your G4P tool.

    Only thing which I think could make a issue is the text editing, it should be disabled but if you disable it the the focus is gone. Do you have a "overkill" idea? XD

    Cheers!

  • Unfortunately there is no way round it - focus is really keyboard focus in other words i responds to keyboard events and the text is editable. If focus is lost then the text is not editable. The solution I provided above was based on detecting where the text insertion point (caret) is, which means it must be enabled and have focus.

    You must remember that the GTextArea class was designed for simple multi-line text input. Since it was created many people have tried using it for a variety of tasks and some methods have been added which make them possible, but there will always be some limitations and this is one of them.

  • Well it serves the purpose, the problem is still solved even if you can edit the text. As I said maybe there is a "overkill" idea heheh

    Kind regards!

Sign In or Register to comment.