Results 1 to 6 of 6
  1. #1
    noobgrammer is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default 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)
    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();
    		}
    	}
    So anyways when I run it it says:

    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)
    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?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    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.

  3. #3
    noobgrammer is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Alright so I'll look it up in there

    So like this?:
    Java Code:
    GridBagConstraints gbc = new GridBagConstraints();
    			
    add(cb, gbc.EAST);
    validate();

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by noobgrammer View Post
    Alright so I'll look it up in there

    So like this?:
    Java Code:
    GridBagConstraints gbc = new GridBagConstraints();
    			
    add(cb, gbc.EAST);
    validate();
    Sorry, but it looks like you're just guessing at this, and this never works with programming. If you haven't yet read the tutorial please do so as it it explains all. If something in the tutorial confuses you, then please ask.
    Last edited by Fubarable; 07-02-2010 at 02:09 AM.

  5. #5
    noobgrammer is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Sorry :( .

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. [SOLVED] [newbie] java.awt.GridBagConstraints
    By jon80 in forum New To Java
    Replies: 0
    Last Post: 05-27-2009, 03:40 PM
  2. GridBagConstraints
    By Aswq in forum New To Java
    Replies: 2
    Last Post: 07-21-2008, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •