2

I want to create a popup user input getter that will show a table like field with multiple text lines.

As an example, I would want a window with 10 rows of text lines and a label to the left side saying what each entry is, and would want to populate it with some default value.

Kind of like this but just the left table and I can modify the white boxes and press confirm.

Is this possible in Swing?

Abra
  • 19,142
  • 7
  • 29
  • 41
Baiqing
  • 1,223
  • 2
  • 9
  • 21
  • 4
    Yes it is. I would make a `JPanel` with the required input components on. Then you could add that to a `JDialog` or a `JOptionPane` – g00se May 09 '22 at 18:33
  • 1
    Use a `JTable` to display the data. You can control which columns are editable. – camickr May 09 '22 at 19:44
  • 2
    Refer to [How to Use Tables](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html) which is part of Oracle's Java tutorials. – Abra May 10 '22 at 03:44

1 Answers1

1

Refer to How to Use Tables [https://docs.oracle.com/javase/tutorial/uiswing/components/table.html] which is part of Oracle's Java tutorials. – @Abra

With this tutorial, I was able to see most of what you are looking for, but it is asking for the names to go on the top with the Object Array like so:

// [rows][columns]
JTable(Object[][] rowData, Object[] columnNames)

It appears that may be the only way to set names in a JTable (column instead of by row). You mentioned a label to the left side saying what each entry is and that gave me the idea of using the html tag as shown here:

How do I put html in a JLabel in java? -- NOTE this syntax is not exclusive to the JLabel

And because I don't see it on that page, I'll include one more piece of information. In order to have multiple lines, you will need to use <br> (break) like so:

<html><tagHere>"first row"<br>"second row"<br>"third row"</tagHere></html>

Or you could set the table one column wider than you need, set the contents for rowData[rowIndex][0] to be the description, and keep that from being edited.

Kemper Lee
  • 276
  • 4
  • 10