-1

I would like to transfer a large String from a JTextField to a JLabel,but it is not displaying as expected

E.g:

wwwwxxyy...

As upon reaching the end of the JLabel it stops, but i would like it to continue to the next line

Please guide me

My code:

String url=txturl.getText();
String uname = txuser.getText();
String pwd = pass.getText();
pass.setVisible(false);
txuser.setVisible(false);
txturl.setVisible(false);
JLabel path=new JLabel();
JLabel username=new JLabel();
JLabel password=new JLabel();
path.setText("path is:"+url.toString());
username.setText("usename is:"+uname);
password.setText("password is:"+pwd);
Peter
  • 1,592
  • 13
  • 20
balu
  • 444
  • 1
  • 3
  • 10
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Judging from the uncompilabel code snippets seen above, though, I'd suggest to use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556), to change the view of the container. – Andrew Thompson Jun 24 '16 at 12:41

1 Answers1

2

If you want to display a lot of text, then a JLabel is not the best choice. A suitable alternative would be a JTextArea due to the word wrapping capabilities

For example, using Lorem Ipsum as the text to display and the text area set to wrap text with: textArea.setLineWrap(true); you can see the benefits:

JLabel vs JTextArea

(Include textArea.setEditable(false); if you don't want the contents to be modifiable by the user)

For more information see: How to Use Text Areas


Another point to note is that judging by this line: String pwd = pass.getText();, you are not using a JPasswordField as getText() is deprecated. If you are then consider switching to getPassword()

If not, you should consider the benefits of using a JPasswordField and reading up on how to use them here: How to Use Password Fields

A key benefit being that the password isn't visible during typing:

JTextField vs JPasswordField

Peter
  • 1,592
  • 13
  • 20