Results 1 to 5 of 5
- 02-11-2011, 12:31 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
please help with passing variables between class's
Hello everyone. Im new to java and this forum and I need some help passing variables between class's. I am writing a soduku game. I have the main class create an object from the class SudokuBoardDisplay. The class board creates a new object, from the class MyItems to create each cell of the soduku board. I'm having problems passing parameters between classes. heres what I have:
Board (Main Class):
Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import javax.swing.*; import java.awt.event.ActionListener; public class Board extends JFrame implements ActionListener{ private SudokuBoardDisplay _sudokuBoard = new SudokuBoardDisplay(null); public Board() { // 1... Create/initialize components JButton moveBtn = new JButton("Check For Solution"); JPanel controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); controlPanel.add(moveBtn); moveBtn.addActionListener(new MoveListener()); JPanel content = new JPanel(); content.setLayout(new BorderLayout()); content.add(controlPanel, BorderLayout.NORTH); content.add(_sudokuBoard, BorderLayout.CENTER); setContentPane(content); setTitle("Sudoku"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } public void actionPerformed(ActionEvent e) { } class MoveListener implements ActionListener { private String _board; public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null, "Checking for solution" + _board); } } public static void main(String[] args) { new Board().setVisible(true); } }
SudokuBoardDisplay Class:
MyItems Class:Java Code: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; 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-1;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(i + "," + j ); SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1, 1, 5, 1, Color.BLACK)); this.add(SodukuCell); repaint(); } else if ((i==3) || (i==6)) { SodukuCell.setMyPos(acrossOrDown,Down); SodukuCell.setNewName(i + "," + j ); SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1, 1, 1, 5, Color.BLACK)); this.add(SodukuCell); repaint(); } else { SodukuCell.setMyPos(acrossOrDown,Down); SodukuCell.setNewName(i + "," + j); SodukuCell.ChangeBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.black)); this.add(SodukuCell); repaint(); } } } }
what i want to do is, when MyItems class is initialized, set the label to a specific value. You can see that, once i create the object from the class, I call a method in that class to change a particular value (in the MyItems.SetNewName method. the value gets changed there but the label doesnt change. anyone have any ideas?Java Code:import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.BorderFactory; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.Border; //======================================================================= 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 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); System.out.print("my new name is:" + MyName + "\n"); repaint(); } public void setNewName(String NewName) { this.MyName = NewName; System.out.print("coming in:" + this.MyName + "\n"); 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) { // throw new UnsupportedOperationException("Not supported yet."); String s = (String) JOptionPane .showInputDialog(this, "Please enter a number:\n" + this.getParent().getName(), "Please enter a number!", JOptionPane.PLAIN_MESSAGE, null, null, " "); } 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."); } }
thanks
jason
- 02-11-2011, 12:33 AM #2
TLDR
Did you repaint?
- 02-11-2011, 12:34 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
yeah, i got repaint everywhere. still not workin
- 02-11-2011, 12:39 AM #4
If you are talking about changing the JLabel MyNum when you call the setNewName method then please indicate where in the below code do you actually change the label?
Java Code:public void setNewName(String NewName) { this.MyName = NewName; System.out.print("coming in:" + this.MyName + "\n"); this.repaint(); }
- 02-11-2011, 01:27 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
Similar Threads
-
Passing dynamic variables between classes
By Brekk in forum New To JavaReplies: 1Last Post: 03-22-2010, 06:07 AM -
accessing a one class's non static variable from another class
By aruna1 in forum New To JavaReplies: 6Last Post: 03-31-2009, 04:27 AM -
How can i use a class's variable in another class?
By Aga^^ in forum New To JavaReplies: 7Last Post: 03-16-2009, 07:19 AM -
passing variables to another frame
By Grom in forum New To JavaReplies: 10Last Post: 11-09-2008, 05:55 PM -
is synchronization on method passing local variables as parameters needed
By reddzer in forum Java ServletReplies: 0Last Post: 11-10-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks