Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Multi-line Text with constant number of visible rows...
Multi-line Text with constant number of visible rows... [message #451453] Wed, 02 March 2005 15:53 Go to next message
Eclipse UserFriend
Originally posted by: msmiths.uk.ibm.com

Hi,

I am trying to emulate the behaviour that can be achieved in SWING by using
a JTextArea and a JScrollPane together, as follows:

JTextArea payloadWindow = new JTextArea();
payloadWindow.setRows(10);
payloadWindow.setColumns(80);

JScrollPane payloadWindowScrollPane = new JScrollPane();
payloadWindowScrollPane.setViewportView(payloadWindow);
payloadWindowScrollPane.setMinimumSize(new Dimension(400, 400));

However, I cannot find any way to tell a Text control to size itself to a
given number of displayable rows and columns. I have code similar to the
following:

int cols = 80;
int rows = 10;
GC gc = new GC(payloadWindow);
FontMetrics fm = gc.getFontMetrics ();
int width = cols * fm.getAverageCharWidth();
int height = rows * fm.getHeight ();
gc.dispose ();
payloadWindow.setSize(payloadWindow.computeSize(width, height));

The problem with this is that the Text control is initially empty.
Therefore, when I invoke pack() on my dialog, the control gets resized
smaller. Also, if add extra lines of text (>10) and then pack() is invoked
on the dialog, my Text control gets resized larger then 10 rows.

The behaviour that I would like to see is to always have 10 rows by 80
columns, even when the Text control is empty or contains a large amount of
text, and I would like to have scroll bars displayed for the Text control
when they are required, i.e., when more than 10 rows are entered or a line
longer than 80 characters is entered.

Any ideas?

P.S. I have tried to place the Text control inside a ScrolledPane, but
still cannot achieve the desired results.
Re: Multi-line Text with constant number of visible rows... [message #451546 is a reply to message #451453] Wed, 02 March 2005 18:06 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
You probably have a Layout on the shell and this is overriding the result of
payloadWindow.setSize(payloadWindow.computeSize(width, height)); You can
not mix a Layout with calls to setSize() or setBounds() - you must choose
one or the other. Below is an example using GridLayout that sets the size
of the Text widget:

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout(2, false));
Text payloadWindow = new Text(shell, SWT.BORDER | SWT.MULTI
|SWT.V_SCROLL | SWT.H_SCROLL);
GC gc = new GC(payloadWindow);
FontMetrics fm = gc.getFontMetrics ();
gc.dispose ();
int cols = 80;
int rows = 10;
int width = cols * fm.getAverageCharWidth();
int height = rows * fm.getHeight();
GridData data = new GridData();
data.widthHint = width;
data.heightHint = height;
payloadWindow.setLayoutData(data);
Button b = new Button(shell, SWT.PUSH);
b.setText("Button");
shell.pack();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

"Martin Smithson" <msmiths@uk.ibm.com> wrote in message
news:d04nif$ed3$1@www.eclipse.org...
> Hi,
>
> I am trying to emulate the behaviour that can be achieved in SWING by
> using
> a JTextArea and a JScrollPane together, as follows:
>
> JTextArea payloadWindow = new JTextArea();
> payloadWindow.setRows(10);
> payloadWindow.setColumns(80);
>
> JScrollPane payloadWindowScrollPane = new JScrollPane();
> payloadWindowScrollPane.setViewportView(payloadWindow);
> payloadWindowScrollPane.setMinimumSize(new Dimension(400, 400));
>
> However, I cannot find any way to tell a Text control to size itself to a
> given number of displayable rows and columns. I have code similar to the
> following:
>
> int cols = 80;
> int rows = 10;
> GC gc = new GC(payloadWindow);
> FontMetrics fm = gc.getFontMetrics ();
> int width = cols * fm.getAverageCharWidth();
> int height = rows * fm.getHeight ();
> gc.dispose ();
> payloadWindow.setSize(payloadWindow.computeSize(width, height));
>
> The problem with this is that the Text control is initially empty.
> Therefore, when I invoke pack() on my dialog, the control gets resized
> smaller. Also, if add extra lines of text (>10) and then pack() is
> invoked
> on the dialog, my Text control gets resized larger then 10 rows.
>
> The behaviour that I would like to see is to always have 10 rows by 80
> columns, even when the Text control is empty or contains a large amount of
> text, and I would like to have scroll bars displayed for the Text control
> when they are required, i.e., when more than 10 rows are entered or a line
> longer than 80 characters is entered.
>
> Any ideas?
>
> P.S. I have tried to place the Text control inside a ScrolledPane, but
> still cannot achieve the desired results.
>
>
Re: Multi-line Text with constant number of visible rows... [message #451624 is a reply to message #451546] Thu, 03 March 2005 15:30 Go to previous message
Eclipse UserFriend
Originally posted by: msmiths.uk.ibm.com

Veronika,

Many thanks... it now works great. It's not immediately obvious from the
docs that Layouts and setSize() or setBounds() methods are mutually
exclusive.


"Veronika Irvine" <veronika_irvine@oti.com> wrote in message
news:d04vaf$or2$1@www.eclipse.org...
> You probably have a Layout on the shell and this is overriding the result
of
> payloadWindow.setSize(payloadWindow.computeSize(width, height)); You can
> not mix a Layout with calls to setSize() or setBounds() - you must choose
> one or the other. Below is an example using GridLayout that sets the size
> of the Text widget:
>
> public static void main (String [] args) {
> Display display = new Display ();
> Shell shell = new Shell (display);
> shell.setLayout(new GridLayout(2, false));
> Text payloadWindow = new Text(shell, SWT.BORDER | SWT.MULTI
> |SWT.V_SCROLL | SWT.H_SCROLL);
> GC gc = new GC(payloadWindow);
> FontMetrics fm = gc.getFontMetrics ();
> gc.dispose ();
> int cols = 80;
> int rows = 10;
> int width = cols * fm.getAverageCharWidth();
> int height = rows * fm.getHeight();
> GridData data = new GridData();
> data.widthHint = width;
> data.heightHint = height;
> payloadWindow.setLayoutData(data);
> Button b = new Button(shell, SWT.PUSH);
> b.setText("Button");
> shell.pack();
> shell.open ();
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
>
> "Martin Smithson" <msmiths@uk.ibm.com> wrote in message
> news:d04nif$ed3$1@www.eclipse.org...
> > Hi,
> >
> > I am trying to emulate the behaviour that can be achieved in SWING by
> > using
> > a JTextArea and a JScrollPane together, as follows:
> >
> > JTextArea payloadWindow = new JTextArea();
> > payloadWindow.setRows(10);
> > payloadWindow.setColumns(80);
> >
> > JScrollPane payloadWindowScrollPane = new JScrollPane();
> > payloadWindowScrollPane.setViewportView(payloadWindow);
> > payloadWindowScrollPane.setMinimumSize(new Dimension(400, 400));
> >
> > However, I cannot find any way to tell a Text control to size itself to
a
> > given number of displayable rows and columns. I have code similar to
the
> > following:
> >
> > int cols = 80;
> > int rows = 10;
> > GC gc = new GC(payloadWindow);
> > FontMetrics fm = gc.getFontMetrics ();
> > int width = cols * fm.getAverageCharWidth();
> > int height = rows * fm.getHeight ();
> > gc.dispose ();
> > payloadWindow.setSize(payloadWindow.computeSize(width, height));
> >
> > The problem with this is that the Text control is initially empty.
> > Therefore, when I invoke pack() on my dialog, the control gets resized
> > smaller. Also, if add extra lines of text (>10) and then pack() is
> > invoked
> > on the dialog, my Text control gets resized larger then 10 rows.
> >
> > The behaviour that I would like to see is to always have 10 rows by 80
> > columns, even when the Text control is empty or contains a large amount
of
> > text, and I would like to have scroll bars displayed for the Text
control
> > when they are required, i.e., when more than 10 rows are entered or a
line
> > longer than 80 characters is entered.
> >
> > Any ideas?
> >
> > P.S. I have tried to place the Text control inside a ScrolledPane, but
> > still cannot achieve the desired results.
> >
> >
>
>
Previous Topic:Regarding Spinner widget in latest SWT swt-3.1M4-win32
Next Topic:Is there a way to actively "read" keyboard modifier state?
Goto Forum:
  


Current Time: Wed Sep 30 22:00:10 GMT 2020

Powered by FUDForum. Page generated in 0.18283 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top