Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Having trouble with Jpanel design view forms :(

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble with Jpanel design view forms :(

    Apologies for my first post being a help one , but I'm in a slight bit of a pickle and my head hurts

    Very basic problem but I can't get my head around forms , anyway Here I have some code for displaying and tracking movement of the mouse over the form .

    package Test;
     
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    /**
     * @author WorldsTallestMidget
     */
     
     
    public class Main {
     
        private JPanel slotsPanel = new JPanel(new GridLayout(8, 8));
     
        public Main() {
            CreateUI();
        }
     
     
        public void CreateUI() {
            JFrame window = new JFrame("Test");
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setBounds(50, 50, 400, 400);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(CreateDisplay(), BorderLayout.CENTER);
            window.add(panel, BorderLayout.CENTER);
            window.setVisible(true);
     
        }
     
     
            private JPanel CreateDisplay() {
            JPanel mainPanel = new JPanel(new BorderLayout());
     
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    final JPanel slots = new JPanel();
                    slots.setBorder(BorderFactory.createLineBorder(Color.black));
                    slots.setBackground(new Color(255, 222, 188));
                    slots.addMouseListener(new MouseListener() {
                        public void mouseClicked(MouseEvent me) {
                        }
                        public void mousePressed(MouseEvent me) {
                        }
                        public void mouseReleased(MouseEvent me) {
                        }
     
                        public void mouseEntered(MouseEvent me) {
                              slots.setBackground(new Color(255, 255, 188));
                        }
                        public void mouseExited(MouseEvent me) {
                              slots.setBackground(new Color(255, 222, 188));
                        }
                    });
     
                    slotsPanel.add(slots);
                }
            }
     
            mainPanel.add(slotsPanel, BorderLayout.CENTER);
            return mainPanel;
        }
     
        public static void main(String[] args) {
     
        new Main();
     
        }
     
    }

    Now I think I have it working to spec , its tracking the movements correctly . But the problem is I don't know how to import it into the design view of a jframe . Its seems easier in class form and we only have to do as such, but being from a vb background and a complete beginner I like the robustness and easier snap to controls especially . Can anyone give me some hints on how to get the jpanels declared and such as when using the design view jpanel forms they can't be editted / are protected

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble with Jpanel design view forms :(

    jpanel forms they can't be editted
    JPanels are not editable. You use a text component of some kind for editable input.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with Jpanel design view forms :(

    apologies if my terminology is terrible

    If I place a jpanel on the form using design view , The variable Declarations are protected . Is there anyway around this ?

    specifically for
    private JPanel slotsPanel = new JPanel(new GridLayout(8, 8));

    OK Tried to add post-declaration code of */private JPanel jPanel1 = new JPanel(new GridLayout(8, 8)); but no luck . Compiles fine but no display

    Essentially all code seems to be the same , but It will not draw the board when it comes to the design view form .

    But If I build the exact same one using a class file it works fine

    Heres the code for the actual NewjPanel . pretty much the same . Anyone know why it won't display yet will in a class file ?

    public class NewJPanel extends javax.swing.JPanel {
     
        /** Creates new form NewJPanel */
        public NewJPanel() {
            initComponents();
            CreateUI();
        }
     
        public void CreateUI() {
            JFrame window = new JFrame("Test");
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setBounds(50, 50, 400, 400);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(CreateDisplay(), BorderLayout.CENTER);
            window.add(panel, BorderLayout.CENTER);
           window.setVisible(true);
     
        }
     
        private JPanel CreateDisplay() {
            JPanel mainPanel = new JPanel(new BorderLayout());
     
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    final JPanel slots = new JPanel();
                    slots.setBorder(BorderFactory.createLineBorder(Color.black));
                    slots.setBackground(new Color(255, 222, 188));
                    slots.addMouseListener(new MouseListener() {
                        public void mouseClicked(MouseEvent me) {
                        }
                        public void mousePressed(MouseEvent me) {
                        }
                        public void mouseReleased(MouseEvent me) {
                        }
     
                        public void mouseEntered(MouseEvent me) {
                              slots.setBackground(new Color(255, 255, 188));
                        }
                        public void mouseExited(MouseEvent me) {
                              slots.setBackground(new Color(255, 222, 188));
                        }
                    });
     
                   jPanel1.add(slots);
                }
            }
     
            mainPanel.add(jPanel1, BorderLayout.CENTER);
            return mainPanel;
        }
     
        /** This method is called from within the constructor to
     
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
     
            jPanel1.setBackground(new java.awt.Color(255, 255, 255));
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 459, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 430, Short.MAX_VALUE)
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(269, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(63, Short.MAX_VALUE))
            );
        }// </editor-fold>
     
        public static void main(String[] args) {
     
        new NewJPanel();
     
            // TODO code application logic here
        }
     
       // private JPanel slotsPanel = new JPanel(new GridLayout(8, 8));
     
        // Variables declaration - do not modify
        /*
        private javax.swing.JPanel jPanel1;
        */private JPanel jPanel1 = new JPanel(new GridLayout(8, 8));
        // End of variables declaration
     
    }
    Last edited by WorldsTallestMidget; June 10th, 2011 at 02:36 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble with Jpanel design view forms :(

    The variable Declarations are protected
    Can you explain what you mean here? Why the capital D?
    How are the declarations protected? Who protects them?

    Tried to add post-declaration code of */private JPanel jPanel1 = new JPanel(new GridLayout(8, 8)); but no luck
    If you got errors, please copy and paste the full text here.

    Are you having a problem with your IDE?

    If I build the exact same one using a class file
    This is another strange one. You don't work with class files. They are created by the compiler.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with Jpanel design view forms :(

    Quote Originally Posted by Norm View Post
    Can you explain what you mean here? Why the capital D?
    How are the declarations protected? Who protects them?


    If you got errors, please copy and paste the full text here.

    Are you having a problem with your IDE?


    This is another strange one. You don't work with class files. They are created by the compiler.
    Apologies and thanks for your time. I originally coded it for instance in the main.java file . The first one compiles fine and displays , tracks movement and such . In the picture provided its the top program and is the main.java file

    The one beneath it however is the one using the jform ( newjpanel.java ). Same code but doesn't seem to be working at all

    Everything seems to be compiling fine , but I don't understand why it won't display in the newjpanel form

    http://img97.imageshack.us/img97/854/netbean.jpg shows the forms / program
    Last edited by WorldsTallestMidget; June 10th, 2011 at 02:57 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble with Jpanel design view forms :(

    You need to talk to your IDE and tell it that you're in charge. It has a mind of its own and is changing how you want to define jPanel1. See what I assume is generated code that defines the layout for jPanel1. It is called after you have defined jPanel1.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with Jpanel design view forms :(

    This is what confuses me , i can only see it being defined once . If i try to redeclare it anywhere I will get "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - jPanel1 is already defined in Test.NewJPanel"

    I use the post declarations to change that to "private JPanel jPanel1 = new JPanel(new GridLayout(8, 8));" . Yet it doesn't seem to work and its the only time its ever defined

    I can create one from scratch in the code if it doesn't coincide with another created element from the design view . But that seems redundant as I would like to be able to place it using the form designer
    Last edited by WorldsTallestMidget; June 10th, 2011 at 03:28 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Having trouble with Jpanel design view forms :(

    You define the layout for jPanel1 when you first initialize it:
    private JPanel jPanel1 = new JPanel(new GridLayout(8, 8));

    The generated code changes it:
    jPanel1.setLayout(jPanel1Layout);

    You'll have to work it out with your IDE. I don't use one and don't know how to use them.

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with Jpanel design view forms :(

    ok thanks

    will have a look

Similar Threads

  1. Killing view of a dead model
    By Alice in forum Java Theory & Questions
    Replies: 9
    Last Post: May 30th, 2011, 05:27 AM
  2. unable to view the result generated by stored procedure
    By najmudeenhaja in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2010, 08:39 AM
  3. How can i view search results by using servlet and jsp?
    By noFear in forum Java Theory & Questions
    Replies: 3
    Last Post: August 27th, 2010, 11:22 AM
  4. Continuous Forms
    By adam1305 in forum AWT / Java Swing
    Replies: 0
    Last Post: March 3rd, 2010, 01:04 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM