I have a JTextArea
in a JDialog
. JDialog
uses a GridLayout
.
I would like to have 7 digits on each line of the JTextArea
(each line will be a 7 int
registration number). For user experience reasons, I would like the JTextArea
to either add a new line or stop expanding when it reaches 7 characters on a line.
Things that didn't work :
- Specifying the number of columns in the JTextArea
constructor
- matriculesText.setLineWrap(true);
and matriculesText.setWrapStyleWord(true);
I'm afraid uploadDialogManager.setHgap(20);
might be breaking the code. I'm wondering if the JDialog
should rather have a fixed size.
This is how I construct my JDialog
:
// initialization
Dialog uploadParent = null;
JDialog uploadDialog = new JDialog(uploadParent);
GridLayout uploadDialogManager = new GridLayout(UPLOAD_DIALOG_ROWS,
UPLOAD_DIALOG_COLUMNS);
// uploadDialog properties
uploadDialog.setSize(new Dimension(UPLOAD_DIALOG_WIDTH, UPLOAD_DIALOG_HEIGHT));
uploadDialog.setLayout(uploadDialogManager);
uploadDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//Set up the horizontal gap value
uploadDialogManager.setHgap(20);
//Set up the vertical gap value
uploadDialogManager.setVgap(20);
//Set up the layout of the buttons
//uploadDialogManager.layoutContainer();
// components initialization
JLabel exerciceLabel = new JLabel("exercice number : ");
JComboBox<Integer> exerciceNumbers = new JComboBox<Integer>
(EXERCICE_NUMBERS);
JLabel matriculeLabel = new JLabel("please enter your matricules, one per
line : ");
JTextArea matriculesText = new JTextArea(1, 1);
JButton confirm = new JButton("confirm");
JButton cancel = new JButton("cancel");
matriculesText.setLineWrap(true);
matriculesText.setWrapStyleWord(true);
I have made another solution that fills my requirements. Instead of going to a new line when 7 characters are reached, I use MATRICULE_REGEX
to check if every line contains 7 digits (1\\d{6}
). If it doesn't, I reject the JTextArea
content.
private static final String MATRICULE_REGEX = "1\\d{6}(\\n1\\d{6})*";
Pattern matriculePattern = Pattern.compile(MATRICULE_REGEX);
Matcher matriculeMatcher = null;
matriculeMatcher = matriculePattern.matcher(text);
isValidMatricule = matriculeMatcher.matches();