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);
Related
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.
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.
I am writing a code, where i have one textfield & another one is textarea with seperate scrollpane for each.
Now what i need is i want to set the size of scrollpane such that it should automatically move my cursor to next line, whenever user reaches the end of first row of textarea or textfield. Please tell me if anyone can help me out.
Below the code to set scrollpane for textarea & textfield:
JScrollPane talkPane = new JScrollPane(talkArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
talkPane.setViewportView(talkArea);
JScrollPane inputPane = new JScrollPane(inputField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
A text field only displays a single line of text so this makes no sense for a text field. You generally don't add a text field to a scrollpane. The user just uses the caret to scroll forwards/backwards to see more text.
For a JTextArea you can turn wrapping on. Then the text will automatically wrap to the next line along with the caret as your type.
textArea.setLineWrap(true);
Now what i need is i want to set the size of scrollpane
You don't set the size of a scrollpane. You give the scrollpane a hint by doing:
JTextArea textArea = new JTextArea(row, columns);
Then the viewport of the scrollpane will be sized based on the row/column you specify.
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.
I want to add different buttons, vertically stacked, to a JPanel at run-time and use a JScrollPane so that all buttons will be visible (with some scrolling).
In order to do this, I have added my JPanel to a JScrollPane, after which I add buttons to my JPanel.
However, when I do this the vertical scrollbar does not allow me to see all images. For example when I add 7 buttons I can only scroll to see 5 full images and half of the 6 images.
Why doesn't my scrollbar allow me to display all 7 buttons?
Create the panel and scrollpane like:
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane( panel );
When you add buttons to the panel at run time the code should be:
panel.add( button );
panel.revalidate();
As long as you are using a layout manager the preferred size will be recalculated and the scrollbar will appear.
Make scroll pane a wrapper over your panel - new JScrollPane (myPanel) and add it instead of naked panel in your panel's container.
You also may want to play with its setPreferredSize() method.