Resizing JTextArea while using lineWrap - java

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.

Related

Text Field setBounds function is not working on a particular JFrame

I am trying to create a JPanel with 3 text fields. Everything else including buttons is falling into place except for textArea3. The final panel is something like this. As you can see in picture, textArea3 uses entire JFrame instead of following setBounds method.
//Text Area 1
JTextArea textArea = new JTextArea();
JScrollPane jScrollPane1 = new JScrollPane(textArea);
jScrollPane1.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setFont(new Font("Consolas", Font.LAYOUT_LEFT_TO_RIGHT, 20));
JTextArea textArea2 = new JTextArea();
JScrollPane jScrollPane2 = new JScrollPane(textArea2);
jScrollPane2.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
textArea2.setWrapStyleWord(true);
textArea2.setLineWrap(true);
textArea2.setFont(new Font("Consolas", Font.LAYOUT_LEFT_TO_RIGHT, 20));
//Text Area 3
JTextField textArea3 = new JTextField();
textArea3.setFont(new Font("Consolas", Font.LAYOUT_LEFT_TO_RIGHT, 20));
jScrollPane1.setBounds(30,30,300,300);
jScrollPane2.setBounds(30,400,200,200);
//textArea3 is not working
textArea3.setBounds(600,800,100,50);
button2.setBounds(350,30,80,30);
button1.setBounds(350,400,80,30);
frame.add(button1);
frame.add(button2);
frame.add(jScrollPane2);
frame.add(jScrollPane1);
frame.add(textArea3);
frame.setVisible(true);
EDIT: So this was a bug within the JDK probably. I made another class called class frame and set methods to produce text area etc.
As you can see in picture, textArea3 uses entire JFrame instead of following setBounds method.
No I can't see. I see 5 components. I don't see any component that uses the entire frame.
If in fact you do see the text area take up the whole frame that is because:
the default layout manager for a JFrame is the BorderLayout
when you add components to a BorderLayout and don't specify a constraint, the CENTER is assumed.
however only a single component can be displayed in the CENTER so the layout manager will only give a size/location to the last component added, which happens to be textArea3.
The other components only happen to appear because you manually set the bounds of each component.
You should NOT be attempting to set the bounds of a component. It is the job of the layout manager to set the size and location of each component.
So the solution is to get rid of all the setBounds() statement and use layout managers.
Read the section from the Swing tutorial on Layout Managers for more information. It appears you are tying to use a grid so you can probably use a GridBagLayout.
Also when you create a JTextArea you should use something like:
JTextArea textArea = new JTextArea(15, 20);
This will allow the text area to calculate its size so that 15 rows will display with about 20 characters in each row. The size will be calculated based on the Font used.

How to center multiple JLabels on one JPanel and how to set the labels text orientation to centered

I'm trying to make a JPanel in which text is displayed on two JLabels like a movie or book title.
How can I set two labels to be centered on one JPanel (one above and one below)?
Also, is there any way to set the text alignment within the JLabel to centered like when writing a title to a document, book or movie. (author and title are centered headers)
EDIT: This should remain true in fullscreen mode.
EXAMPLE:
JLabel1
Jlabel2withlongertext
OK, adding an answer so that others don't waste time here:
As pointed out by Andrew, this can be achieved by combining GridLayout which can be used to create a column of the labels, and setting the text alignment in the labels themselves:
JPanel panel = new JPanel();
// Setting rows to 0 means that new rows are added as needed
panel.setLayout(new GridLayout(0, 1));
panel.add(new JLabel("text", SwingConstants.CENTER));
panel.add(new JLabel("longer text", SwingConstants.CENTER));
GridLayout makes the labels both the same size, so centering the text inside the labels also centers them relative to each other.

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.

Sizing srollpane on textarea & textfield

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.

Typing in JTextArea affects other components

I'm working on panel which has four components: a label, a textfield that is uneditable, another label and a JTextArea. These components are aligned vertically one after the other and I am using Box Layout for this panel. What I have noticed is that when I type in the text area component, it shifts the labels character by character till it can't anymore. They labels initially are aligned to the left but as soon as I start typing they start moving to the right. I have tried so many other components but Box Layout seems to do what I want, I just have to fix this error. Any one ideas?
This is my panel code:
JPanel Panel = new JPanel();
Panel.setLayout(new BoxLayout(Panel,BoxLayout.Y_AXIS));
Panel.add(new JLabel("just a label here"));
Panel.add(textFieldComponent);
Panel.add(new JLabel("just a label here"));
Panel.add(textAreaComponent);
Use another LayoutManager e.g. GridBagLayout or
Place the JLabel in a panel with Horizontal BoxLayout (or BorderLayout) to actieve desired alignment.
another alternative:
add the textAreaComponent to a JScrollPane (set the scrollPane's alignmentX to 0.0f)
I had this issue as well and I added: textArea.setLineWrap(true). It prevented other objects from being pushed when you type in the field.
You should definitely use another Layout. One of my personal favorite is Forms from JGoodies. I've yet to see a Java Swing layout that comes anywhere close.

Categories