-
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
Code:
/*
* 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();
}
});
}
}
-
Try this, pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
-
Or change to HORI ZONTAL_SCROLLBAR_NEVER in the constructor
Kind regards,
Noel
-
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.
Last edited by yiming1; 02-02-2006 at 02:38 AM.
Similar Threads
-
Replies: 0
Last Post: 08-08-2005, 09:46 AM
-
By Sharky01276 in forum Java
Replies: 1
Last Post: 05-17-2005, 12:43 PM
-
Replies: 3
Last Post: 08-30-2001, 11:45 AM
-
By George Gilbert in forum vb.announcements
Replies: 0
Last Post: 08-19-2001, 11:34 AM
-
By adam adam in forum VB Classic
Replies: 0
Last Post: 12-28-2000, 11:33 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks