AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with GridLayout

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
erenay
Guest





PostPosted: Tue May 23, 2006 9:07 am    Post subject: Problem with GridLayout Reply with quote



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.
Back to top
Piotr Hrycenko
Guest





PostPosted: Tue May 23, 2006 9:07 am    Post subject: Re: Problem with GridLayout Reply with quote



erenay napisał(a):
Quote:
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.

Maybe your graph components have setted preff or minimum sizes.


Regards.
Back to top
Vova Reznik
Guest





PostPosted: Tue May 23, 2006 2:07 pm    Post subject: Re: Problem with GridLayout Reply with quote



erenay wrote:
Quote:
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);

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

Quote:
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.
Back to top
erenay
Guest





PostPosted: Tue May 23, 2006 5:07 pm    Post subject: Re: Problem with GridLayout Reply with quote

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?
Back to top
Vova Reznik
Guest





PostPosted: Tue May 23, 2006 7:07 pm    Post subject: Re: Problem with GridLayout Reply with quote

erenay wrote:
Quote:
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?




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);
}
}
}
}
Back to top
erenay
Guest





PostPosted: Wed May 24, 2006 5:07 pm    Post subject: Re: Problem with GridLayout Reply with quote

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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.