[request] Input text field or input field that maintains formatting

Asked by dude

I'm wondering if it's possible to make a text field pop-up box like input but with multiple lines that retains formatting.
When I'm copying and pasting stuff into an input field the output always ends up being a single long line and I will would prefer to retain formatting if possible in some cases

thanks

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
RaiMan Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Sorry, not with the current features on the script level.

But since we run on Java, you might try this (tested and works):
# make your own input function
def myInput(title):
    import javax.swing.JTextArea as JTextArea
    import javax.swing.JPanel as JPanel
    import java.awt.Dimension as Dimension
    import javax.swing.JOptionPane as JOptionPane

    ta = JTextArea("")
    ta.setPreferredSize(Dimension(50,50));
    myPanel = JPanel();
    myPanel.add(ta);

    if 0 == JOptionPane.showConfirmDialog(None, myPanel, title, JOptionPane.OK_CANCEL_OPTION):
        return ta.getText()
    else:
        return ""

# and use it like this
given = myInput("gimmi some text")
if given != "":
    print given
else:
    print "no input"

With Dimension(50,50) you set the size of the displayed text area (which may take content of any size).
This is a quick and dirty solution and might be beautified at your needs.

I will add an inputText() function to the scripting API.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

this is a somewhat enhanced version:

def myInput(title, width = 10, lines = 5):
    import javax.swing.JTextArea as JTextArea
    import javax.swing.JPanel as JPanel
    import java.awt.Dimension as Dimension
    import javax.swing.JOptionPane as JOptionPane
    import javax.swing.BorderFactory as BF
    import java.awt.Color as COLOR

    textH = 16
    textW = 10
    inner = 5
    ta = JTextArea("")
    ta.setPreferredSize(Dimension(width*textW,lines*textH + inner*2));
    b0 = BF.createEmptyBorder(inner, inner, inner, inner)
    b1 = BF.createLineBorder(COLOR.BLACK)
    ta.setBorder(BF.createCompoundBorder(b1, b0))
    myPanel = JPanel();
    myPanel.add(ta);

    if 0 == JOptionPane.showConfirmDialog(None, myPanel, title, JOptionPane.OK_CANCEL_OPTION):
        return ta.getText()
    else:
        return ""

given = myInput("gimmi some text")
if given != "":
    print given
else:
    print "no input"

The text area now has a simple border.
You may specify the number of characters (width = 10 default) per line and the number of lines (lines = 5 default) (this is based on the assumption, that a character is 10x16 Pixel.

Revision history for this message
dude (dude1) said :
#3

Awesome, that works great
One thing I wonder I don't know how it would be implemented in sikuli
It starts out with the dialogue focus on the OK button and if you tab twice you're into the JTextArea

Would it be hard to implement that if while in the JTextArea instead of it putting a tab in the JTextArea when you press it cycles back to the OK button and maybe a shift tab will actual do a tab inside the JTextArea or vice versa

thanks again for all your help :-)

Revision history for this message
Best RaiMan (raimund-hocke) said :
#4

Thanks for feedback.
I have already added the feature including a password entry to version 1.1.

Your tabbing request is not possible with the nice and easy implementation using JOptionPane.

But at least on Mac (did not check on Windows yet) using ctrl-tab does what you want.

Revision history for this message
dude (dude1) said :
#5

Thanks RaiMan, that solved my question.

Revision history for this message
dude (dude1) said :
#6

ctrl-tab works on both platforms FYI :D