gbcbr
asked on
GridBagLayout
Please advice how to specify in GridBagLayout one line text boxes with different width.
At the moment I use simple GridLayout, but it split panel eventually and some small text use unnecessary big area.
How to define sizes for each box, so I'll have them in one line but with different sizes.
Thank you
At the moment I use simple GridLayout, but it split panel eventually and some small text use unnecessary big area.
How to define sizes for each box, so I'll have them in one line but with different sizes.
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use FlowLayout and specify the sixe for the text field in its constructor, for e.g.
new JTextFeild( 10 ) ;
new JTextFeild( 20 ) ;
new JTextFeild( 10 ) ;
new JTextFeild( 20 ) ;
ASKER
Can you please provide sample for this code:
jPanel1.setBounds(new Rectangle(10, 30, 1260, 30));
jPanel1.setSize(new Dimension(1260, 30));
jPanel1.setLayout(new GridLayout(1, 15));
jLabel1.setText("EUR/USD");
jLabel1.setForeground(new Color(0, 0, 165));
jLabel1.setFont(new Font("Lucida Grande", 1, 14));
jToggleButton1.setText("SELL");
jToggleButton1.setBackground(Color.red);
jToggleButton1.setOpaque(true);
jToggleButton1.setFont(new Font("Lucida Grande", 1, 14));
jToggleButton1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
jToggleButton1.setBounds(new Rectangle(87, 0, 57, 30));
jToggleButton1.setToolTipText("SELL");
jToggleButton1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
try {
jToggleButton1_mouseClicked(e);
} catch (Exception f) {
f.printStackTrace();
}
}
});
jToggleButton2.setText("BUY");
jToggleButton2.setBackground(Color.red);
jToggleButton2.setOpaque(true);
jToggleButton2.setFont(new Font("Lucida Grande", 1, 14));
jToggleButton2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
jToggleButton2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
try {
jToggleButton2_mouseClicked(e);
} catch (Exception f) {
f.printStackTrace();
}
}
});
jTextField1.setBounds(new Rectangle(265, 0, 80, 30));
jTextField1.setSize(new Dimension(80, 30));
jTextField1.setHorizontalAlignment(JTextField.CENTER);
jTextField1.setEditable(false);
jTextField2.setBounds(new Rectangle(355, 0, 80, 30));
jTextField2.setSize(new Dimension(80, 30));
jTextField2.setHorizontalAlignment(JTextField.CENTER);
jTextField2.setEditable(false);
jTextField3.setBounds(new Rectangle(450, 0, 80, 30));
jTextField3.setSize(new Dimension(80, 30));
jTextField3.setBackground(new Color(181, 255, 181));
jTextField3.setHorizontalAlignment(JTextField.CENTER);
jTextField3.setText("1.38624");
jTextField4.setBounds(new Rectangle(535, 0, 80, 30));
jTextField4.setSize(new Dimension(80, 30));
jTextField4.setHorizontalAlignment(JTextField.CENTER);
jTextField4.setEditable(false);
jTextField5.setBounds(new Rectangle(620, 0, 80, 30));
jTextField5.setSize(new Dimension(80, 30));
jTextField5.setHorizontalAlignment(JTextField.CENTER);
jTextField5.setEditable(false);
jTextField6.setBounds(new Rectangle(705, 0, 80, 30));
jTextField6.setSize(new Dimension(80, 30));
jTextField6.setBackground(new Color(255, 132, 132));
jTextField6.setHorizontalAlignment(JTextField.CENTER);
jTextField6.setText("1.35200");
jTextField7.setBounds(new Rectangle(790, 0, 80, 30));
jTextField7.setSize(new Dimension(80, 30));
jTextField7.setHorizontalAlignment(JTextField.CENTER);
jTextField7.setText("3");
jTextField8.setBounds(new Rectangle(875, 0, 80, 30));
jTextField8.setSize(new Dimension(80, 30));
jTextField8.setHorizontalAlignment(JTextField.CENTER);
jTextField8.setText("300");
I usually use my own mini-framework for this. Is this the sort of thing you mean? (java -jar form-maker.jar)
http://technojeeves.com/tech/form-maker.jar
http://technojeeves.com/tech/form-maker.jar
ASKER
@CEHJ Thanks you but this is different case.
I try all layouts, but now I'm sure that the best solution for me is this GridBagLayout.
I need advice because for me little bit complicated how to define exact size for each element.
I try all layouts, but now I'm sure that the best solution for me is this GridBagLayout.
I need advice because for me little bit complicated how to define exact size for each element.
Well what i have uses a GridBagLayout actually
if you don't worry abt resizing,
>>jPanel1.setLayout(new GridLayout(1, 15));
change it to
jPanel1.setLayout(null);
>>jPanel1.setLayout(new GridLayout(1, 15));
change it to
jPanel1.setLayout(null);
ASKER
I tried it, no good.
Too much work with low appearance.
Simple Grid Layout much better, but I want improve it and use place better.
Too much work with low appearance.
Simple Grid Layout much better, but I want improve it and use place better.
>>I tried it, no good.
you need to adjust the bounds and size... you'll have to do same type of work with gbl... but gbl offers flexi positioning when resize happens whcih you seem don't want!
you need to adjust the bounds and size... you'll have to do same type of work with gbl... but gbl offers flexi positioning when resize happens whcih you seem don't want!
ASKER
I have 10 similar panels 15 elements each, so I have recreate 150 elements manually, and if I need to make some changes I have to do it from zero.
This is the reason why I want to make template of GBL layout and apply it to all 10 panels in the frame. And when I need to make changes, I just change a layout.
This is the reason why I want to make template of GBL layout and apply it to all 10 panels in the frame. And when I need to make changes, I just change a layout.
A straight FlowLayout should achieve that (you just need to make sure parent panel is wide enough)
ASKER
I can't find in Class FlowLayout how to set dimentions of elements in FlowLayout, just setHgap, setAlingment.
you set the (preferred) size of the elements being added, and the FlowLayout will use them
ASKER
@objects
Can you provide please sample based on my code, so I can reproduce it.
Can you provide please sample based on my code, so I can reproduce it.
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JTextField(20));
panel.add(new JTextField(50));
panel.add(new JTextField(30));
panel.add(new JTextField(20));
panel.add(new JTextField(50));
panel.add(new JTextField(30));
ASKER
>>panel.add(new JTextField(20))
what is 20?
what is 20?
just specifies size of the field
ASKER
means horizontal size, what about vertical size?
you can use setPreferredSize()
field.setPreferredSize(new Dimension(width, height));
field.setPreferredSize(new
If you want to use a GridBagLayout, try something like the following:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class F extends JFrame {
private void setGui() {
Container cp = getContentPane();
cp.add(new FieldsPanel());
}
public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
F f = new F();
f.setGui();
f.setSize(200, 200);
f.setVisible(true);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private class FieldsPanel extends JPanel {
final int NUM_FIELDS = 15;
JTextField[] fields = new JTextField[NUM_FIELDS];
public FieldsPanel() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
for (int i = 0; i < NUM_FIELDS; i++) {
gbc.gridx = i;
fields[i] = new JTextField(10);
cp.add(fields[i], gbc);
}
}
}
}
ASKER
@CEHJ
But I don't see any specification for each field. It will be even again.
But I don't see any specification for each field. It will be even again.
Just tested FlowLayout, and it works fine
ASKER
@objects
nothing works. I can't set anything.
nothing works. I can't set anything.
private JTextField jTextField1 = new JTextField(80);
private JTextField jTextField2 = new JTextField(80);
private JTextField jTextField3 = new JTextField(60);
private JTextField jTextField4 = new JTextField(80);
private JTextField jTextField5 = new JTextField(80);
private JTextField jTextField6 = new JTextField(60);
............
private void jbInit() throws Exception {
........
this.getContentPane().add(jPanel1, null);
..............
jPanel1.add(jTextField1, null);
................
jPanel1.setBounds(new Rectangle(10, 30, 1260, 30));
jPanel1.setSize(new Dimension(1260, 30));
jPanel1.setLayout(new FlowLayout());
..................
jTextField1.setBounds(new Rectangle(265, 0, 80, 30));
jTextField1.setSize(new Dimension(80, 30));
jTextField1.setHorizontalAlignment(JTextField.CENTER);
jTextField1.setEditable(false);
DCC5.tiff
>>But I don't see any specification for each field. It will be even again.
Sorry - missed that you didn't want them even
Sorry - missed that you didn't want them even
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@CEHJ Too complicated for me, sorry.
@objects I'll try tomorrow and post the result. Looks easy, hope it will work for me
@objects I'll try tomorrow and post the result. Looks easy, hope it will work for me
yes, keep it simple :)
>>@CEHJ Too complicated for me, sorry.
Well - the question was about how to use a GridBagLayout -
>>Please advice how to specify in GridBagLayout one line text boxes with different width.
so i showed you
Well - the question was about how to use a GridBagLayout -
>>Please advice how to specify in GridBagLayout one line text boxes with different width.
so i showed you
ASKER
@CEHJ
With same result you can show map of Moon. I will never be there. You know my level, so when I see something overcomplicated for me, I just block my brain from overload.
I need light and easy understandable advice, I'm not from CERN.
Otherwise it has to be ready solution, like OHLC from for_yan, copy-paste and I don't care what is inside and how it works, I just add my parameters and see only perfect result.
With same result you can show map of Moon. I will never be there. You know my level, so when I see something overcomplicated for me, I just block my brain from overload.
I need light and easy understandable advice, I'm not from CERN.
Otherwise it has to be ready solution, like OHLC from for_yan, copy-paste and I don't care what is inside and how it works, I just add my parameters and see only perfect result.
ASKER
This FlowLayout quite understandable for me, but in view that it's already created huge class of 2500 lines, it's also not so easy to reorganize it step by step. This reason I asked about GBL.
But if it is not simple solution for this layout, I will need to make donkey job step by step.
But if it is not simple solution for this layout, I will need to make donkey job step by step.
>>it's already created huge class of 2500 lines
It might be worth attaching that sourc file
It might be worth attaching that sourc file
ASKER
what you mean?
you don't need to attach the source :)
ASKER
I don't attach anything, first of all I don't understand what to that to attach and secondary< work now like coal miner for this changes one by one, looks like I'll arrange it, but for sure not today.
ASKER
OK, let's say I go from my GridLayout not to GBL to FlowLayout. How I can set different gaps between elements? My main problem is to avoid self elements organizing, I want to do it manually, as a size as a gaps between them. How I can do it in FlowLayout?
public FlowLayout(int align, int hgap, int vgap) this will help u to specify the gaps, the size you can pass it to the control constructors as I mentioned earlier!
ASKER
but it will define all hgaps, but I need individual.
>>My main problem is to avoid self elements organizing, I want to do it manually,
So, I need, in some cases change standard hgap between only two selected elements, and another two, so I can design my view, don't look like army, everything the same.
>>My main problem is to avoid self elements organizing, I want to do it manually,
So, I need, in some cases change standard hgap between only two selected elements, and another two, so I can design my view, don't look like army, everything the same.
then you need to go with gbl!
>>I want to do it manually, as a size as a gaps between them. How I can do it in FlowLayout?
You can't. You'd need to be using something like a BoxLayout to do that?
You can't. You'd need to be using something like a BoxLayout to do that?
ASKER
So, we come back to my original question.
May somebody help me please?
May somebody help me please?
or play with multiple panel, for e.g.,
JPanel main = new JPanel( new FlowLayout( 10, 10 ) ) ;
JPanel sub1 = new JPanel( 20, 20 ) ;
JPanel sub2 = new JPanel( 10, 20 ) ;
main.add( sub1 ) ;
main.add( sub2 ) ;
whatever copnents added under sub1 will have a space of 20, 20...
JPanel main = new JPanel( new FlowLayout( 10, 10 ) ) ;
JPanel sub1 = new JPanel( 20, 20 ) ;
JPanel sub2 = new JPanel( 10, 20 ) ;
main.add( sub1 ) ;
main.add( sub2 ) ;
whatever copnents added under sub1 will have a space of 20, 20...
ASKER
This what I have now, I start to redesign it from first line, because I need to add more components in each row.
Each row is a JPanel.
Any advises? Just bear in mind that this is not final construction, maybe tomorrow I'll need to add 1, 2, 3 elements for each row, so I will not redraw everything, just add. DCC6.tiff
Each row is a JPanel.
Any advises? Just bear in mind that this is not final construction, maybe tomorrow I'll need to add 1, 2, 3 elements for each row, so I will not redraw everything, just add. DCC6.tiff
>>Each row is a JPanel
what I said is multiplt panel in each row!
what I said is multiplt panel in each row!
ASKER
Yes, each row is one JPanel, so I need organize elements in one row inside my panel, but I want as I said before - organize, don't follow self-organizing.
For example, I want to make gap between JLable EUR/USD and Toggle button SELL 15 or 20px,
how I can do this?
For example, I want to make gap between JLable EUR/USD and Toggle button SELL 15 or 20px,
how I can do this?
>>Yes, each row is one JPanel
I said, multiple panel in each row. you may want to write some test program!
I said, multiple panel in each row. you may want to write some test program!
ASKER
You advice to split my row-panel to sub-panels?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Looks nice, but how to adopt it to me case
whats the issue now?
ASKER
I see 10 field on each row, but I don't see any parameters which create them.
ASKER
I don't need just picture, I need practical solution.
> but it will define all hgaps, but I need individual.
you can control individual gaps with a border
though looking at the image the gaps appear all the same
you can control individual gaps with a border
though looking at the image the gaps appear all the same
ASKER
@objects
any sample?
any sample?
which part of the layout are stuk on exactly. looking at the image you posted all the textfields in the table are evenly spaced so you could use a GridLayout (or even a JTable)
ASKER
Yes, at the moment it's a GridLayout.
At first line I try to change it, so small text fields don't share the same area like big one and use for this a FlowLayout.
Generally it looks better and take less area, but I'm also want to group them by adding little more space between groups.
Let's say, first I want move JLabel at the beginning by adding 20 pixels out of first JToggleButton, and add few pixels between 3 and 4 JTextFields to better visualization etc.
This reason why I ask about a JPanel layout in which I can locate each item in exact position, like I can locate JPanel inside JFrame.
At first line I try to change it, so small text fields don't share the same area like big one and use for this a FlowLayout.
Generally it looks better and take less area, but I'm also want to group them by adding little more space between groups.
Let's say, first I want move JLabel at the beginning by adding 20 pixels out of first JToggleButton, and add few pixels between 3 and 4 JTextFields to better visualization etc.
This reason why I ask about a JPanel layout in which I can locate each item in exact position, like I can locate JPanel inside JFrame.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for everybody, I'll use all your advises to improve my picture.
ASKER
But now I need to place 15 boxes in one line with FIXED, permanent width.
Can you provide sample for this case please?