How to set a line border around JTextArea? - java

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

Related

Writing at the bottom of a TextArea with linewrap

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!

Why are all my labels spaced by like 4 tabs while being boderLayout WEST?

I have a JPrame and it it set to borderLayout. Then i created another JPnael which uses BoxLayout. Now i have added labels(which contain text) and textFields to the JPanel. I have also setLayout of JPanel to WEST. Now i dont know why, but I have two problems with what is being displayed.
1) The labels are all indented 4-5 tab spaces, this is so random I dont even know why it is tabbed. All the labels in this panel are like this. Please note that this only happens if I add more stuff to the JPanel. If I only have 1 label in the JPanel it is correctly alligned to the west corner of the screen. Does anyone know why they get indented when i add more elements to the JPanel?
2)The Textfields size are huge, as in they take up many lines. I have set the size of the text fields only to 20. Yet they take up like 5 lines. Why is it not just a single line?
Im sorry guys, I have been trying to fix this for the past 2 hours and dont know what is causing this issue. I would post code, but this is an assignment and I dont feel comfortable posting it on to the internet. I hope you understand.
Just to make things clear, I have a JPanel for example called "aPanel" which is set to BorderLayout then I have another JPanel called 'subPanel' which uses boxlayout and set the layout of that to be west. After this I add stuff to this 'subPanel' expecting the elements to stack over eacher other towards the left border of the JFrame.
1) Read the section from the Swing tutorial on Fixing Alignment Problems. When you add different components to the panel they may have different "X alignment" so you get a different layout than you expect.
2) The box layout will expand to fill the available space. So you need to override the getMaximumSize() method of you text fields to return the preferred height of the text field and the maxiumum width of the text field.
Edit:
Just read the last part where you mention a FlowLayout. If this is what you are using then read the FlowLayout API. It has a parameter that controls left/center/right alignment of components added to the panel.

Java JLabel, break text to next line?

It's been awhile since I asked a question!
I'm developing an application in Java where JLabels are used. Anyway, as you may or may not be able to see from the picture below, the text that says Category Test gets cut off and ends up saying Categor... instead. Is there any way that I can "break" the text once it fills up the width of the label? Here is the image:
What I did
I used a JTextPane like so:
JTextPane text = new JTextPane();
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER);
pane.add(text, c);
Then I added my text, but thanks anyway!
JLabels can't do that by default. But JLabels have some support for html, so a JLabel with the text <html>First Line<br />Second Line</html> would show up on two lines.
If you want a component that can split the lines by itself, take a look at JTextArea.
As I recall, you need to use a JTextArea if you want textwrap. JLabel doesn't do it.
You can at StyledLabel component from JIDE Common Layer open source project at http://java.net/projects/jide-oss/.
The problem with html JLabel approach is it doesn't auto-wrap and about 20 to 40 times slower than a plain JLabel.
The problem with JTextArea or JTextPane approach is it has a weird size issue and is also 20 times slower.
StyledLabel extends JLabel. Automatically line wrapping is just one of the many features it adds. And the performance is as fast as a plain JLabel.
Hope it will help.

JLabel problem with BoxLayout in Java

I have a panel with a BoxLayout declared as follows:
venueInfoPanel.setLayout(new BoxLayout(venueInfoPanel, BoxLayout.Y_AXIS));
When I add two JTextArea to this panel, they all align to the left, which is what I want. However, when I add a JLabel, it aligns itself to the center, instead of to the left. Why is this? How can I make it so it aligns with all the other JTextArea? I read the document here and found out that I use Component.LEFT_ALIGNMENT and I did that by doing
label.setAlignmentX(label.LEFT_ALIGNMENT);
where label is the JLabel I wanted to add to the JPanel
All components need the alignmentX set to left. The tutorial you referenced has plenty of working examples. You should be able to figure it out on your own. If you still have a problem post your SSCCE demonstrating the problem.

Java - Is it possible to put a JLayeredPane inside JScrollPane?

when I try putting my JLayeredPane inside a JScrollPane I get a blank window (with white background) instead of the content I am trying to render (it could be an image, a button, a canvas). Does anyone know of a problem with layout managers that might cause it? Is it possible?
EDIT:
thanks to camickr help, I can now put a JLayeredPane inside a JScrollPane, though now I am facing a different problem:
I am using a very large image and I am trying to put it inside my JLayeredPane that's inside the JScrollPane. From some reason when I use this large image (I am not receiving a heap overflow exception) I get this blank (white screen). Has anyone experienced something like this?
Well this is pretty much a guess because you haven't provided much information, but it sounds to me like the JLayeredPane's preferred size is (0,0), and the "white" you're seeing is the background of the JScrollPane's JViewport child. Try setting a preferred size on the JLayeredPane as a start.
Read the Swing tutorial on How to Use Layered Panes for a working example.
Change the following line:
// add(layeredPane);
add(new JScrollPane(layeredPane));

Categories

Resources