Add a JScrollPane to a JLabel - java

I can't seem to find out how I can add a JScrollPane to a JLabel. The JLabel that I'm using is populated with a long formatted HTML string. Please help.
area = new JLabel();
JScrollPane scroller = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);

Really not good idea to hold or display long Html formatted text in the JLabel, since is possible, better would be use JEditorPanes / JTextPanes, these JComponets support styled and html formatted text, Icons etc ... , examples for JTextPane and JEditorPane

Can you provide us your code? Are you setting the viewport view to the JLabel? Instantiate your JLabel and a JScrollPane. then set the JScrollPane viewport to the JLabel (setViewPortView(jlabel);) then add the JScrollPane to whatever component you want the scrolling JLabel to be on.
Hope this helps!

You can't add a JScrollPane to a JLabel , what you can do is to create a JScrollPane and add a JLabel.
See this: http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node63.html

You need to set the JScrollPane's viewport view:
scroller.setViewPortView(area);
stick that line just before you go panel.add(scroller);
Let us know if that helps or not.

Related

Resizing JTextArea while using lineWrap

I want to create three JTextArea in my swing application.
Each JTextArea has a different size.
The first JTextArea should have 8 columns
The second one should only have 1 column
And the last one should have 50 columns.
My initial problem is that:
Whenever I type something, the JTextArea will keep on re-sizing its width.
This has been fixed by JScrollPane, setLineWrap(true), and setWrapStyleWord(true).
So here's my problem.
Whenever I add setLineWrap() to a JTextArea, the JTextArea will be resized.
My first and second JTextArea have been resized to 12 columns.
I searched and found some solution but they use MigLayout.
Is there any way to add word and line wrap in JTextArea without resizing it (and ofcourse, without the use of MigLayout)?
What's the easiest way to set the columns of JTextArea with word and line wrap?
What's the easiest way to set the columns of JTextArea with word and line wrap?
You create the JTextArea with code like:
JTextArea textArea = new JTextArea(5, 50);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel panel = new JPanel();
panel.add(scrollPane);
frame.add(panel, BorderLayout.PAGE_START);
By default a JPanel uses a FlowLayout which respects the size of any component added to it. The BorderLayout.PAGE_START will repect the height of any component added to it.
Scrollbars will appear as required what text is added to the text area. So the key is to use a layout manager (or combination of layout managers) that meet your requirement.

JTextArea with JScrollPane isn't scrolling once full

I'm trying to make my JTextArea scrollable when it fills up with text but when I add a JScrollPane it just adds a scrollbar that doesn't do anything. When I add more text than my JTextArea can display it doesn't change and doesn't append any more text.
Container window = getContentPane();
window.setLayout(new FlowLayout());
display = new JTextArea(TEXT_AREA_ROWS, TEXT_AREA_COLUMNS);
display.setLineWrap(true);
display.setPreferredSize(TEXT_AREA_DIMENSIONS);
display.setBackground(TEXT_BG_COLOR);
display.setForeground(TEXT_COLOR);
display.setEditable(false);
display.setFont(TEXT_FONT);
window.add(display);
scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setPreferredSize(display.getPreferredSize());
window.add(scroll);
Thanks in Advance
EDIT: Realised my mistake, I was setting the preferred size of the textArea instead of the scrollPane.
This is solved by removing display.setPreferredSize(TEXT_AREA_DIMENSIONS); and adding a scroll.setPreferredSize(new Dimension(width, height)); Silly me.
but if you append text to it with display.append(string) then the text gets added to the bottom which may or not be on screen at the time.
Well, you didn't state that you were using the append(...) method in your original question. That is why you should always post a proper SSCCE that demonstrates the problem so we don't have to guess what you are doing.
See Text Area Scrolling for the probable problem and a solution.

Display Form in ScrollPane

I created a form. Actually it is 10 JLabels with each JLabel having a text field next to it.
consider,
JLabel_called_Name JTextField_to_obtain_name
JLabel_called_Phone JTextField_to_obtain_phone_number
and so on..
I usually position this in a JPanel and display it in a frame. But my panel and frame have height smaller than the size required to hold 10 of these Labels and Textfields.
So I wish to add them to a JScrollPane.
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box,
or adding JLabels to a JList.
However I would like to represent it in the format I showed above. A Jlabel beside a JTextField.
How can one acheive this?
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box, or adding JLabels to a JList.
You can add any component to a JScrollPane:
JPanel = new JPanel();
panel.add( label1 );
panel.add( textField1 );
JScrollPane scrollPane = new JScrollPane( panel );
The trick is choosing the correct layout manager for you panel. Read the Swing tutorial on Layout Managers to help you decide how to design the panel. You can also nest panels to get your desired layout.

Vertical scrollpane problem in JDialog

I am doing a project in Java Swing. I have a JDialog which contains lot of text. I want the JDialog to be small and have a vertical scrollbar so that user can scroll to see the information in the JDialog. How can I get a vertical scrollbar for my JDialog?
Any suggestions would be helpful.
As you have long text use JtextArea with word wrap in it. Add that text area to JScrollPane and add scrollpane to your jdialog and its done. Now you will scroll when your text grows.
Example:-
JTextArea jTextArea = new JTextArea();
jTextArea.setWrapStyleWord(true);
jTextArea.setLineWrap(true);
JScrollPane jScrollPane = new JScrollPane(jTextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

setBorder on JTextField does not work ? or does it?

The code is like this:
JTextField txt = new JTextField();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
However the text field is ignoring my call to setBorder.
No changes whatsoever.
I were to replace it with a JLabel (for instance)
JLabel txt = new JLabel();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
I would see the red border.
Can anybody tell me why? Or even better explain to me how to add a border in the JTextField?
Check out this explanation/recommendation from the Java API
In general, when you want to set a
border on a standard Swing component
other than JPanel or JLabel, we
recommend that you put the component
in a JPanel and set the border on the
JPanel.
So... you should nest your JTextField in a JPanel or JLabel, and put the border on the JPanel or JLabel. Voila!

Categories