Results 1 to 20 of 22
Thread: Printing out value of an object
- 02-15-2011, 01:17 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
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:
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.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 }
Thanks for any help
jason
- 02-15-2011, 01:23 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
How did you initialize each object?
- 02-15-2011, 01:24 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
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();
- 02-15-2011, 01:25 AM #4
How about a toString method.
- 02-15-2011, 01:29 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
yeah, that just prints out the same info as above. I need to print out values that i created with the class
- 02-15-2011, 01:30 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Didn't you implement accessor/mutators ?
- 02-15-2011, 01:34 AM #7
- 02-15-2011, 01:41 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
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
- 02-15-2011, 01:45 AM #9
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.
- 02-15-2011, 01:51 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
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
- 02-15-2011, 01:58 AM #11
Does this make sense?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); } }
- 02-15-2011, 02:00 AM #12
If this is not what you want then you are not explaining yourself clearly enough for me to understand.
- 02-15-2011, 02:11 AM #13
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
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.
- 02-15-2011, 02:15 AM #14
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
MyItems class: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++; } } } } }
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."); } }
- 02-15-2011, 02:20 AM #15
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.
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.Java Code:class Foo { Bar b = new Bar(); Foo() { b.doStuff(); String s = b.getStuff(); } }
- 02-15-2011, 02:29 AM #16
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
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.
- 02-15-2011, 02:37 AM #17
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.
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.Java Code:// array objectArray[0].doStuff(); // List objectList.get(0).dostuff();
- 02-15-2011, 02:51 AM #18
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
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
- 02-15-2011, 03:09 AM #19
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
In MyItem class which variable is the Soduko value stored in that you are trying to get?
- 02-15-2011, 03:13 AM #20
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
Similar Threads
-
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:11 PM -
Printing Object Information
By dom12 in forum New To JavaReplies: 10Last Post: 11-04-2010, 06:39 PM -
Printing values from object in Array?
By thesinter in forum New To JavaReplies: 3Last Post: 01-20-2010, 05:19 AM -
Rotated Shape Object Line weight is not retaining properly in printing
By dorairaj in forum AWT / SwingReplies: 7Last Post: 10-06-2009, 05:58 AM -
[SOLVED] printing address of current instance of object?
By emceenugget in forum New To JavaReplies: 1Last Post: 02-09-2009, 09:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks