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.

Page 2 of 2 FirstFirst 12
Results 26 to 32 of 32

Thread: Accessing && Editing properties of objects in an array. Plus a few more questions.

  1. #26
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Accessing && Editing properties of objects in an array. Plus a few more questions

    Alright I'll put it all in the main class. Ill let you know how it goes.
    And thanks for all the help so far.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Accessing && Editing properties of objects in an array. Plus a few more questions

    You don't have to move the code.
    You do need to pass a reference to JFramePlayArea to the CreatePlayField class and save it in the jFrameMain variable.

  3. #28
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Accessing && Editing properties of objects in an array. Plus a few more questions

    I moved everything to the main class and still nothing.
     
    import java.awt.*;
    import javax.swing.*;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /*
     * jFramePlayArea.java
     *
     * Created on Aug 7, 2011, 5:07:11 PM
     */
    /**
     *
     * @author Cameron
     */
    public class jFramePlayArea extends javax.swing.JFrame {
     
        JLabel[] horizontalLines; //Declaring the horizontal lines array.
        JLabel[] verticalLines; //Declaring the vertical lines array.
        Point[] boxes; //Declaring the boxes array
     
        /** Creates new form jFramePlayArea */
        public jFramePlayArea() {
     
            initComponents();
     
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jButton1 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            jLabel1.setText("jLabel1");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jButton1))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(421, 421, 421)
                            .addComponent(jLabel1)))
                    .addContainerGap(613, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(138, 138, 138)
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 604, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>                        
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
        createBoxes();
     
    }                                        
     
        public void createBoxes() {
     
            /*
             * In this section we set the arrays up.
             */
     
            //Giving the arrays their size and initializing them.
            horizontalLines = new JLabel[272];
            verticalLines = new JLabel[272];
            boxes = new Point[272];
     
     
     
            //Variables used for creating the Box Points
            int XX;
            int YY;
            int B = 0;
            int boxY = 0;
     
            //Creating 272 JLabels for all of the vert and horiz lines
            for (int lineX = 1; lineX < 272; lineX++) {
     
                horizontalLines[lineX] = new JLabel();
                verticalLines[lineX] = new JLabel();
     
            }
     
            //Creating 256 Point variables used to draw and fill the boxes when needed.
            while (boxY < 272) {
     
                for (int A = 0; A <= 16; A++) {
     
                    //Calculating the variables used to set the points X and Y
                    XX = (A * 30) + 1;
                    YY = B * 30;
     
                    //creating the new point and setting its X and Y.
                    boxes[boxY] = new Point(XX, YY);
     
                    boxY++;
     
                    if (A == 16) {
     
                        B++;
     
                    }
                }
            }
     
            /*
             * In this section we will define all of the objects
             */
     
            for (int X = 1; X < 272; X++) {
     
                //Define horizontal line X
     
                horizontalLines[X].setBackground(new java.awt.Color(0, 0, 0));
                horizontalLines[X].setOpaque(true);
                horizontalLines[X].setLocation(boxes[X]);
                horizontalLines[X].setPreferredSize(new Dimension(28, 2));
                horizontalLines[X].setText("");
                horizontalLines[X].setVisible(true);
                getContentPane().add(horizontalLines[X]);
                //Define vertical line X
     
            }
     
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(jFramePlayArea.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(jFramePlayArea.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(jFramePlayArea.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(jFramePlayArea.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new jFramePlayArea().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration                   
    }

    Am I not adding it correctly? Do I have to do it like the IDE automatically does in initComponents()?

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Accessing && Editing properties of objects in an array. Plus a few more questions

    Now you have gotten into a conflict with the layout manager. You have already added some components to the frame and shown it. Where should the layout manager put the new ones you want to add?

    The IDE has generated lots of code to position the two components that are being shown. The new ones don't fit in with them.

  5. #30
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Accessing && Editing properties of objects in an array. Plus a few more questions

    Quote Originally Posted by Norm View Post
    Now you have gotten into a conflict with the layout manager. You have already added some components to the frame and shown it. Where should the layout manager put the new ones you want to add?

    The IDE has generated lots of code to position the two components that are being shown. The new ones don't fit in with them.
    Where I have specified with the points in the boxes[] array that I generated.
    Would it be easier to do all of this in a separate class or in the main class?

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Accessing && Editing properties of objects in an array. Plus a few more questions

    I don't normally do weird GUI. Removing components and resetting layout in a container is not something that makes sense to me.
    I really have no recommendations for you if want to do this kind of stuff.

  7. #32
    Junior Member
    Join Date
    Aug 2011
    Posts
    21
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Accessing && Editing properties of objects in an array. Plus a few more questions

    Alright I'll repost in a more suitable board. Thanks for all of the help.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM
  2. editing array
    By silverspoon34 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 15th, 2011, 01:40 AM
  3. Accessing Properties File
    By java_mein in forum Java Servlet
    Replies: 5
    Last Post: May 14th, 2010, 02:44 AM
  4. Vectors - accessing an unknown amount of objects
    By fox in forum Loops & Control Statements
    Replies: 1
    Last Post: May 7th, 2010, 03:54 PM
  5. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM