Results 1 to 8 of 8
Thread: component wont display
- 05-04-2012, 03:49 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
component wont display
i wrote a class that uses a gridbaglayout to display a drawing panel, and next to it a chat panel. the problem is, only the chatpanel shows up and i have no idea why.
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mytutor; import java.awt.*; import javax.swing.*; public class CanvasPanel extends JComponent { private Image image; private Graphics2D graphics2D; public CanvasPanel(int width, int height) { super(); //this.setSize(width, height); revalidate(); //this.setVisible(true); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); if(image == null){ image = createImage(getSize().width, getSize().height); graphics2D = (Graphics2D)image.getGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); clear(); } g.drawImage(image, 0, 0, null); } public void clear(){ graphics2D.setPaint(Color.white); graphics2D.fillRect(0, 0, getSize().width, getSize().height); graphics2D.setPaint(Color.black); repaint(); } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mytutor; import java.awt.*; import javax.swing.*; import java.util.*; public class MainPanel extends JPanel implements TutorComponent { private CanvasPanel drawingpanel; private ChatPanel chatpanel; private ArrayList<TutorComponent> children; public MainPanel(int width, int height){ super(); setSize(width, height); drawingpanel = new CanvasPanel((int)(width*.8), height); chatpanel = new ChatPanel((int)(width*.2), height); this.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.weightx = .8; c.weighty = 0; this.add(drawingpanel, c); c.gridy = 1; c.weightx = .2; this.add(chatpanel, c); } public void resizeChildren(int width, int height) { } }
-
Re: component wont display
You will want to set the JComponent's preferredSizes in their constructors and then call pack on the JFrame after creating it.
- 05-04-2012, 04:06 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
Re: component wont display
that did it. thanks!
however, i have a new problem now, the components dont resize. I chose GridBagLayout because in the guides it said that its supposed to resize components. I thought maybe the paint method wasnt being called again, but i implemented componentListener and tried calling clear on resize and it still wont grow. The other weird thing is when I shrink it, the chatpanel becomes shorter and fatter and the drawing panel dissappears, but when i expand it, empty space shows up between the two panels. I also added anchors to the left and right for each panel since I last posted.Last edited by yemista; 05-04-2012 at 04:20 AM.
-
Re: component wont display
You're quite welcome!
Have you gone through the GridBagLayout tutorials fully? Are you using the GridBagConstraints correctly? For instance, I don't see you setting the fill field at all? What if you set it withhowever, i have a new problem now, the components dont resize. I chose GridBagLayout because in the guides it said that its supposed to resize components. I thought maybe the paint method wasnt being called again, but i implemented componentListener and tried calling clear on resize and it still wont grow. The other weird thing is when I shrink it, the chatpanel becomes shorter and fatter and the drawing panel dissappears, but when i expand it, empty space shows up between the two panels. I also added anchors to the left and right for each panel since I last posted.
For my money, though, I avoid GridBagLayout when I can and instead nest JPanels that use simpler layouts for good effect and easier code.Java Code:c.fill = GridBagConstraints.BOTH;
- 05-04-2012, 05:06 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
Re: component wont display
I tried that, and what is happening now is the chat panel does resize, but the canvas panel doesnt. However, I think it actually does but just wont repaint itself, because that would explain all the empty space around it. I made CanvasPanel a componentlistener, and put the following for the resize method:
but this doesnt work either, i tried it without the call to getParent as well. Another problem I am having is that when I shrink the frame, the chatpanel suddenly gets wider, and its painted over the canvas panel, but im betting it has to do with the canvas panel not resizing correctly as well. Why do you suggest using other layout managers? That was my original design, but things like flow layout dont allow things like weight, so the only option is to implement a function called resizeChildren in every component i use and recursively call it when I resize. I was going to do it that way, but a friend of mine told me gridbaglayout resizes for you and I thought this would result in much less, cleaner code.Java Code:public void componentResized(ComponentEvent e) { setPreferredSize(new Dimension(getParent().getWidth(),getParent().getHeight())); clear(); validate(); }
-
Re: component wont display
Don't worry about more code, worry about functioning and maintainable code. And there's absolutely no need for a "resizeChildren" method. Please read all the layout manager tutorials, as this is easily and simply solved with just a little effort on your part.
- 05-04-2012, 09:06 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
Re: component wont display
ok, i will study them some more. thank you for your help
- 05-06-2012, 08:34 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 41
- Rep Power
- 0
Re: component wont display
ok, i figured it out, just in case someone needs this answer in the future. The trick is to implement componentListener in the main panel and child panels, , and in the componentResize method of the main panel, call the componentResize method of the child panels. For someone reason, when you resize the frame, it resizes the main panel, but doesnt resize its children. I thought the layout manager was supposed to handle that, but apparently after some debugging it appears that it doesnt. You have to manually call the resize method of the children, which has to set the preferred size of the child and then validate the child, which will then let the layout manager do its thing.
Similar Threads
-
image correctly loaded, but JPanel wont display
By apchar in forum AWT / SwingReplies: 3Last Post: 04-13-2011, 03:15 AM -
Why wont my JSP page display my selected item?
By system32 in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-01-2011, 04:02 PM -
wont display selected array
By newbie80 in forum New To JavaReplies: 7Last Post: 03-21-2011, 01:10 AM -
uploaded image wont display if i change filename
By schenker in forum Java ServletReplies: 12Last Post: 06-11-2010, 11:13 AM -
[SOLVED] Last line in JTextArea wont display
By Chris.Brown.SPE in forum Advanced JavaReplies: 5Last Post: 04-11-2008, 01:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks