JTextArea Size

K

Kevin Munro

Hello, I'm writing a java app for a pda and as the screen size is tiny I
want to write a MiniDialog class. I only want a JTextArea and an OK button
on it and I want the JTextArea to fit the size of the modal dialog.

If I pass in a long line of text then the JTextArea appears to get wider and
I have to scroll across the screen to see the text. I want it to wrap onto
the second line etc.

I must be doing something stupid!

Thanks, Kevin.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MiniDialog {

JDialog dialog=new JDialog(new JFrame(),"Info",true);

public MiniDialog() { }

public void show(String label) {
Container c=dialog.getContentPane();

JButton ok=new JButton("OK");
dialog.getRootPane().setDefaultButton(ok);

ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});

JTextArea txt=new JTextArea(5,20);
txt.setEditable(false);
txt.append(label);

JScrollPane scroll=new JScrollPane(txt);
scroll.setSize(1,1);

dialog.setSize(200,200);
dialog.setLocation(20,40);
c.add(scroll,BorderLayout.CENTER);
c.add(ok,BorderLayout.SOUTH);
dialog.show();
}
}
 
A

Alex Hunsley

Kevin said:
Hello, I'm writing a java app for a pda and as the screen size is tiny I
want to write a MiniDialog class. I only want a JTextArea and an OK button
on it and I want the JTextArea to fit the size of the modal dialog.

If I pass in a long line of text then the JTextArea appears to get wider and
I have to scroll across the screen to see the text. I want it to wrap onto
the second line etc.

I must be doing something stupid!

Thanks, Kevin.
[snip]

try doing setLineWrap(true) on the JTextArea.

alex
 
A

Alex Hunsley

Alex said:
Kevin said:
Hello, I'm writing a java app for a pda and as the screen size is tiny I
want to write a MiniDialog class. I only want a JTextArea and an OK
button
on it and I want the JTextArea to fit the size of the modal dialog.

If I pass in a long line of text then the JTextArea appears to get
wider and
I have to scroll across the screen to see the text. I want it to wrap
onto
the second line etc.

I must be doing something stupid!

Thanks, Kevin.

[snip]

try doing setLineWrap(true) on the JTextArea.

Also, when you post code in the future, can you please post a fully
working selfcontained version? What you posted lacked a main method so
can't be run to test it (unless someone writes a main method).

alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top