I'm having an issue with resizing of JTextArea in java.swing. The problem is that once the current line is finished (so for example if i keep pressing space) - it doesn't go to the second line - it just keeps on going. Same thing when i press enter - it stretches out the box vertically. How do I prevent this? I'm using GridBagLayout.
JTextArea MainText = new JTextArea();
MainText.setFont(new Font("Arial", Font.PLAIN, 16));
MainText.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets (10, 10, 10, 10);
c.gridx = 0;
c.gridy = 2;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = 3;
c.gridheight = 1;
panel.add(MainText, c);
Set the lineWrap and wrapStyleWord properties of the JTextArea
JTextArea MainText = new JTextArea();
MainText.setLineWrap(true);
MainText.setWrapStyleWord(true);
Take a look at How to use Text Areas for more details
You might also find having a read through Code Conventions for the Java Programming Language of some use
Unless you really don't want to, I would also suggest adding the JTextArea into a JScrollPane instead of adding it directly to the conatiner
panel.add(new JScrollPane(MainText), c);
This will prevent the JTextArea from wanting to grow as more text is added to it (unless that's what you're going for)
To get your JTextAreas to wrap lines on words appropriately, use: mainText.setWrapStyleWord(true) and mainText.setLineWrap(true)
Most important though, get very familiar with using the Java API as it will likely answer 90% of similar questions:
JTextArea API
Related
I want to make the text automatically as wide as the window. I tried using text.setSize(window.getWidth(),20) and text.setBounds(window.getWidth(),20), (where text is JTextfield), but the only way that seems to work is: static JTextField text = new JTextField(int numberOfColumns); I'm using GridBag layout.
EDIT: I have edited example according to GridBagLayout.
Use layout manager. It will automatically expands component according to window.
For example;
Jpanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = GridBagConstraints.REMAINDER;
panel.add(textfield,c);
What I mean by a JLabel-JTextField pair is a JLabel component followed by a JTextField one, for example, "Parameter 1: -----" where "-----" denotes a blank JTextField.
The problem is, the width of JLabels varies due to the varying lengths of parameter names, so that the starts of JTextFields are not aligned vertically.
Is there any way to align the JLabels vertically to their right, so that the starts of JTextFields that follow would be aligned? Thanks.
Is there any way to align the JLabels vertically to their right, so that the starts of JTextFields that follow would be aligned?
1.6+, GroupLayout. E.G. from the JavaDocs:
Use the label alignment that pushes the text to the RHS.
See also this answer for an MCVE.
You didn't specify which layout do you use, so a good layout to implement that would be GridBagLayout. The demo in oracle site is great to start with.
And a short example:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
panel.add(new JLabel("Label 1:"), c);
c.gridx = 1;
c.gridy = 0;
panel.add(new JTextField("TextField 1"), c);
c.gridx = 0;
c.gridy = 1;
panel.add(new JLabel("Label 2:"), c);
c.gridx = 1;
c.gridy = 1;
panel.add(new JTextField("TextField 2"), c);
or
there is possible align just text inside JTextComponents with
JLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
This is a perfect use case for DesignGridLayout:
DesignGridLayout layout = new DesignGridLayout(contentPane);
layout.labelAlignment(LabelAlignment.RIGHT);
layout.row().grid(label1).add(field1);
layout.row().grid(label2).add(field2);
...
I would suggest the GridLayout layout manager. It presents the easiest solution to show pair-wise visualization of label and textbox controls. Thereby you simply define the number of rows and columns at time of instantiation and the added controls will be handled by the manager.
Good solutions for this that I've seen include use of the GridBagLayout (as noted above) or the MiGLayout, though since the latter isn't part of standard Java, it must be downloaded and placed on the classpath prior to use. MiGLayout is not as difficult to use.
The LayoutManager of the parent component has the responsability of positioning the elements contained. Maybe you need to set an XYLayout.
See the setLayoutManager() for your parent class.
So I have a JPanel that is split into 2 other separate JPanels (although this is mostly irrelevant). On the left side I have a GridBagLayout that I have organized to have a JLabel at the top, and a JTextArea below it, however the JTextArea isn't directly beneath the JLabel. Instead there is this empty space and I can't figure out why it's there or how to fix it. I'm fairly new to Java in general and GridBagLayout so I could be missing something, but I've tried several things to get it to work. I have the code for this below along with a visual representation of what it's doing. Any help is appreciated.
CODE
//LEFT SIDE
GridBagConstraints leftGBC = new GridBagConstraints();
JPanel leftSide = new JPanel(new GridBagLayout());
leftSide.setBorder(new EmptyBorder(inset, inset, inset, inset));
Font titleFont = bb5.comfortaa.deriveFont(Font.PLAIN, 16f);
leftGBC.gridx = 0;
leftGBC.gridy = 0;
leftGBC.weightx = 1;
leftGBC.weighty = 1;
leftGBC.anchor = GridBagConstraints.NORTH;
JLabel leftTitle = new JLabel("Objective");
leftTitle.setFont(titleFont);
leftTitle.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.BLACK));
leftGBC.fill = GridBagConstraints.HORIZONTAL;
leftSide.add(leftTitle, leftGBC);
leftGBC.gridy = 1;
leftGBC.anchor = GridBagConstraints.NORTH;
JTextArea objectiveArea = new JTextArea(objectiveText);
objectiveArea.setBackground(leftTitle.getBackground());
objectiveArea.setEditable(false);
objectiveArea.setFocusable(false);
objectiveArea.setFont(bb5.samsung1.deriveFont(16f));
objectiveArea.setLineWrap(true);
objectiveArea.setWrapStyleWord(true);
leftSide.add(objectiveArea, leftGBC);
VISUAL
leftGBC.weightx = 1;
leftGBC.weighty = 1;
Read the section from the Swing tutorial How to use GridBagLayout. The tutorial explains how the weightx/weighty constraints work. Those values indicate how to allocate "extra space" to each cell. So it would appear that both the label and text area getting extra space. I would guess you want 0 for the label.
If this doesn't help (and in the future when you ask a question), post a proper SSCCE that demonstrates the problem.
I want to create two non-editable textboxes (each will contain only one line) with a fixed size, but I want them to be scrollable (horizontally only) because I know the text they will contain will be very long. I want them to be below the two buttons I define below, and I want each textbox on their own row.
Problem is, everything shows up and buttons work as expected, but the textbox won't scroll, although I can somehow drag and select the rest of the text in the box that isn't visible. I don't know if labels are scrollable, would they be a better option?
Code:
public static void main(String[] args)
{
JFrame win = new JFrame("Window");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setSize(400, 300);
GridBagConstraints c = new GridBagConstraints();
win.setLayout( new GridBagLayout() );
JTextArea master = new JTextArea(1,1);
JTextArea vendor = new JTextArea(1,1);
master.setEditable(false);
vendor.setEditable(false);
master.setPreferredSize( new Dimension(100,20) );
vendor.setPreferredSize( new Dimension(100,20) );
master.setText(/*some really long string*/);
vendor.setText(/*some really long string*/);
JScrollPane mPane = new JScrollPane(master, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane vPane = new JScrollPane(vendor, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
mPane.getHorizontalScrollBar().isVisible();
vPane.getHorizontalScrollBar().isVisible();
JButton one = new JButton("Select");
ActionListener select = new SelectButton(master, vendor);
one.addActionListener(select);
JButton two = new JButton("Run");
c.gridx = 0;
c.gridy = 0;
win.add(one, c);
c.gridx = 1;
c.gridy = 0;
win.add(two, c);
c.gridx = 0;
c.gridy = 1;
win.add(master, c);
win.add(mPane, c);
c.gridx = 0;
c.gridy = 2;
win.add(vendor, c);
win.add(vPane, c);
win.setLocationRelativeTo(null);
win.setVisible(true);
return;
}
Don't, ever, use setPreferredSize! This is overriding the information that the JScrollPane needs in order to make decisions about how the components should be scrolled. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details. Instead use the JTextArea(int, int) constructor to provide hints to the JScrollPane, for example JTextArea master = new JTextArea(1, 20);. Any text longer then 20 characters will cause the JScrollPane to display the horizontal scroll bar...
Don't add both the JTextArea AND the JScrollPane to the container. Adding the JTextArea automatically removes it from the JScrollPane, which isn't what you want.
Use GridBagConstaints#gridwidth to control the number of columns a component might expand across to help fix your layout...
For example...
c.gridx = 0;
c.gridy = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
win.add(mPane, c);
c.gridx = 0;
c.gridy = 2;
win.add(vPane, c);
I'm hoping this is a really simple example, if not, you should always make sure that your UI is created and modified from within the context of the EDT. See Initial Threads for more details
What I mean by a JLabel-JTextField pair is a JLabel component followed by a JTextField one, for example, "Parameter 1: -----" where "-----" denotes a blank JTextField.
The problem is, the width of JLabels varies due to the varying lengths of parameter names, so that the starts of JTextFields are not aligned vertically.
Is there any way to align the JLabels vertically to their right, so that the starts of JTextFields that follow would be aligned? Thanks.
Is there any way to align the JLabels vertically to their right, so that the starts of JTextFields that follow would be aligned?
1.6+, GroupLayout. E.G. from the JavaDocs:
Use the label alignment that pushes the text to the RHS.
See also this answer for an MCVE.
You didn't specify which layout do you use, so a good layout to implement that would be GridBagLayout. The demo in oracle site is great to start with.
And a short example:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
panel.add(new JLabel("Label 1:"), c);
c.gridx = 1;
c.gridy = 0;
panel.add(new JTextField("TextField 1"), c);
c.gridx = 0;
c.gridy = 1;
panel.add(new JLabel("Label 2:"), c);
c.gridx = 1;
c.gridy = 1;
panel.add(new JTextField("TextField 2"), c);
or
there is possible align just text inside JTextComponents with
JLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
This is a perfect use case for DesignGridLayout:
DesignGridLayout layout = new DesignGridLayout(contentPane);
layout.labelAlignment(LabelAlignment.RIGHT);
layout.row().grid(label1).add(field1);
layout.row().grid(label2).add(field2);
...
I would suggest the GridLayout layout manager. It presents the easiest solution to show pair-wise visualization of label and textbox controls. Thereby you simply define the number of rows and columns at time of instantiation and the added controls will be handled by the manager.
Good solutions for this that I've seen include use of the GridBagLayout (as noted above) or the MiGLayout, though since the latter isn't part of standard Java, it must be downloaded and placed on the classpath prior to use. MiGLayout is not as difficult to use.
The LayoutManager of the parent component has the responsability of positioning the elements contained. Maybe you need to set an XYLayout.
See the setLayoutManager() for your parent class.