Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default Printing out value of an object

    hello everyone,
    I have a panel with several (81) objects on them (created from a class i wrote). Each object has a specific name and other values. I know i can print out the name of each object with this code:

    Java Code:
     for (Component c : _sudokuBoard.getComponents()) {  
    //_sudokuBoard comes from a class
                         System.out.print(i + ": " + c.getName() + "\n");
    //this will print out all the objects names thats found in  _sudokuBoard
    
                 
                    }
    But, what if i want to print out those other values. For instance, each of the objects also have another quality called "MyValue" which is a publicly declared variable and I would like to print that out as well.

    Thanks for any help
    jason

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    they are initiated in the _sudokuBoard class this way:
    MyItems SodukuCell = new MyItems();
    SodukuCell.setMyPos(acrossOrDown,Down);
    SodukuCell.setNewName("" + TheBoardTest[j-1][i-1]);
    SodukuCell.ChangeBorder(BorderFactory.createMatteB order(1, 1, 5, 1, Color.BLACK));
    SodukuCell.MyValue("" + TheBoardTest[j-1][i-1]);
    this.add(SodukuCell);
    repaint();

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    How about a toString method.

  5. #5
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    yeah, that just prints out the same info as above. I need to print out values that i created with the class

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by jasonwucinski View Post
    yeah, that just prints out the same info as above. I need to print out values that i created with the class
    Huh?

    A toString method can return a String consisting of any data you want.

  8. #8
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    Eranga, you mean "get" and "set" methods? yeah, each class of the object has several. as an example, i have a public variable called MyVal. In the class they each have a method called getMyVal and setMyVal. example:

    public void setMyVal(String myNewVal)
    {
    MyVal = myNewVal
    }


    public void getMyVal()
    {
    return MyVal;
    }

    Junky:
    Printing out using toString() seems to only print out the generic info of the object (its name, size, possition, things like that) but not user defined values that i created in the class

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    SO CHANGE IT! Make it do want you want instead. If for some reason you cannot change it then write another method that returns all the information that you want in a user friendly format and call that instead.

  10. #10
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    i'm sorry, I should have posted that I'm new to java. change what? If i knew how to make it do what I wanted, I wouldn't be asking lol. I've created get and set methods but how do i get a value from an object thats already created. I know i can use

    SomeClass newClass = new SomeClass
    String myVar = newClass.GetMethod;

    But isnt that just creating a new object and not getting the value of a previously created object?

    Thanks

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    class Person {
        String name;
        int lucky;
        String colour;
    
        Person(String n, int l, String c) {
            name = n;
            lucky = l;
            colour = c;
        }
    
        public String toString() {
            return "My name is " + name + "\nMy lucky number is " + lucky + "\nand my favourite colour is " + colour;
        }
    
        public static void main(String[] args) {
            Person p = new Person("Earl", 42, "Red");
            System.out.println(p);
        }
    }
    Does this make sense?

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If this is not what you want then you are not explaining yourself clearly enough for me to understand.

  13. #13
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    hello,
    let me try to explain what i'm doing in more detail. I'm creating a soduku game. the main class is split up into two panels. the first panel has some controls (a button) and the second panel has an instance of a class. that class is where i draw the game board. The board is made up of another instance of a class, one for each tile on the board. I chose this method to 1. teach myself about classes and 2. i thought by using this method, i could create the object and each square would hold several bits of data (ie, the name of the object, the value of the string held in the object, its position in the 9X9 grid, etc). some of these values will never change after the object is created (like the name) but some will (like the value of the label in the object)

    So, from the main class, as i said i have a button there, i want the button to print out the various values of each of the 81 objects. i can print out the name of each class, because i assume all classes have some name but not the values i created specifically for the object. Your example looks like it might work except that the class that's printing out the values of the object is not the same class as the object. (I guess the parent is trying to get the values of each child). So, how do i get the child object to return values to the parent? If it would help, i can show my code but its long.

  14. #14
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    Java Code:
        public class Board extends JFrame implements ActionListener{
            private SudokuBoardDisplay _sudokuBoard = new SudokuBoardDisplay(null);
            public  JPanel controlPanel = new JPanel();
            public  JPanel content = new JPanel();
            public int[][]TheBoard;
            public int[][] NewBoard=
                            {{0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0, 0, 0, 0, 0}};
    
    
            public Board() {
                JButton moveBtn = new JButton("Check For Solution");           
                controlPanel.setLayout(new FlowLayout());
                controlPanel.add(moveBtn);
                moveBtn.addActionListener(new MoveListener());
               
                content.setLayout(new BorderLayout());
                content.add(controlPanel, BorderLayout.NORTH);
                content.add(_sudokuBoard, BorderLayout.CENTER);
                TheBoard = _sudokuBoard.TheBoardTest;
    
                setContentPane(content);
                setTitle("Sudoku");
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                pack();
            }
    
            public void actionPerformed(ActionEvent e) {
    
            }
    
            public void  setBoardVal(int[][] getIt){
                TheBoard = getIt;
            }
            public void fillTheArray(String MyVal, int Counter){
    
    
                         //System.out.print(MyVal + "\n");
                         NewBoard[0][0] =Integer.parseInt(MyVal);
    
       
    
    
              // System.out.print(Arrays.deepToString(NewBoard));
            }
            ///////////////////////////////////////////////////////////// MoveListener
            class MoveListener implements ActionListener {            
                public void actionPerformed(ActionEvent ae) {
                    JOptionPane.showMessageDialog(null, "Checking for solution");
                    int i =0;
                    for (Component c : _sudokuBoard.getComponents()) {
                        fillTheArray(c.getName(), i);
                        System.out.print(i + ": " + c.toString() + "\n");
                        i++;
                    }
    
    
                }
            }
    
    
            public static void main(String[] args) {
                new Board().setVisible(true);
            }
    
    }
    //=======================================================================
    
    
    class SudokuBoardDisplay  extends JComponent  {
        private static final int CELL_PIXELS = 49;  // Size of each cell.
        private static final int PUZZLE_SIZE = 9;   // Number of rows/cols
        private static final int BOARD_PIXELS = CELL_PIXELS * PUZZLE_SIZE;
        public int MyCounter =0;
    
          public  int[][] TheBoardTest =
                            {{0, 6, 0, 1, 0, 4, 0, 5, 0},
                             {0, 0, 8, 3, 0, 5, 6, 0, 0},
                             {2, 0, 0, 0, 0, 0, 0, 0, 1},
                             {8, 0, 0, 4, 0, 7, 0, 0, 6},
                             {0, 0, 6, 0, 0, 0, 3, 0, 0},
                             {7, 0, 0, 9, 0, 1, 0, 0, 4},
                             {5, 0, 0, 0, 0, 0, 0, 0, 2},
                             {0, 0, 7, 2, 0, 6, 9, 0, 0},
                             {0, 4, 0, 5, 0, 8, 0, 7, 0}};
     
        SudokuBoardDisplay(Object object) {
            setPreferredSize(new Dimension(BOARD_PIXELS + 2, BOARD_PIXELS + 2));
            setBackground(Color.WHITE);
            drawGridLines();
        }
    
    
    
        private void drawGridLines()
        {
            for (int i = 1; i <= PUZZLE_SIZE; i++)
            {
                 for (int j = 1; j <= PUZZLE_SIZE;j++)
                 {
                    int acrossOrDown =( i * CELL_PIXELS)-50;
                    int Down = (j * CELL_PIXELS)-50;
                    MyItems SodukuCell = new MyItems();
    
                    if ((j==3) || (j==6))
                    {
                        SodukuCell.setMyPos(acrossOrDown,Down);
                        SodukuCell.setNewName("" + TheBoardTest[j-1][i-1]);
                        SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1, 1, 5, 1, Color.BLACK));
                        SodukuCell.setName("" + TheBoardTest[j-1][i-1]);
                        this.add(SodukuCell);
                        repaint();
                        MyCounter++;
    
                    }
                    else  if ((i==3) || (i==6))
                    {
                        SodukuCell.setMyPos(acrossOrDown,Down);
                        SodukuCell.setNewName("" + TheBoardTest[j-1][i-1]);
                        SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1, 1, 1, 5, Color.BLACK));
                        SodukuCell.setName("" + TheBoardTest[j-1][i-1]);
                        this.add(SodukuCell);
                        repaint();
                        MyCounter++;
                    }
                    else
                    {
                        SodukuCell.setMyPos(acrossOrDown,Down);
                        SodukuCell.setNewName("" + TheBoardTest[j-1][i-1]);
                        SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black));
                        SodukuCell.setName("" + TheBoardTest[j-1][i-1]);
                        this.add(SodukuCell);
                        repaint();
                        MyCounter++;
                    }
                }
             }
    
        }
    
    }
    MyItems class:
    Java Code:
    public class MyItems extends JPanel implements MouseListener
    {
        public int MyX;
        public int MyY;
        public String MyName;
        public Border myBorder=BorderFactory.createMatteBorder(0, 0, 1, 1, Color.BLACK);
        public Color myColor = Color.WHITE;
        public Color myFontColor = Color.BLACK;
        public String lblNum = "test";
        public JPanel InputPanel;
        public int CurrentPos;
        public boolean canEdit;
        public JLabel MyNum = new JLabel("name=" + MyName);
    
     public MyItems()
        {
    
            this.setBorder(myBorder);
            this.setSize(50, 50);
            this.setLocation(MyX, MyY);
            this.setBorder(myBorder);
            this.addMouseListener(this);
            this.add(MyNum);       
            repaint();
        }
    
    public void setNewName(String NewName)
        {
            if (Integer.parseInt(NewName)==0){
                MyNum.setText("");
                canEdit = true;
            }
            else{
                MyNum.setText("" +NewName);
                canEdit = false;
            }
         
            MyName = NewName;
            this.repaint();
        }
    
        public void ChangeBorder(Border NewBorder)
        {
           myBorder = NewBorder;
           this.setBorder(NewBorder);
    
            if (this.MyName=="3,3"){
                 this.ChangeBorder(BorderFactory.createMatteBorder(1, 1, 5, 5, Color.BLACK));
    
            }
    
            this.repaint();
        }
    
        public void setMyPos(int i, int j)
        {
            MyX=i;
            MyY=j;
    
            this.setLocation(MyX, MyY);
            this.repaint();
        }
    
        public void mouseClicked(MouseEvent e) {
            if (canEdit == false) {
    
            }
            else{
                String str = JOptionPane.showInputDialog(null, "Please enter a number:\n", "Please enter a number",1);
            
                if(str != null){
                    MyNum.setForeground(Color.red);
                    MyNum.setText(str);
                    this.setName(str);
                }
                else{
                    
                }
          
                repaint();
    
                }
            }
        public void mousePressed(MouseEvent e) {
           // throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseReleased(MouseEvent e) {
           // throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseEntered(MouseEvent e) {
           // throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseExited(MouseEvent e) {
          //  throw new UnsupportedOperationException("Not supported yet.");
        }
    
    
    
    
    }

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Where did all this parent and child class carp come from? There is no inheritance in my example. Is there inheritance in your code?

    An object of one class can obtain access to an object of another class as long as it has a reference to that class and it calls methods on that class. Remember you can write as many methods in a class as your little heart desires.

    Java Code:
    class Foo {
        Bar b = new Bar();
    
        Foo() {
            b.doStuff();
            String s = b.getStuff();
        }
    }
    In the above example I did not include the code for the Bar class but that is unimportant. As long as you know that the 2 methods doStuff adn getStuff exist you can call them and that they will do what the Foo class is expecting.

  16. #16
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    ok, with your example, i understand it this way (please correct me).

    Bar b = new Bar();

    creates an object from the Bar class. I can use get/sets to set specific values in the b object and return that value.


    But, what happens if i've created 81 objects from the Bar class at the start of the game and i want to use getStuff() to return a value of a specific object? each object has a different value returned from getStuff(), which can be changed at any time. it seems like if i create a new object, its just gonna have the default of values.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I only briefly glanced at your code as it is too long. Generally when posting code you make a SSCCE (google it).

    Where are those 81 objects stored? Hopefully in an array or List etc. Lets say you want to use the first object.
    Java Code:
    // array
    objectArray[0].doStuff();
    
    // List
    objectList.get(0).dostuff();
    No you do not have to create a new object. Just find the object that you do want to manipulate and call the method(s) on that object.

  18. #18
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    nope, i wasn't storing them anywhere. so, i guess when i create the object, then i would add it to an array of objects? thanks for the help
    jason

  19. #19
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    In MyItem class which variable is the Soduko value stored in that you are trying to get?

  20. #20
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    i took it out in the above section so that i could have a working example. but, MyX and MyY are good examples or lblNum

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  2. Printing Object Information
    By dom12 in forum New To Java
    Replies: 10
    Last Post: 11-04-2010, 06:39 PM
  3. Printing values from object in Array?
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 05:19 AM
  4. Replies: 7
    Last Post: 10-06-2009, 05:58 AM
  5. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 09:36 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
  •