Avatar of MarkLoveExEx
MarkLoveExEx
 asked on

Java GridBagLayout help

I'm having trouble with using the GridBagLayout in Java.  See attached hand-drawn outline of what I'm looking to do. Can't figure out all those contraints. This is what I have so far:

      static JTextArea JTextArea_4CHPS_ofsde_oper = new JTextArea("These will be ofsde files. Directory: .../serfc/workspace/DATAFLOW/data/FORCHPS_ofsde. STAND BY", 10, 30);
      Font font = new Font("MonoSpaced", Font.PLAIN, 10);
      JScrollPane JScrollPane_Source_ofsde_oper = new JScrollPane(JTextArea_4CHPS_ofsde_oper,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      
	static JTextArea JTextArea_4CHPS_Q2 = new JTextArea("These will be Q2 (grib1) files. Directory: .../serfc/workspace/DATAFLOW/data/FORCHPS_Q2. STAND BY", 10, 30);
	Font font2 = new Font("MonoSpaced", Font.PLAIN, 10);
	JScrollPane JScrollPane_Source_Q2 = new JScrollPane(JTextArea_4CHPS_Q2,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

Open in new window


		GridBagConstraints constraints;
		setLayout(gridbag);
		
		JTextArea_4CHPS_ofsde_oper.setFont(font);
		JTextArea_4CHPS_Q2.setFont(font2);
		
		
		JButton_RunQ2.addActionListener(this);
		JButton_RunQPF.addActionListener(this);
		JButton_RunQ2.setActionCommand("1");
		JButton_RunQPF.setActionCommand("2");
		
		addComponent(JScrollPane_Source_Q2,         0, 1, 1, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
		addComponent(JScrollPane_Source_ofsde_oper, 0, 2, 1, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
		//component, gridx, gridy gridwith, gridheight, weightx, weighty, 
		addComponent(JButton_RunQ2,                 0, 3, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER);

Open in new window


	private void addComponent(Component component, int gridx, int gridy,
			int gridwidth, int gridheight, int weightx, int weighty, int fill,
			int anchor) {
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = gridx;
		constraints.gridy = gridy;
		constraints.gridwidth = gridwidth;
		constraints.gridheight = gridheight;
		constraints.weightx = weightx;
		constraints.weighty = weighty;
		constraints.fill = fill;
		constraints.anchor = anchor;
		gridbag.setConstraints(component, constraints);
		add(component);
		
	}

Open in new window

IMG_0314.JPG
Java

Avatar of undefined
Last Comment
MarkLoveExEx

8/22/2022 - Mon
mccarl

You haven't said explicitly what you are getting and what you need to do differently. But going from your drawing, the only thing that I can tell is that you need spacing between all your components. You should be able to do that by adding something like this to your addComponent method...

constraints.insets = new Insets(5, 5, 5, 5);
MarkLoveExEx

ASKER
Oh so that's what the insets are for...  So far, I'm getting two huge overlapping TextAreas. Can't see the JButton at all. It's just a mess. Maybe I should go for a simpler layout manager.
mccarl

Try this...

		addComponent(JScrollPane_Source_Q2,         0, 1, 4, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
		addComponent(JScrollPane_Source_ofsde_oper, 4, 1, 4, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
		//component, gridx, gridy gridwith, gridheight, weightx, weighty, 
		addComponent(JButton_RunQ2,                 0, 3, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER);

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
MarkLoveExEx

ASKER
Its better. How did you come up with the constraint numbers? is there a method to the madness, or is it trial-and-error? I need to expand upon this to come up with something closer to my drawing.
crd_ark1.png
MarkLoveExEx

ASKER
	static JTextArea JTextArea_4CHPS_ofsde_serfc = new JTextArea("These will be ofsde_serfc files. Directory:" + "\n" + "go serfc /workspace/DATAFLOW/data/FORCHPS_ofsde. STAND BY", 18, 60);
	Font font = new Font("MonoSpaced", Font.BOLD, 12);
	JScrollPane JScrollPane_4CHPS_ofsde_serfc = new JScrollPane(JTextArea_4CHPS_ofsde_serfc,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	
	static JTextArea JTextArea_4CHPS_ofsde_sju = new JTextArea("These will be ofsde_sju files. Directory:" + "\n" + "go serfc /workspace/DATAFLOW/data/FORCHPS_ofsde. STAND BY", 18, 60);
	Font font4 = new Font("MonoSpaced", Font.BOLD, 12);
	JScrollPane JScrollPane_4CHPS_ofsde_sju = new JScrollPane(JTextArea_4CHPS_ofsde_serfc,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	
	static JTextArea JTextArea_4CHPS_Q2 = new JTextArea("These will be Q2 (grib1) files. Directory:" + "\n" + "go serfc /workspace/DATAFLOW/data/FORCHPS_q2. STAND BY", 18, 60);
	Font font2 = new Font("MonoSpaced", Font.BOLD, 12);
	JScrollPane JScrollPane_4CHPS_Q2 = new JScrollPane(JTextArea_4CHPS_Q2,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	
	static JTextArea JTextArea_4CHPS_WPCQPF = new JTextArea("These will be WPCQPF (grib1) files. Directory:" + "\n" + "go serfc /workspace/DATAFLOW/data/FORCHPS_wpcqpf. STAND BY", 18, 60);
	Font font3 = new Font("MonoSpaced", Font.BOLD, 12);
	JScrollPane JScrollPane_4CHPS_wpcqpf = new JScrollPane(JTextArea_4CHPS_WPCQPF,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

Open in new window


		addComponent(JScrollPane_4CHPS_Q2,          0, 1, 4, 2, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NORTHWEST);
		addComponent(JScrollPane_4CHPS_wpcqpf,      4, 1, 4, 2, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NORTHWEST);
		addComponent(JScrollPane_4CHPS_ofsde_serfc, 0, 3, 4, 2, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NORTHWEST);
		addComponent(JScrollPane_4CHPS_ofsde_sju,   4, 3, 4, 2, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NORTHWEST);

Open in new window


QUESTIONS:  Why is my ofsde_serfc way over to the right? I have gridx value set at 0?
And where is my ofsde_sju textarea? Very frustrating...
crd_ark2.png
MarkLoveExEx

ASKER
I've wasted too much time trying to figure this out. I'm giving up.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
MarkLoveExEx

ASKER
I've wasted too much time trying to figure this out. I'm giving up.

That's a shame.

Yeah, I know. :(  Would have like to get it working, but I was on a tight deadline, and had to move on. I had better luck with using a combination of the other LayoutManagers, although using them I wasn't able to get "exactly" what I wanted. But it will do.

BTW, I have a "sudo" question out there that is still unsolved...I would appreciate your input... :)
MarkLoveExEx

ASKER
I appreciate the input...