How do I keep JTextFields in a Java Swing BoxLayout from expanding? - java

I have a JPanel that looks something like this:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
...
panel.add(jTextField1);
panel.add(Box.createVerticalStrut(10));
panel.add(jButton1);
panel.add(Box.createVerticalStrut(30));
panel.add(jTextField2);
panel.add(Box.createVerticalStrut(10));
panel.add(jButton2);
... //etc.
My problem is that the JTextFields become huge vertically. I want them to only be high enough for a single line, since that is all that the user can type in them. The buttons are fine (they don't expand vertically).
Is there any way to keep the JTextFields from expanding? I'm pretty new to Swing, so let me know if I'm doing everything horribly wrong.

textField = new JTextField( ... );
textField.setMaximumSize( textField.getPreferredSize() );

If you want the width to keep changing, just keep it set to MAX INT. So...
textField.setMaximumSize(
new Dimension(Integer.MAX_VALUE, textField.getPreferredSize().height) );

In my case I need a combination of all the answers for it to work properly. If I don't use glue, it is not centered vertically; if I don't restrict maximum size, it extends vertically; if I restrict both width and height, it is too small, being only wide enough to contain the initialization text.
textField = new JTextField("Hello, world!");
textField.setMaximumSize(
new Dimension(Integer.MAX_VALUE,
textField.getPreferredSize().height));
Box box = Box.createVerticalBox();
box.add(Box.createVerticalGlue());
box.add(textField);
box.add(Box.createVerticalGlue());

set the max height.
or put them in a scroll region

JPanel panel = new JPanel();
Box box = Box.createVerticalBox();
JTextField tf = new JTextField(8);
box.add(tf);
panel.add(box);
frame.getContentPane().add(panel, BorderLayout.CENTER);

Related

How to get JPanels to vertically align?

This is my first time really using graphics with Java.
I have a problem where I'm trying to populate a JScrollPane with an undefined number of panels (The following code is just a test; I know it's written using bad practices, but it's a test to see how to do it is all).
In the final program I'm going to have an ArrayList of orders (ArrayList size not predetermined), and in a JScrollPane I'm going to have a JPanel that holds a JPanel (each containing several labels) that will have details of each order.
here's the current test code (the JScrollPane is simply named scrollPane):
JPanel panel = new JPanel();
GroupLayout experimentLayout = new GroupLayout(panel);
ArrayList<JPanel> panelArray = new ArrayList();
for(int i = 0; i <3; i++){
JPanel panel2 = new JPanel();
JLabel label2 = new JLabel("Hello");
JLabel label3 = new JLabel("Hi");
panel2.add(label2);
panel2.add(label3);
//panel.add(panel2);
panelArray.add(panel2);
}
experimentLayout.setHorizontalGroup(
experimentLayout.createParallelGroup()
.addComponent(panelArray.get(0))
.addComponent(panelArray.get(1))
.addComponent(panelArray.get(2))
);
experimentLayout.setVerticalGroup(
experimentLayout.createSequentialGroup()
//.addGroup(experimentLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(panelArray.get(0))
.addComponent(panelArray.get(1))
.addComponent(panelArray.get(2))
);
scrollPane.setViewportView(panel);
When this runs, the JPanels that contain the labels are displayed horizontally (next to each other) instead of under one another.
Any help on how I could go about displaying them vertically would be a great help - Thank you all :)
You might find GridLayout to be useful.

JTextArea that needs to grow with parent panel

I want to make my JTextArea field as big as it can be in current JPanel. How to do that?
Now it is like this:
JPanel statusBar = new StatusBar(project);
JTextArea outputBox = new JTextArea(1, 50);
outputBox.setEditable(true);
statusBar.add(outputBox);
The default layout manager of JPanel is FlowLayout, which wouldn't let the text area fill the entire available space in the panel.
Using BorderLayout should work well:
statusBar.setLayout( new BorderLayout() );
JTextArea outputBox = new JTextArea(1, 50);
outputBox.setEditable(true);
statusBar.add(outputBox, BorderLayout.CENTER);
You need a layout manager on the JPanel. If its just the JTextArea contained within it and you need to maximise it you can use a simple GridLayout:
statusBar.setLayout(new GridLayout(1,1));

How do I right align text within a JLabel?

