Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

how to let text go to next line instead of using horizontal scrollbar

Hi,
When i typed a very long text in the JTextPane (the one near the "send" button) and clicked "Send", I expect the text that cannot be accomodated in the size of the JEditorPane to go to the next line, instead of needing to use the horizontal scrollbar.

How can I change the code to make tat happen? My code as below.

Thanks in advance

Regards

/*
* GridBagLayoutDemo.java is a 1.4 application that requires no other files.
*/
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.Timer;
//import javax.swing.tree.*;
import javax.swing.text.html.*;
import java.text.SimpleDateFormat;
import java.awt.Component;
import java.util.Vector;
import javax.swing.text.*;


public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;




public static void addComponentsToPane(Container container) {

JCheckBox checkbox;
JButton see;
JLabel testlabel;
final JEditorPane recv;
final JTextArea type;
JButton send;
final JComboBox JCB;
ComboBoxModel model;


GridBagLayout gbl = new GridBagLayout();
container.setLayout(gbl);
gbl.layoutContainer(container);

GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.HORIZONTAL; //natural height, maximum width


recv = new JEditorPane();
recv.setEditorKit(new HTMLEditorKit());
recv.setEditable(false);

JScrollPane pane
= new JScrollPane(recv,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

pane.setPreferredSize(new Dimension(300,150));

c.weightx = 100.0;
c.weighty = 1.0; //request any extra vertical space
c.gridwidth = 10;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
container.add(pane, c);

c.weighty = 0;

checkbox = new JCheckBox("This is a checkbox", true);
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
container.add(checkbox, c);

type = new JTextArea();
type.setFont(new Font("Arial",Font.PLAIN,11));
type.setLineWrap(true);

JScrollPane typepane
= new JScrollPane(type,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
//JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

typepane.setPreferredSize(new Dimension(300,50));
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 6;
c.fill = GridBagConstraints.HORIZONTAL;
container.add(typepane, c);

send = new JButton("Send");

c.fill = GridBagConstraints.NONE;
c.gridx = 3;
c.gridy = 3;
c.ipady = 10;
c.anchor = GridBagConstraints.LINE_END;
container.add(send, c);

String s = "this is testlabel";
testlabel = new JLabel(s);

c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 2;
c.anchor = GridBagConstraints.LINE_START;
container.add(testlabel, c);

see = new JButton("See");
see.setEnabled(false);

c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
c.ipadx = 2;
c.ipady = 2;
c.gridx = 2;
c.gridy = 3;
container.add(see, c);


String[] petStrings = { "Bird", "Cat Cat Cat"," Dog Dog Dog Dog Dog Dog Dog Dog Dog Dog Dog Dog Dog "};
//String[] petStrings = { "Bird", "Cat Cat Cat", "Dog "};
JCB = new JComboBox(petStrings);
JCB.setPreferredSize(new Dimension(300,20));

c.fill = GridBagConstraints.NONE;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 4;
c.anchor = GridBagConstraints.LINE_START;
container.add(JCB, c);

send.addMouseListener(
new MouseListener(){
public void mouseClicked(MouseEvent event)
{

try {
((HTMLEditorKit)recv.getEditorKit()).read(new java.io.StringReader(type.getText()),
recv.getDocument(), recv.getDocument().getLength());
recv.setCaretPosition(recv.getDocument().getLength());
} catch(Exception e){}

JCB.insertItemAt(type.getText(),0);
type.setText("");

}

public void mouseExited(MouseEvent event)
{ //do nothing
}

public void mouseEntered(MouseEvent event)
{//do nothing
}

public void mouseReleased(MouseEvent event)
{//do nothing
}

public void mousePressed(MouseEvent event)
{//do nothing
}
}
);
model = JCB.getModel();
//Object value = model.getElementAt(0);
Object value = new String ("1234567890");
JCB.setPrototypeDisplayValue(value);

}

public static void appendData()
{

}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setSize(550,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the content pane.
addComponentsToPane(frame.getContentPane());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
[6963 byte] By [yiming1] at [2007-11-11 7:53:52]
# 1 Re: how to let text go to next line instead of using horizontal scrollbar
Try this, pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
major at 2007-11-11 22:37:07 >
# 2 Re: how to let text go to next line instead of using horizontal scrollbar
Or change to HORI ZONTAL_SCROLLBAR_NEVER in the constructor

Kind regards,
Noel
noelob at 2007-11-11 22:38:01 >
# 3 Re: how to let text go to next line instead of using horizontal scrollbar
Hi,

It seems that if i pressed a character such that it is one long continuous string of characters (eg. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", the extra characters that cannot be accomodated in the given area will not go to the next line.

Any other ideas?

Thanks in advance.

Regards.
yiming1 at 2007-11-11 22:39:10 >