Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2006

Tip: Looking for answers? Try searching our database.

Problem with GridLayout

Thread view: 
erenay - 23 May 2006 09:21 GMT
Hi everybody, I'm having a problem with the layout of my application.
When I run the following code, I only see one graph in the frame. I'm
sopposed to see "graphNum" graphs in a row.
This part of code runs when I press the "draw graph" button:

  public void run() {
      Graph[] graphs = new Graph[graphNum];
      JFrame frame = new JFrame();
      for(int i=0; i<graphNum; i++){
        graphs[i] = new Graph();
        SliceVal[] sliceValA = new SliceVal[sliceNum];
        for(int j=0;j<sliceNum;j++){
          sliceValA[j] =  new SliceVal(new BigDecimal(10.0),
colors[j]);
        }
        graphs[i].setSliceVal(sliceValA);
      }
      JPanel grafPanel = new JPanel();
      graphPanel.setLayout(new GridLayout(1,graphNum));
      for (int i=0; i<graphNum; i++) {
        grafPanel.add(graphs[i]);
      }
      frame.getContentPane().add(graphPanel);
      frame.setSize(300, 200);
      frame.setVisible(true);
   }
 }

SliceVal class holds the value and color of a slice of the pie chart.
Graph class extends JComponent and has a paint() function in it.
I set the values of slices to 10.0 for simplifying the code

Do you see an error in the code above?
I would appreciate any help, thank you.
Piotr Hrycenko - 23 May 2006 09:35 GMT
erenay napisał(a):
> Hi everybody, I'm having a problem with the layout of my application.
> When I run the following code, I only see one graph in the frame. I'm
[quoted text clipped - 30 lines]
> Do you see an error in the code above?
> I would appreciate any help, thank you.

Maybe your graph components have setted preff or minimum sizes.

Regards.
Vova Reznik - 23 May 2006 14:36 GMT
> Hi everybody, I'm having a problem with the layout of my application.
> When I run the following code, I only see one graph in the frame. I'm
[quoted text clipped - 20 lines]
>        frame.getContentPane().add(graphPanel);
>        frame.setSize(300, 200);

Your GridLayout set to show one row with graphNum columns,
but you limited width of the graphPanel by setting size for the
JFrame.
Try to replace
frame.setSize(300, 200);
with
frame.pack();

Also check preferred size of graph. Looks like it has width
equals or bigger than 300.

http://java.sun.com/docs/books/tutorial/uiswing/layout/grid.html

>        frame.setVisible(true);
>     }
[quoted text clipped - 6 lines]
> Do you see an error in the code above?
> I would appreciate any help, thank you.
erenay - 23 May 2006 17:59 GMT
Thanks for the answers.

I used frame.pack(); but it didn't help, the same problem goes on.
This is how the frame looks like:
http://img50.imageshack.us/img50/5034/graph1xz.jpg
graphNum was 4 and sliceNum was 6. When I change the size of the frame,
the graph adjusts itself to an appropriate size.

This is how my Graph class is like:
---------------------------------------------------------------------------------
public class Graph extends JComponent{

 public SliceVal[] slices;

 public Graph(){
  slices = new SliceVal[AProject.sliceNum];
 }

 public SliceVal[] getSliceVal(){
   return this.slices;
 }

 public void setSliceVal(SliceVal[] slices){
   this.slices = slices;
 }

 public void drawGraph(Graphics2D g, Rectangle area, SliceVal[]
slices) {
       ...
       g.setColor(...);
       g.fillArc(...);
       }
     }

 public void paint(Graphics g) {
       drawGraph((Graphics2D)g, getBounds(), slices);
 }
}
------------------------------------------------------------

Maybe the problem is with getBounds(), returning the coordinate of the
rectangle as (0,0)
I don't know. What should I do?
Vova Reznik - 23 May 2006 19:25 GMT
> Thanks for the answers.
>
[quoted text clipped - 39 lines]
> rectangle as (0,0)
> I don't know. What should I do?

Check this.
- Graph needs to have preferred size.
- Don't override paint, but do paintComponent
(nothing wrong with your example)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestSlices {

    private static Color    colors[]    = { Color.GREEN, Color.RED, Color.CYAN,
            Color.ORANGE, Color.MAGENTA, Color.BLUE };

    public static void main(String[] args) {
        JPanel pnl = new JPanel(new GridLayout(1, colors.length));
        for (int i = 0; i < 7; i++) {
            pnl.add(new Graph());
        }

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(pnl);
        f.pack();
        f.setVisible(true);

    }

    static class Graph extends JComponent {

        Graph() {
            setPreferredSize(new Dimension(100, 100));
        }

        protected void paintComponent(Graphics g) {
            // public void paint(Graphics g) {
            int x = 0;
            int y = 0;
            int w = getWidth() / 2;
            int h = getHeight() / 2;
            int shift = 60;
            for (int i = 0, angle = 0; i < colors.length; i++, angle += shift) {
                g.setColor(colors[i]);
                g.fillArc(x, y, w, h, angle, shift);
            }
        }
    }
}
erenay - 24 May 2006 17:08 GMT
Sorry Vova. I've been trying to fix it all day but I failed.
As it can be seen from
http://img459.imageshack.us/img459/5517/graf5gf.jpg
the components are there but they can't be seen.
I set the preferred size and used paintComponent.
I tried things like:
    for (int i=0; i<graphNum; i++) {
        graphPanel.add(graphs[i]);
        graphs[i].repaint();
      }
      graphPanel.validate();

I wonder if it has something to do with the Graphics object passed to
paintComponent.
I will inform you if I can find a solution.
Thanks anyway.
erenay - 30 May 2006 07:19 GMT
The problem is fixed.

Since each component has its own container, all of them need to be
drawn starting at (0,0) however  getBounds() returns the coordinates of
components, not the containers.
So getBounds() needs to be replaced with new Rectangle(0,0,100,100).

public void paintComponent(Graphics g) {
         super.paintComponent(g);
         drawGraph((Graphics2D)g, new Rectangle(0,0,100,100), slices);
 }  

Thanks to Michael Dunn for help.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.