I have the following code:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int xx =0; xx < 3; xx++)
{
JLabel label = new JLabel("String");
label.setPreferredSize(new Dimension(300,15));
label.setHorizontalAlignment(JLabel.RIGHT);
panel.add(label);
}
This is how I would like the text to look:
[ String]
[ String]
[ String]
this is how it looks
[String]
[String]
[String]
For some reason label doesn't get set to the preferred size I specified and I think because of this it doesn't right align my label text. But im not sure. Any help would be appreciated.
JLabel label = new JLabel("String", SwingConstants.RIGHT);
:)
The setPreferredSize/MinimumSize/MaximumSize methods is dependent from the layout manager of the parent component (in this case panel).
First try with setMaximumSize instead of setPreferredSize, if I'm not going wrong should work with BoxLayout.
In addition: probably you have to use and play around with glues:
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(label);
panel.add(Box.createHorizontalGlue());
If you need the Y_AXIS BoxLayout you could also used nested panel:
verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(label);
panel.add(Box.createHorizontalGlue());
verticalPanel.add(panel);
I think it depend to layout that you are using, in XY (I remember was some kind of layouts in JBuilder) it should work, but in other can be problem. Try to change minimum size to prefered size.
This is a bit annoying, but you could use nested JPanels with box layouts if you wanted more flexibility in your alignment than with grid layout.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (int xx = 0; xx < 3; xx++) {
JPanel temp = new JPanel();
temp.setLayout(new BoxLayout(temp,BoxLayout.LINE_AXIS));
JLabel label = new JLabel("String");
temp.add(Box.createHorizontalGlue());
temp.add(label);
panel.add(temp);
}
I used horizontal glue to keep it at the right no matter the size, but you can put in rigid areas to make it a specific distance.
You need to ensure that your LayoutManager is sizing the label to fill the target area. You probably have a JLabel component which is exactly sized to the length of the text and which has been left aligned in the layout.
myLabel#setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
rather than use
label.setHorizontalAlignment(JLabel.RIGHT);
use
label.setHorizontalAlignment(SwingConstants.RIGHT);
thus you have:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int xx =0; xx < 3; xx++)
{
JLabel label = new JLabel("String");
label.setPreferredSize(new Dimension(300,15));
label.setHorizontalAlignment(SwingConstants.RIGHT);
panel.add(label);
}
Couldn't you use the following?
Jlabel label = new JLabel("String");
label.setBounds(x, y, width, height); // <-- Note the different method used.
label.setHorizontalAlignment(JLabel.RIGHT);
This works within a JFrame Container at least. Not sure about a JPanel.
With the responses from you guys I was able to determine that BoxLayout doesn't support the text alignment that I wanted, so I changed it to
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1,0,0);
and everything worked fine.

Button inside text box

My code shows a button inside a textbox, but when the input value changes, the size of the text box also changes. That I don't like. Is there any solution such that the textbox size remains fixed? Or any other idea on how to create a button inside textbox?
The following is my code:
JPanel panel = new JPanel();
panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) );
panel.add(textField);
panel.add(button);
panel.setBackground( textField.getBackground() );
panel.setBorder( textField.getBorder() );
textField.setBorder(null);
Try a BorderLayout and add the textfield at BorderLayout.CENTER and the button at BorderLayout.EAST.
I tried your code, and it seemed to work fine for me, with a slight modification. It may be that panel is resizing itself, not textField. Try wrapping panel in another JPanel to force its shape.
JPanel panel = new JPanel();
JPanel outerPanel = new JPanel();
panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) );
panel.add(textField);
panel.add(button);
panel.setBackground( textField.getBackground() );
panel.setBorder( textField.getBorder() );
textField.setBorder(null);
outerPanel.add(panel);
Your example appears to be the second alternative discussed in the article Component Border. Instead, you may want to install() a ComponentBorder, a third alterative described later in the same article.
Is there any solution such that the textbox size remains fixed?
I'm guessing you are creating the JTextField like:
JTextField textField = new JTextField();
In this case the text field size changes as you add text to it.
Instead you should use:
JTextField textField = new JTextField(10);
and the text field will remain a constant size an hold a minimum of 10 characters before scrolling.
Use a different Layout, or try absolute positioning

How can I set a JPanel maximum or preferred size that uses gridLayout?

I had problem using a very simple frame containing two JPanel.
The problem is on the layout of the Center JPanel that contains four JButton.
How can I set a better size for buttons or directly for JPanel that uses the GridLayout. On the picture the problem:
alt http://img42.imageshack.us/img42/4601/horrible.jpg
!
Here the code: ` JFrame window = new JFrame("Horrible! LOL");
JTextField textField = new JTextField("");
textField.setPreferredSize(new Dimension(200,20));
JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
textPanel.add(textField);
window.add(textPanel, BorderLayout.NORTH);
JButton plus = new JButton("+");
//plus.setPreferredSize(new Dimension(50,50)); nothing would change
JButton minus = new JButton("-");
JButton per = new JButton("x");
JButton divide = new JButton("/");
JPanel prova = new JPanel(new GridLayout(2,2,10,10));
Dimension d = new Dimension(20,20);
prova.setMaximumSize(d); // nothing changed!
prova.add(plus);
prova.add(minus);
prova.add(per);
prova.add(divide);
window.add(prova, BorderLayout.CENTER);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(250,300);
window.setResizable(false);
window.setVisible(true);`
Which is a good solution?
Kind regards
Unfortunately gridlayout doesent respect preferred sizes. But still if you want to stick to grid layout then you can try something like this:
public static JComponent wrap(JComponent comp)
{
JPanel panel = new JPanel();
panel.add(comp);
return panel;
}
And then instead of direclty adding in to prova add like this:
prova.add(wrap(plus));
prova.add(wrap(minus));
prova.add(wrap(per));
prova.add(wrap(divide));
Tested, Works perfect!!
There are other better ways though
That's what happen to me:
It's definitely attached to the upper edge of the grid.
alt text http://img96.imageshack.us/img96/9431/stillnot.jpg
Even if in this case, in the wrap method I can set the preferredSize of buttons/comp, every buttons is on its own edge. What about others solutions. How would you position buttons for a calculator?
Kind regards and thanx angain!

Categories

Resources