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]

# 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 >

# 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);
}
}