0

I am new to GUI and I am trying to print a table in a database on the GUI window, I used the JTextArea in order to allow new lines on the window.

However, when the GUI shows up, I can easily erase them with my cursor! How can I prevent that?

String toPrintOnGUI = "";
while (resultSet.next()) {
    for (int i = 1; i <= columnsNumber; i++) {
        toPrintOnGUI += (resultSet.getString(i) + " \t");
    }
    toPrintOnGUI = toPrintOnGUI.substring(0, toPrintOnGUI.length() - 1);
    toPrintOnGUI += "\n";
}
JTextArea label = new JTextArea(toPrintOnGUI);
label.setBackground(Color.WHITE);
panel.add(label);
frame.setVisible(true); 

camickr
  • 321,443
  • 19
  • 166
  • 288
Najwa K. Semdina
  • 31
  • 1
  • 1
  • 4

1 Answers1

2

I am trying to print a table in a database on the GUI window

Typically you would use a JTable for something like this so the data is nicely formatted. See: How to get a DefaultTableModel object's data into a subclass of DefaultTableModel for a basic example.

I can easily erase them with my cursor! How can I prevent that?

But if you really want to use a JTextArea then you can make the text area non-editable:

label.setEditable( false );
camickr
  • 321,443
  • 19
  • 166
  • 288