Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing - how to mix JTextField and JTextAreas and have same visual appearance?

I am using miglayout to create a form in which there are JTextFields (short input answers) as well as JTextAreas (Longer answers). The problem is twofold.

  1. The border placed around a Scrollpane wrapped text area does not match that of a Text Field.
  2. The width and placement of the textarea/textfield differ, causing them not to line up correctly.

alt text http://grab.by/3O0V

After changing from right/left to right/fill: alt text http://grab.by/3RMk You can see that the bounds line up, but that there are still gaps. I tried setting novisualpadding but this did not fix it.

Source code:

package test2;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;

public class Test extends JPanel {

private static final int NUM_CHARACTERS_WIDTH = 20;
private static final int NUM_ROWS = 5;
public Test() {

    setLayout(new MigLayout(
            "wrap 2",
            // Align text labels on the so their right edge meets left edge of the text fields
            "[right][left]"
            ));

    add(new JLabel("Text field:"));
    add(new JTextField(NUM_CHARACTERS_WIDTH));

    add(new JLabel("No scrollpane text area:"));
    add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));

    add(new JLabel("Scrollpane text area:"));
    add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)));

    add(new JLabel("Text field:"));
    add(new JTextField(NUM_CHARACTERS_WIDTH));

}


public static void main(String[] args) {
    JFrame frame = new JFrame("");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new Test();
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

}

What's the preferred way to mix and match jtextfield and jtextareas, while still maintaining visual harmony? I notice now that the text field has a blue highlight around it when focus is in it, as opposed to the text area... another source of visual discontinuity.

like image 808
I82Much Avatar asked Apr 16 '10 17:04

I82Much


People also ask

What is the difference between JTextField and JTextArea?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.

Can you use basic editing operations when entering text in JTextField or JTextArea?

By default, JTextField and JTextArea are editable; you can type and edit in both text components. They can be changed to output-only areas by calling setEditable(false) .

What method should you use to get the value of a JTextField?

As the user inputs text value, it sets the value of the JTextField variable component. We can use a getter method to access this value input by the user, display it on JLabel, store it in a database, or display it in a JTable.

How do I change the shape of a JTextField?

The important methods of a JTextField class are setText(), getText(), setEnabled() and etc. By default, a JTextfield has a rectangle shape, we can also implement a round-shaped JTextField by using the RoundRectangle2D class and need to override the paintComponent() method.


1 Answers

I know this question is pretty old, but to get the border for a TextArea to match that of a TextField:

(myTextArea).setBorder(new JTextField().getBorder());

That should give a border to your TextArea like the one around a TextField.

like image 192
MikeM Avatar answered Nov 09 '22 17:11

MikeM