Results 1 to 6 of 6
Thread: GridBagConstraints help
- 07-01-2010, 08:30 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
GridBagConstraints help
Hi, I looked this problem up in the search bar but couldn't find anything that matches my exact problem nor anything that could help me so sorry if some has posted this before, I looked...
Anyways I have a problem with grid bag constraints when using a program that extends JPanel, I was thinking I probably shouldn't post all the code because I doubt any of you will be familiar with most of since its from a package called edu.mines.jtk.mosaic but on second thought some of you may be able to understand some of it because PlotPanel is a subclass of IPanel which is a subclass of JPanel, besides my problem is with GridBagConstraints anyways
Anyways, when I run this program the line in yellow has a problem...
ignore all the code in navy blue
(Didn't post imports for room's shake)
So anyways when I run it it says:Java Code:[COLOR="Navy"]private class MyPointsViewPlotPanel extends PlotPanel { private MyPointsViewPlotPanel() { float[] x = new float[100]; float[] y = new float[100]; for (int i = 0; i < 100; i++) { x[i] = i + 10 * rand.nextFloat(); y[i] = i + 25 * rand.nextFloat(); } float[][] jil = new float[2][100]; for (int i = 0; i < 100; i ++) { jil[0][i] = x[i]; jil[1][i] = y[i]; } PointsView pv = addPoints(y); pv.setLineStyle(PointsView.Line.NONE); pv.setMarkStyle(PointsView.Mark.FILLED_CIRCLE); pv.setMarkColor(colorScale0to100(75.25f)); Color[] clr = new Color[256]; float fl = 0.0f; float clrmx = 100.0f / 256.0f; for (int o = 0; o < 256; o++) { clr[o] = colorScale0to100(fl); fl = fl + clrmx; }[/COLOR] ColorBar cb = new ColorBar("Color Bar"); ColorMap cm = new ColorMap(0.0, 100.0, clr); JToolBar jtb = new JToolBar("Tool Bar"); cb.colorMapChanged(cm); [COLOR="Yellow"]add(cb, GridBagConstraints.EAST);[/COLOR] validate(); } }
obviously this is because of the gridBagConstraints because when I change it to GridBagConstraints.NONE it works. How do I get the colorbar to the Right side of the PlotPanel?Java Code:Exception in thread "main" java.lang.IllegalArgumentException: illegal component position at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at opfviewer.opfv$MyPointsViewPlotPanel.<init>(opfv.java:89) at opfviewer.opfv$MyPointsViewPlotPanel.<init>(opfv.java:47) at opfviewer.opfv.<init>(opfv.java:40) at opfviewer.opf_Main_Viewer.main(opf_Main_Viewer.java:9)
- 07-01-2010, 08:49 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You need to read the section from the Swing tutorial on Using Layout Managers especially the part on Using a GridBagLayout. That is not how you use a GridBagConstraint.
You get an exception because you are trying to add a component in a place that is not valid. For example if you have 1 component on the panel then you can use:
a) panel.add(component, 0) to add the component at the start of the panel
b) panel.add(component, 1) to add the component after the first component on the panel.
This of course assumes you are using a FlowLayout or GridLayout or BoxLayout. When using a GridBagLayout you need to add a GridBagConstraint Ojbect, not a variable.
- 07-02-2010, 01:20 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
Alright so I'll look it up in there
So like this?:
Java Code:GridBagConstraints gbc = new GridBagConstraints(); add(cb, gbc.EAST); validate();
-
Last edited by Fubarable; 07-02-2010 at 02:09 AM.
- 07-02-2010, 11:48 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
Sorry :( .
-
No need to apologize. Rather spend the energy applying yourself. Best of luck.
Similar Threads
-
[SOLVED] [newbie] java.awt.GridBagConstraints
By jon80 in forum New To JavaReplies: 0Last Post: 05-27-2009, 03:40 PM -
GridBagConstraints
By Aswq in forum New To JavaReplies: 2Last Post: 07-21-2008, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks