DiscreteTomatoes DiscreteTomatoes - 3 months ago 8
Java Question

Removing border from JTextArea

Hello i'm having a problem where even though i set

JTextArea1.setBorder(null);
or
jTextArea1.setBorder(javax.swing.BorderFactory.createEmptyBorder());
my JTextArea will still have a bevel border (top and left side - thick, bottom and right side - thin). Is there any reason why this is not working? i want absolutely no border of any kind on this text area.

Here is some of my code:

jTextArea1 = new javax.swing.JTextArea();

jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 6)); // NOI18N
jTextArea1.setForeground(new java.awt.Color(29, 209, 32));
jTextArea1.setLineWrap(true);
jTextArea1.setRows(20);
jTextArea1.setText("...");
jTextArea1.setWrapStyleWord(true);
jTextArea1.setAutoscrolls(false);
jTextArea1.setBorder(null);
jTextArea1.setBorder(javax.swing.BorderFactory.createEmptyBorder());
jTextArea1.setOpaque(false);
jTextArea1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jScrollPane1.setViewportView(jTextArea1);

Answer

The border comes from the scroll pane, not the text area.

Another tip:

new java.awt.Font("Monospaced", 0, 6)
  • Change "Monospaced" to Font.MONSPACED for compile time checking.
  • Change 0 to Font.PLAIN both for compile time checking & to be more informative to anyone that needs to look at or work on the code later.

So it would best be:

new java.awt.Font(Font.MONSPACED, Font.PLAIN, 6)
Comments