Re: GridBagLayout problems
Quote:
I need a layout manager that allows me to resize each component
When you use a layout manager you hand over positioning and sizing of the components to the layout manager - that's one of the reasons for using a layout manager ie to save you the hassle of calculating the positions and sizes without overlapping components etc especially when you first add the components and/or the panel size changes.
Each layout manager has it's own set of rules on how it lays out components and some, such as GridBagLayout, also allow you to specify additional layout constraints (rules). If you can't find a layout manager or combination of layout managers (you can have panels within panels each with their own layout manager) to do what you want you have 2 choices:
1. Write your own layout manager - not as difficult as it sounds as long as you have a clear idea of what you are trying to achieve.
2. Set the panels layout manager to null and use absolute positioning - but I really wouldn't recommend this approach, there is generally a way of doing it with layout managers.
Quote:
First of all I am trying to put the label (title) and the textbox close to each other at the top of the frame but they stay in the centre.
In that case use a BorderLayout, add a JPanel to the NORTH area with a FlowLayout and add your label and textbox to this panel.
Re: GridBagLayout problems
Yeah I have used no layout on one of my previous applications, it was a lot to do as size and positions were dynamic and calculated according to the size of the main frame etc.
I've heard about creating a layout,if is not hard I might do that.
Re: GridBagLayout problems
Quote:
Originally Posted by
cpu2007
First of all I am trying to put the label (title) and the textbox close to each other at the top of the frame but they stay in the centre. for some reason.
When I run it (after fixing the DataTable not found error) I only see the JTextArea. I don't even see the 'SQL Search Query :' label. That is because you put them both in the same GridBagLayout cell:
Code:
public void setTxtQuery(){
JLabel lblQry = new JLabel();
lblQry.setText("SQL Search Query :");
...
c.gridx=0;
c.gridy=0;
...
UIPanel.add(lblQry,c);
//----------------------------------------------
JScrollPane scrollPane = new JScrollPane(txtQuery);
...
c.gridx=0; // Bad. You are already using cell 0,0 for the lblQry. Change this to 1.
c.gridy=0;
...
UIPanel.add(scrollPane,c);
}
The way you figure these things out is to break the problem down into its smallest, simplest state. Remove anything that is not necesarry to demonstrate the problem. Create an SSCCE.
I commented out the UIPanel.add(scrollPane,c); line and then I saw the label. Then I un commented it & figured out why the JTextArea appered on top of the JLabel. First I got rid of about 80% of unnecesarry code that only added to the confusion.
http://sscce.org/
http://docs.oracle.com/javase/tutori...t/gridbag.html
Re: GridBagLayout problems
Oh and change 'UIPanel' to 'uiPanel' so that you are using correct java naming conventions. Especially important when you are asking other people to read your code.