Is this even doable

Hello everyone

I need some inputs from the experts of Swing.

JPanelA, which uses a GridBagLayout has 9 JTextArea with long tests in them such as the one below: "

JTextArea t1 =new JTextArea("Type your message using the form below. When finished,

you can optionally preview your post by clicking on the Preview button. Otherwise,

click the Post button to submit your message immediately.")

JPanelA is embedded in JPanelB.

I don't ever want horizontal scrollbar to appear but I need the tests in the JTextArea to word wrap themselves automatically when i shrink the window and un word wrap (excuse the vocab) themselves when i expand the window.

I used setLinewrap, but it wraps permanently.

Is this doable ?

Message was edited by:

va97

Message was edited by:

va97

[937 byte] By [va97a] at [2007-11-26 18:36:24]
# 1
> Is this doable ?Of course. Web browsers do it.You'll have to build a custom text renderer though
tjacobs01a at 2007-7-9 6:10:26 > top of Java-index,Desktop,Core GUI APIs...
# 2
They do? everytime I am surfing some page, when I shrink the window of IE, a horizontal scrollbar appears. The text don't word wrap all the way. I new at Swing, what is a custom text renderer ?
va97a at 2007-7-9 6:10:26 > top of Java-index,Desktop,Core GUI APIs...
# 3

Actually you've stumbled on something that's a massive ballache to achieve in Java. Yes, it's possible. It's not trivial - I've done it but the solutions I've used behave differently in dialogs vs standard layouts so I've not found a 'one size fits all' fix.

There is a suitable component in the Jide framework, if I recall, but I've not seen the source.

everytime I am surfing some page, when I shrink the window of IE, a horizontal scrollbar appears

Sadly, most people design fixed-width layouts. Find one with a variable-width layout (er, this site for instance, once you get above the minimum width).

itchyscratchya at 2007-7-9 6:10:26 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I used setLinewrap, but it wraps permanently.

No it doesn't. Wrapping is done every time the text area size is changed.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-9 6:10:26 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks camickrI'll create an SSCCE.
va97a at 2007-7-9 6:10:26 > top of Java-index,Desktop,Core GUI APIs...
# 6

import java.awt.Component;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.ScrollPaneConstants;

public class program1 extends JPanel

{

private static final Insets insets = new Insets(0,0,0,0);

private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill)

{

GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 0.0,0.0, anchor, fill, insets, 0,0);

container.add(component, gbc);

}

public static void main(String args[])

{

JFrame frame = new JFrame();

frame.setTitle("Program1");

JPanel mainPanel = new JPanel();

JPanel subPanel = new JPanel();

subPanel.setLayout(new GridBagLayout());

int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;

int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;

JScrollPane scrollPane = new JScrollPane(subPanel,v,h);

String steps[] = {"1. We are told about Bacon's taste for raffish, " +

"lower-class lovers, his penchant for gambling and his almost complete disregard for money.",

"2. Their commentaries inspired generations of schoolboys to pen compositions " +

"in praise of the Spartan lad who flinched not as the fox... ",

"3.Stories not only provide context for statistical statements but canillustrate and vivify them as well."};

for (int i = 0; i < steps.length; i++)

{

JTextArea textArea = new JTextArea(steps[i]);

textArea.setEditable(false);

addComponent(subPanel, textArea, GridBagConstraints.RELATIVE,i, 1,1, GridBagConstraints.NORTH, GridBagConstraints.NONE);

}

mainPanel.add(scrollPane);

frame.add(mainPanel);

frame.setSize(new Dimension(700,200));

frame.setLocation(200,200);

frame.setVisible(true);

}

}

camickr

Here is the SSCCE, about what I was talking about. Although I didn't set the size of subPanel, the tests don't even show all the way. What Am i doing wrong ?

oh and this was written and compile w/ eclipse using jdk 1.6

va97a at 2007-7-9 6:10:26 > top of Java-index,Desktop,Core GUI APIs...
# 7

It doesn't make sense to use multiple text areas. As you change the width of the frame you can cause the text area to wrap or unwrap the text as required. However, the height of the frame will not be affected unless you start adding a ComponentListner to the JFrame and pack() the frame after every change in size of the frame.

So, an easier solution is to simply use a single text area and add each step to a new line in the text area:

import java.awt.*;

import javax.swing.*;

public class program1 extends JPanel

{

public static void main(String args[])

{

JFrame frame = new JFrame();

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setTitle("Program1");

JTextArea textArea = new JTextArea(5, 50);

textArea.setLineWrap( true );

textArea.setWrapStyleWord( true );

textArea.setEditable( false );

JScrollPane scrollPane = new JScrollPane(textArea);

String steps[] = {"1. We are told about Bacon's taste for raffish, " +

"lower-class lovers, his penchant for gambling and his almost complete disregard for money.",

"2. Their commentaries inspired generations of schoolboys to pen compositions " +

"in praise of the Spartan lad who flinched not as the fox... ",

"3.Stories not only provide context for statistical statements but canillustrate and vivify them as well."};

textArea.append(steps[0]);

for (int i = 1; i < steps.length; i++)

{

textArea.append("\n");

textArea.append( steps[i] );

}

frame.getContentPane().add(scrollPane);

frame.pack();

frame.setLocationRelativeTo( null );

frame.setVisible(true);

}

}

camickra at 2007-7-9 6:10:26 > top of Java-index,Desktop,Core GUI APIs...
# 8
Many thanks to You camickr
va97a at 2007-7-9 6:10:26 > top of Java-index,Desktop,Core GUI APIs...