I want to create a console-like visual with text flowing from bottom to top.
I found a very useful solution for that right there:
Writing at the bottom of JTextArea
It works as for one point: this solution does not linewrap.
In the current implementation, if I append a long line at the end of the text, then it scrolls right. See an example here:
What I would like instead, is the "sentence" world to be linewrapped and therefore placed in the next line. And it should not scroll right.
I played around a bit, by adding:
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
But no effect was visible.
When I also added:
ta.setPreferredSize(new Dimension(100, 100));
it linewrapped but now the text does not scroll down any longer...
Can you please help me fixing this issue?
Tried HTML? Something along these lines:
jTextArea.setText("<html><body style='vertical-align:bottom;width:100%;height:100%;'>" +
text);
After re-trying out, it seems that the text does linewrap normally, except for the few pixels below the right scrollbar. When the right scrollbar is visible, then the screen creates a bottom scrollbar for scrolling below the right scrollbar. When the word is long enough, is does linewrap normally/
Thanks camickr!
Related
I have a JTextArea in my panel, but it is hard to distinguish it from the background.
I tried setBound() but it doesn't really help.
Screenshot of my GUI
(The textarea is next to
the 'DESCRIPTION')
Is there any way to have a clear bound around it other than changing the colour of the background? Say having a line bound like what JTextField has(I put one next to 'EXPENSE' in my GUI).
Thanks for the guys in the comment area! I put the textarea into a JScrollpane. It does create a border:
using JScrollPane
Then I also added a line border to make it more clear.
des.setBorder(BorderFactory.createLineBorder(Color.BLACK));
Thanks again to Andrew Thompson, the suggestion about using GridBagLayout does make everything look much better.
Using GridBagLayout
I have got the following basic setup on a Part of my GUI:
A JScrollPane
On it, a JPanel with a BoxLayout (new BoxLayout(tablePanel, BoxLayout.PAGE_AXIS))
And on this Panel, a Bunch ob Panels.
I am trying to scroll to the Panel that has been highlighted... this works ALMOST.
Currenly, if a Panel is only half-visible on the bottom Part, the ScrollPane scrolls to make it fully visible.. great.
If it is half-visible on the TOP part, it does not... I could live with that.
But if a totally invisible Panel at the very bottom is highlighted, the system does not comment, but neither does it scroll there!
if(selectedPanel!=null){
Rectangle targetRectangle = new Rectangle(selectedPanel.getX(), selectedPanel.getY(), selectedPanel.getWidth(), selectedPanel.getHeight());
Rectangle r = scrollPane.getVisibleRect();
if (!r.contains(targetRectangle)) {
tablePanel.scrollRectToVisible(targetRectangle);
}
}
I am unfortunately not 100% sure how it behaves when the second-to-last panel is selected while not visible, because I cannot make that happen without some code-gymnastics; perhaps someone can help with the information I can give at this point.
you have to compare Rectangle from/returns JViewport(visible rectangle from JScrollPane), not from JScrollPane
use selectedPanel.getBounds instead of (selectedPanel.getX(), selectedPanel.getY(), selectedPanel.getWidth(), selectedPanel.getHeight());
still isn't centerred, have to divide JVievports and selectedPanel with 2
the same result as to use single code line JComponentPlacedIntoJScrollPane.scrollRectToVisible(selectedPanel.getBounds())
for better help sooner post an SSCCE/MCVE, short, runnable, compilable
I have a JSplitPane with a vertical split. The top component is a table which is in a JScrollPane, and the bottom component is a detailed description of a row. The bottom component always have the same number of elements, so essentially it stays the same height.
Right now, I am using setDividerLocation to position where the pane is being split. If I put 100 into it, the top portion will be 100px, if 500 then the top will be 500px. But if I re-size the JFrame, then the top component is still 100px and the bottom component is really big.
I was wondering if there is a way to set the divider location based on the bottom component instead of the top component?
Sorry if I didn't explain it well, if what I said is still confusing, please let me know and I can try to explain it better.
------ EDIT ------
Thanks to ControlAltDel for figuring it out. This is what my code looks kinda looks like now:
JSplitPane splitPane = new JSplitPane();
splitPane.setTopComponenet(new JTable());
splitPane.setBottomComponent(new JPanel());
splitPane.setResizeWeight(1); // This gives the top component priority when the window is resized
http://docs.oracle.com/javase/7/docs/api/javax/swing/JSplitPane.html#setResizeWeight(double)
I am Working with Swing in java and I'm making a little tool to compare 2 Files.
The Comparing works, the differences are marked in Red.
So I tough that I could make a kind of Bar next to the ScrollBar to show where I have to Scroll to find de differences in my text. Something like in Eclipse to show where are Errors, Warnings and TODOs
Another possibility coulb be to put the marks into the ScrollBar.
Is that Possible and if Yes, how can I code this? Thank you for your help
Place your scroll pane in a JPanel with BorderLayout. Add one more JPanel's extension to show all the marks and place it to the east (or west). Place the marks on the panel and add a MouseListener to process clicks and scroll to desired positions.
Or you can use custom row/column header. Like this
http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
I have a TextArea in LWUIT that I am having an issue manipulating. I have the following two issues:
Even though I call setIsScrollVisible(true), I do not seem to have a
scrollbar and cannot scroll when the output goes below the visible
area of the container.
How do I tell the TextArea to automatically scroll to the bottom
programmatically?
My code for initializing the TextArea looks like this:
myTextArea = new TextArea(20, Display.getInstance().getDisplayWidth());
myTextArea.setEditable(false);
myTextArea.setEnabled(true);
myTextArea.setIsScrollVisible(true);
myTextArea.setGrowByContent(false);
System.out.println(myTextArea.isScrollableY());
isScrollableY() returns true. Any ideas? Is there something I am missing? There doesn't seem to be anything visible in the API that lets me explicitly enable or disable scrolling. Thanks in advance for any assistance on this.
The width of the text area is in columns NOT pixels as you have in your code.
Setting the scroll to visible won't cause it to appear since LWUIT scrollbars are always "as needed" which means a scrollbar will only appear when necessary, setting this value to false would just hide the scrollbar regardless of necessity.
To have the text area grab the entire width just place it within a box layout Y container/form and the layout manager will stretch it on the X axis.
You can use scrollRectToVisible() to scroll the text area to the bottom or alternatively you can derive text area and use setScrollY(int) with the appropriate value (see the source code of text area for how this is used to scroll the text area.
Try a simple textArea.setFocusable(false). This worked for me.