Search code examples
javaswingjlabeljtextfield

testing suite with swing


I'm started to make a "testing suite" in java. I'm currently working on sentence completion type tests. Basically I have a text with placeholders, where the user should put in some text, and which will be evaluated later. I found out that by using FlowLayout I could fit the JLabels and JTextFields after each other. The problem is when a block of text is too long. It should span into multiple lines, and I'm not sure how to actually do that. And while that's okay, if I push a small text from the end of the line to a new line, but I'm still stuck if the whole text block is longer than the line width.

And I don't want to reinvent the wheel, so is there any opensource libraries for testing suites? My googlefu failed.

the desired output


Solution

  • The best solution I've found to this problem is using Rob Camick's WrapLayout

    WrapLayout is essentially an extension of FlowLayout which wraps the content when it can no longer fit horizontally.

    Check out the linked blog above as it explains why your having the problems you are.

    Updated

    Another option would be to use JTextPane and insert fields into, for example...

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.HeadlessException;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.StyledDocument;
    
    public class TestText {
    
        public static void main(String[] args) {
            new TestText();
        }
    
        public TestText() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JTextPane tp = new JTextPane();
                    tp.replaceSelection("Asd, asd, asd, fgh ");
                    addField(tp);
                    tp.replaceSelection(" more funky text here ");
                    addField(tp);
                    tp.replaceSelection(" and this must wrap on the edge. The color code of red is: #");
                    addField(tp);
                    tp.replaceSelection(". ");
                    tp.setEditable(false);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(tp));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
                protected void addField(JTextPane tp) {
                    JTextField field = new JTextField(10);
                    field.setAlignmentY(0.75f);
                    tp.insertComponent(field);
                }
            });
        }
    
    }
    

    Note, the editor itself is not editable, but the text fields are...