-
February 1st, 2012, 02:56 PM
#1
GridBagLayout problems
Hi there
I need a layout manager that allows me to resize each component and that gives me the ability to set desired positiong of each component withing the frame, therefore I chose to use the GridBagLayout.
However I am having probelms even after reading the tutorials and can't get my head around it.
I have a frame where I am putting a lbl and textlabel and I will be inserting a table and other components later on.
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.
here is the code
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MainInterface extends JFrame {
// Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize().;
// Screen height and width
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
JPanel UIPanel = new JPanel(new GridBagLayout());
JTextArea txtQuery = new JTextArea();
GridBagConstraints c = new GridBagConstraints();
public MainInterface() {
setInterface();
setUIPanel();
setTxtQuery();
this.setVisible(true);
}
void setUIPanel(){
UIPanel.setPreferredSize(new Dimension(this.getPreferredSize()));
UIPanel.setBackground(Color.gray);
UIPanel.setLayout(new GridBagLayout());
this.add(UIPanel);
}
void setInterface() {
setTitle("LSA Analysis");
setSize((screenWidth - 100), (screenHeight - 100));
UIPanel.setBackground(Color.white);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation((getCentre()), 0);
}
private int getCentre() {
int centre = screenWidth - this.getSize().width;
return centre = centre / 2;
}
public void setTxtQuery(){
//-------------------------------------
JLabel lblQry = new JLabel();
lblQry.setText("SQL Search Query :");
lblQry.setForeground(Color.white);
lblQry.setFont(new Font("Serif", Font.BOLD, 16));
c.weightx = 0.5; //request any extra vertical space
c.gridx=0;
c.gridy=0;
c.ipady = 10; //reset to default
c.ipadx=5;
//c.anchor = GridBagConstraints.PAGE_START; //bottom of space
c.insets = new Insets(0,0,0,0); //top padding
UIPanel.add(lblQry,c);
//----------------------------------------------
JScrollPane scrollPane = new JScrollPane(txtQuery);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
txtQuery.setFont(new Font("Serif", Font.BOLD, 24));
//txtQuery.setPreferredSize(new Dimension(400,16));
txtQuery.setWrapStyleWord(true);
c.weightx = 0.5;
c.gridx=0;
c.gridy=0;
c.ipady = 10; //reset to default
c.ipadx=600;
//request any extra vertical space
// c.anchor = GridBagConstraints.PAGE_START; //bottom of space
c.insets = new Insets(0,0,0,0); //top padding
UIPanel.add(scrollPane,c);
}
void setDemoTable(){
DataTable dt = new DataTable();
c.ipady = 10; //reset to default
c.ipadx=600;
c.gridx=1;
c.gridy=2;
//c.weighty=100;
//c.weightx=10;
c.anchor= GridBagConstraints.LINE_START;
this.UIPanel.add(dt,c);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//MainInterface maininterface = new MainInterface();
new MainInterface();
}
}
Another question, is it there any program that allows me to put all the components like it's possible in csharp and .net?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|