Results 1 to 2 of 2
  1. #1
    Djangolo is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default Drawing on aJPanel

    I've built a GUI using Netbeans. The GUI includes a JPanel and I want to draw graphics on this panel. The instance of JPanel is called jPanel1 and is set to preferred size 604x310.

    If I create a bit of code like this -

    Java Code:
      public JPanel createContentPane () {
         JPanel redPanel = new JPanel();
            redPanel.setBackground(Color.red);
            redPanel.setLocation(10, 10);
            redPanel.setSize(50,50);
            return redPanel;
    }
    and then add this to jPanel1 like this -

    Java Code:
     jPanel1.add(createContentPane());
    the red square appears on the panel. Simple stuff.
    But if I do this -

    Java Code:
      public JPanel createContentPane (){
            
           JPanel mainPanel = new JPanel(new GridLayout(2, 4, 10, 30));
           mainPanel.setPreferredSize(new Dimension(604, 310));
            
            JPanel tempPanel = new JPanel();
            tempPanel.setBackground(Color.blue);
            tempPanel.setMinimumSize(new Dimension(50, 50));
            tempPanel.setMaximumSize(new Dimension(50, 50));
            tempPanel.setPreferredSize(new Dimension(50, 50));
            mainPanel.add(tempPanel);
            return mainPanel;
    and then try and add it to jPanel1, nothing is displayed.

    Can anyone tell me why this happens, 'cos I don't understand what's wrong.

    Thanks for any help.

    DJ

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test {
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(getContentPane());
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        private static JPanel createContentPane() {
            JPanel redPanel = new JPanel();
            redPanel.setBackground(Color.red);
            redPanel.setLocation(10, 10);
            redPanel.setSize(50,50);
            return redPanel;
        }
    
        private static JPanel getContentPane(){
            // GridLayout tends to ignore the size hints,
            // creates a grid of the available space and
            // tries to size the components to fill the
            // space of the grid cells.
            JPanel mainPanel = new JPanel(new GridLayout(2, 4, 10, 30));
            mainPanel.setPreferredSize(new Dimension(604, 310));
    
            JPanel tempPanel = new JPanel();
            tempPanel.setBackground(Color.blue);
            tempPanel.setMinimumSize(new Dimension(50, 50));
            tempPanel.setMaximumSize(new Dimension(50, 50));
            tempPanel.setPreferredSize(new Dimension(50, 50));
            mainPanel.add(tempPanel);
            mainPanel.add(createContentPane());
            mainPanel.add(createContentPane());
            mainPanel.add(createContentPane());
            return mainPanel;
        }
    }

Similar Threads

  1. drawing window
    By BlitzA in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 12:55 PM
  2. New: Want to understand Drawing...
    By diRisig in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 08:13 AM
  3. drawing window
    By BlitzA in forum Advanced Java
    Replies: 0
    Last Post: 12-30-2007, 05:39 PM
  4. Drawing outside paintComponent()
    By DarkSide1 in forum Java 2D
    Replies: 2
    Last Post: 11-08-2007, 10:36 PM
  5. Help with Drawing a line
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:40 AM

Posting Permissions

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