Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding text next to a textfield to describe what data the user is expected to enter into it

I am trying to create a simple GUI that simulates a record store. I am still in the beginning stages.

I am running into trouble when I try to add text to describe what the user is expected to enter in the text field.

In addition, I am also having trouble positioning every textfield on its own line. In other words if there is space for two textfields in one line, then it displays in one line, and I am trying to display every text field on its own line.

This is what I tried so far:

item2 = new JTextField("sample text");

However the code above just adds default text within the text field, which is not what I need :/

I appreciate all the help in advance.

public class MyClass extends JFrame{
        private JTextField item1;
        private JTextField item2;

        public MyClass(){
            super("Matt's World of Music");
            setLayout(new FlowLayout());

            item1 = new JTextField();
            item2 = new JTextField();

            add(item1);
            add(item2);

            thehandler handler = new thehandler();

            item1.addActionListener(handler);
            item2.addActionListener(handler);   

        }

    }
like image 500
AnchovyLegend Avatar asked Jan 16 '23 17:01

AnchovyLegend


2 Answers

For your first problem, you need to use a JLabel to display your text. The constructor is like this:

JLabel label = new JLabel("Your text here");

Works really well in GUI.

As for getting things on their own lines, I recommend a GridLayout. Easy to use.

In your constructor, before adding anything, you do:

setLayout(new GridLayout(rows,columns,x_spacing,y_spacing));

x_spacing and y_spacing are both integers that determine the space between elements horizontally and vertically.

Then add like you have done. Fiddle around with it and you'll get it worked out.

So your final would look like:

setLayout(new GridLayout(2,2,10,10));
add(new JLabel("Text 1"));
add(text1);
add(new JLabel("text 2"));
add(text2);
like image 90
brandonsbarber Avatar answered Jan 30 '23 21:01

brandonsbarber


You could just use a JLabel to label your textfields.

    JLabel label1 = new JLabel("Item 1: ");
    add(label1);
    add(item1);

If you really want text inside the fields, you could set the text in the field with the constructor, and then add a MouseListener to clear the text on click:

    item1 = new JTextField("Text");
    item1.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (item1.getText().equals("Text")) // User has not entered text yet
                item1.setText("");
        }
    });

Or, (probably better) use a FocusListener:

    item1 = new JTextField("Text");
    item1.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
            if (item1.getText().equals("Text")) // User has not entered text yet
                item1.setText("");
        }
        public void focusLost(FocusEvent e) {
            if (item1.getText().equals("")) // User did not enter text
                item1.setText("Text");
        }
    });

As for layout, to force a separate line, you use use a Box.

    Box itemBox = Box.createVerticalBox();
    itemBox.add(item1);
    itemBox.add(item2);
    add(itemBox);
like image 24
Alden Avatar answered Jan 30 '23 19:01

Alden