I want to add text to a JTextArea, and to have an auto scrollbar on vertical.
but when typing horizonally, I want an auto new line when there is no space in line..
If I use only JTextArea it's OK, but when I put it in a JScrollPane, it is not
make a new line when needed.
How can I do that?
Thanks!
you have to configure the textArea to wrap:
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
You might consider reading a basic tutorial to get you started effectively :-)
By default, a JTextArea will not wrap text so you have to manually define that behavior:
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
Also, make sure you're setting up the JScrollPane correctly:
JScrollPane sp = new JScrollPane(textArea);
//JScrollPanes are just like JPanels (except for the scrollbars) so be careful not to just add the JComponent to your frame; add the container instead.
frame.add(sp);
As a side note, read the tutorial #kleopatra so helpfully suggested to get a good solid base on textareas.
Isn't JTextArea implementing the Scrollable interface? So why you need JScrollPane?
Edit to your comment, this one works for me:
JScrollPane sP= new JScrollPane(txtarea);
sP.setBounds(10,60,780,500);
sP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Related
Based on existing solutions, I haven't found a way to make a JTextPane (or JEditorPane) have either word-wrap or forced line-wrap inside a JScrollPane. JTextArea is not a solution as I need HTML to display in the particular input.
output = new JTextPane();
scrollPane.setViewportView(output);
contentPane.add(scrollPane, gbc_scrollPane);
output.setEditorKit(new WrapEditorKit());
output.setForeground(Color.WHITE);
output.setFont(new Font("Courier New", Font.PLAIN, 12));
output.setEditable(false);
output.setContentType("text/html; charset=UTF-8");
output.setBackground(Color.DARK_GRAY);
The WrapEditorKit is the one available here. I have also tried ScrollablePanel, but every time the JTextPane's text is not wrapped at all. How would one go around to implement a JTextPane inside a JScrollPane with only vertical scrolling (and horizontal word or forced line wrap)?
It is apparently caused by the <pre> tags rather than any other setting. Without the <pre> tags, the text seems to wrap properly. I originally added them to prevent an another issue with newlines, but I'll try to fix it using some other method then. This could've never have happened with some more advanced HTML skills.
Some 6 years later I had the same problem. Mine was not caused by <pre> tags - didn't have them in the text.
Solution:
Subclass JEditorPane or (JTextPane which is a subclass of JEditorPane) and override boolean javax.swing.Scrollable#getScrollableTracksViewportWidth().
Returning true from this method forces a Scrollable (such as a JTextPane) to wrap its contents instead of activating horizontal scroll if the enclosing JScrollPane.
According to the docs should work with all them Scrollables:
DefaultTreeCellEditor.DefaultTextField, JEditorPane, JFormattedTextField, JLayer, JList, JPasswordField, JTable, JTextArea, JTextComponent, JTextField, JTextPane, JTree.
So, I'm wondering how I would go about colouring certain words in my scrollpane. After doing some research I have found almost nothing helpful. What is there to help with colouring certain words?
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
txtInfo = new JTextArea("Bot In Progress...\n");
txtInfo.setColumns(35);
txtInfo.setEnabled(false);
txtInfo.setDisabledTextColor(Color.black);
scrollPane.setViewportView(txtInfo);
So, here is my scrollpane. What I have it doing now is accepting outputs from part of my program and displaying them. Certain words that I want coloured are Win!(Green) and Loose(Red), and maybe some more later.
The reason why a scrollpane works the best for me is because I need the log to update constantly and the user is supposed to be able to look back up at the log at any time. Is there a better way of storing that information or what?
I don't think you can do it with JTextArea instead you can do it using JTextPane. You can then add the JTextPane to your JScrollPane. Have a look at this site http://javatechniques.com/blog/setting-jtextpane-font-and-color/ where it explains how to do this.
I want to add table2 into the scrollpanel (called feedback) which already has table1 in there. But only one table shows up. If I use feedback.add(table2), only the 1st table shows (I guess the 2nd table is behind the first one, but I don't know how to make the second one below the first one). if I use feedback.getViewport().add(table2, null), only the 2nd table shows. Do I need to use some layout manager here? i tried to search online about scrollpanel layout but didn't get any solutions. Could anyone tell me what's the problem or give me some related example links? Thanks a lot. The relative code are:
content = getContentPane();
content.setLayout(new FlowLayout());
scrollPane = new JScrollPane(tree);
feedback = new JScrollPane(table1);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scrollPane, feedback);
content.add(splitPane);
.
.
.
.
feedback.add(table2);
//i add this, but still doesn't work
content.add(table2);
Only a single component can be added to the "viewport" of a JScrollPane. This is done by using:
JScrollPane scrollPane = new JScrollPane( table );
or
scrollPane.setViewportView( table );
If you want multiple component to appear in a scrollPane then add the component to a panel first and add the panel to the viewport.
Read the JScrollPane API for more information and follow the link to the Swing tutorial as well for examples.
How to implement a panel that would support line wrapping and line breaking?
I would only add textual labels and line breaks to this panel.
The labels should flow from left to right, wrapping to the next "line" if
needed. The line breaks would cause a jump to the next line.
I would also like to make the panel vertically scrollable.
The solution should work in Java 5. SwingX can be used.
Clarification: The textual labels are actually JXHyperlinks (from SwingX),
i.e. the panel contains clickable labels. This is the reason I cannot just use
JTextArea.
UPDATE: I missed the request for hyperlink support. Don't know how to do that w/o using the EditorPane.
JTextArea does exactly what you've described.
JTextArea textArea = new JTextArea();
JScrollPanel sPane = new JScrollPane(textArea);
alt text http://img187.imageshack.us/img187/3238/wraprn0.png
This sample is not from a panel, that is a container, but from a JLabel, that is intended to show content.
You could use HTML in your content and use a <br> on each break. You should programmatically calculate the breaks according with your rules on component resize.
Here's the code:
import javax.swing.*;
import java.awt.*;
public class Wrap {
public static void main( String [] args ) {
JFrame frame = new JFrame("Wrap test");
String text = "<html>This<br>is<br>a<br>multiline<br>label</html>";
frame.add( new JLabel( text ) );
frame.pack();
frame.setVisible( true );
}
}
I found JTextPane, which I had overlooked before for some reason. This class does what I need.
Thanks for your help though. :)
Although it may not be a solution you're in search of, but from the requirements you have, it seems like a custom LayoutManager may be able to achieve what you are after. By designing and assigning a custom Layout Manager which allows line breaks to a Container (such as Panel), it should be possible to have a Panel which allows line breaks.
The Laying Out Components Within a Container article from The Java Tutorials will provide general information on how Layout Managers work in Java, and in particular, the Creating a Custom Layout Manager will provide information on how to make a custom Layout Manager to apply to an Container.
The behavior of the FlowLayout (the default Layout Manager for Panel) seems fairly close to the behavior you may be after. Adding functionality to line break seems like the missing piece.
Suggestion: Perhaps the custom Layout Manager can have the ability to add a line break by having a Component that represents a line break, which can be added to a Container by using the add() method.
For example, have a class constant Component in the custom Layout Manager, such as (a hypothetical) LineBreakLayout.LINE_BREAK, and adding that to the Container can tell the custom layout manager to move to the next line. Perhaps an implementation can be like:
Panel p = new Panel(new LineBreakLayout());
p.add(new Label("First Line"));
p.add(LineBreakLayout.LINE_BREAK);
p.add(new Label("Second Line"));
The above hypothetical LineBreakLayout will then render the first Label in one line and the second Label in the second line.
How do I create a horizontal JSeparator with a title label in Java Swing?
Something like this:
--- Title XYZ --------------------
I found a solution: SwingX JXTitledSeparator. We already use SwingX in our project. I didn't know that SwingX provides a titled separator.
#Bombe Thank you for your help.
Hmm… without any testing and completely from the top of my head:
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder("Title"));
menu.add(panel);
Most Swing containers eat anything so that might even work.
Another approach would be to create a custom component (maybe with a horizontal BoxLayout) and add a JSeparator, a JLabel, and another JSeparator to it, then add it to the menu.