Results 1 to 8 of 8
  1. #1
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default 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) {
            
        }
    }

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

    Default 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.

  3. #3
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default 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.

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

    Default Re: component wont display

    Quote Originally Posted by yemista View Post
    that did it. thanks!
    You're quite welcome!

    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.
    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 with
    Java Code:
    c.fill = GridBagConstraints.BOTH;
    For my money, though, I avoid GridBagLayout when I can and instead nest JPanels that use simpler layouts for good effect and easier code.

  5. #5
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default 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:
    Java Code:
    public void componentResized(ComponentEvent e) {
            setPreferredSize(new Dimension(getParent().getWidth(),getParent().getHeight()));
            clear();
            validate();
        }
    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.

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

    Default 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.

  7. #7
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default Re: component wont display

    ok, i will study them some more. thank you for your help

  8. #8
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default 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

  1. image correctly loaded, but JPanel wont display
    By apchar in forum AWT / Swing
    Replies: 3
    Last Post: 04-13-2011, 03:15 AM
  2. Why wont my JSP page display my selected item?
    By system32 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-01-2011, 04:02 PM
  3. wont display selected array
    By newbie80 in forum New To Java
    Replies: 7
    Last Post: 03-21-2011, 01:10 AM
  4. uploaded image wont display if i change filename
    By schenker in forum Java Servlet
    Replies: 12
    Last Post: 06-11-2010, 11:13 AM
  5. [SOLVED] Last line in JTextArea wont display
    By Chris.Brown.SPE in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 01:52 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
  •