Results 1 to 5 of 5
Thread: GridBagLayout
- 05-18-2012, 10:58 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
GridBagLayout
Hello!
Having some odd issues using a GridBagLayout, where I have components in odd places, or on top of other components.
Any suggestions would be brilliant. :]
Java Code:public void addComponent(Container contentPane){ setContentPane(contentPane); GridBagConstraints c = new GridBagConstraints(); contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); //display = new JTextArea("k"); //display.setSize(FRAME_WIDTH, FRAME_HEIGHT / 2); //display.setVisible(true); //c.fill = GridBagConstraints.HORIZONTAL; //c.gridx = 0; //c.gridy = 0; //c.gridwidth = 3; loanAmountLabel = new JLabel("Loan Amount"); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(10, 10, 10, 10); c.gridx = 0; c.gridy = 1; c.gridwidth = 1; contentPane.add(loanAmountLabel, c); interestRateLabel = new JLabel("Interest Rate"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 1; c.gridwidth = 1; contentPane.add(interestRateLabel, c); loanPeriodLabel = new JLabel("Loan Period"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = GridBagConstraints.RELATIVE; c.gridy = 2; c.gridwidth = 1; contentPane.add(loanPeriodLabel, c); loanAmount = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 2; c.gridwidth = 1; contentPane.add(loanAmount, c); interestRate = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 2; c.gridwidth = 1; contentPane.add(interestRate, c); loanPeriod = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 2; c.gridy = 2; c.gridwidth = 1; contentPane.add(loanPeriod, c); }
- 05-19-2012, 12:07 AM #2
Re: GridBagLayout
Moved from New to Java.
Nowhere in the posted code is the layout set to a GridBagLayout. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-19-2012, 12:17 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
Re: GridBagLayout
Thanks for the tip. This one should be compilable.
Java Code:package mortgagecalc; import java.awt.ComponentOrientation; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class MortgageCalc extends JFrame implements ActionListener{ JTextField loanAmount, interestRate, loanPeriod; JLabel loanAmountLabel, interestRateLabel, loanPeriodLabel; public static void main( String[] args ) { MortgageCalc frame = new MortgageCalc(); frame.setVisible(true); } public MortgageCalc(){ setSize(FRAME_WIDTH, FRAME_HEIGHT); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); JPanel content = new JPanel(new GridBagLayout()); addComponent(content); } public void addComponent(Container contentPane){ setContentPane(contentPane); GridBagConstraints c = new GridBagConstraints(); contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); //display = new JTextArea("k"); //display.setSize(FRAME_WIDTH, FRAME_HEIGHT / 2); //display.setVisible(true); //c.fill = GridBagConstraints.HORIZONTAL; //c.gridx = 0; //c.gridy = 0; //c.gridwidth = 3; loanAmountLabel = new JLabel("Loan Amount"); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(10, 10, 10, 10); c.gridx = 0; c.gridy = 1; c.gridwidth = 1; contentPane.add(loanAmountLabel, c); interestRateLabel = new JLabel("Interest Rate"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 1; c.gridwidth = 1; contentPane.add(interestRateLabel, c); loanPeriodLabel = new JLabel("Loan Period"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = GridBagConstraints.RELATIVE; c.gridy = 2; c.gridwidth = 1; contentPane.add(loanPeriodLabel, c); loanAmount = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 2; c.gridwidth = 1; contentPane.add(loanAmount, c); interestRate = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 2; c.gridwidth = 1; contentPane.add(interestRate, c); loanPeriod = new JTextField(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 2; c.gridy = 2; c.gridwidth = 1; contentPane.add(loanPeriod, c); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
- 05-19-2012, 04:10 AM #4
Re: GridBagLayout
Nope.
dbJava Code:E:\Java\examples\src\swing\layout\MortgageCalc.java:17: cannot find symbol symbol : variable FRAME_WIDTH location: class swing.layout.MortgageCalc setSize(FRAME_WIDTH, FRAME_HEIGHT); E:\Java\examples\src\swing\layout\MortgageCalc.java:17: cannot find symbol symbol : variable FRAME_HEIGHT location: class swing.layout.MortgageCalc setSize(FRAME_WIDTH, FRAME_HEIGHT); 2 errorsWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-19-2012, 04:26 AM #5
Re: GridBagLayout
After fixing that, the code does pretty much what I would expect.
-- The JTextFields are constructed using the no-arg constructor, so the preferredSize.width is a few pixels.
-- Those text fields that are in the same column as a JLabel adopt the preferred width of the label
-- loanPeriodLabel is added with x=RELATIVE, which evaluates to 2 (relative to 1 for interestRateLabel) and y=2, so occupies the same cell as loanPeriod
A note: posting several commented lines only adds clutter, as does an empty implemention of an interfacethat has nothing to do with your question. You should take the trouble to remove them when posting on a forum. Also, there's no point repeatedly assigning the same value to the same field of your GridBagConstraints. Here's your code correctly formatted/indented and with all redundant lines removed; no other changes, except to change the fields to local variables (this is a layout problem; they aren't needed at the class level) and launch the GUI on the EDT (see Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)).
Notice how much shorter it is.dbJava Code:import java.awt.*; import javax.swing.*; public class MortgageCalc extends JFrame { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new MortgageCalc().setVisible(true); } }); } public MortgageCalc() { setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel content = new JPanel(new GridBagLayout()); addComponent(content); pack(); setLocationRelativeTo(null); } public void addComponent(Container contentPane) { setContentPane(contentPane); GridBagConstraints c = new GridBagConstraints(); contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); JLabel loanAmountLabel = new JLabel("Loan Amount"); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(10, 10, 10, 10); c.gridx = 0; c.gridy = 1; contentPane.add(loanAmountLabel, c); JLabel interestRateLabel = new JLabel("Interest Rate"); c.gridx = 1; contentPane.add(interestRateLabel, c); JLabel loanPeriodLabel = new JLabel("Loan Period"); c.gridx = GridBagConstraints.RELATIVE; c.gridy = 2; contentPane.add(loanPeriodLabel, c); JTextField loanAmount = new JTextField(); c.gridx = 0; contentPane.add(loanAmount, c); JTextField interestRate = new JTextField(); c.gridx = 1; contentPane.add(interestRate, c); JTextField loanPeriod = new JTextField(); c.gridx = 2; contentPane.add(loanPeriod, c); } }Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
GridBagLayout vs me: 1 - 0
By JosAH in forum AWT / SwingReplies: 9Last Post: 08-19-2011, 09:18 PM -
GridBagLayout Help
By Zman3359 in forum AWT / SwingReplies: 0Last Post: 02-21-2011, 08:39 PM -
[AWT] GridBagLayout Help.
By Sandia_man in forum AWT / SwingReplies: 2Last Post: 05-23-2010, 08:54 PM -
help with gridbaglayout
By robertbob in forum AWT / SwingReplies: 5Last Post: 05-18-2010, 04:14 AM -
GridBagLayout
By carderne in forum New To JavaReplies: 8Last Post: 01-25-2009, 02:06 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks