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 4 of 4

Thread: JPanel "ActionListener"

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JPanel "ActionListener"

    I'm recreating Minesweeper because why not? Anyway, I've run into a problem with how I have it coded when I wish to update my mine counter (the box traditionally on top of the screen).

    Anyway, I have a JPanel that contains most of the components on the screen (all but the menu bar). I have a separate JPanel that holds the board which contains the game play, etc.

    I am at a loss on how to update the mine count (the user-altered and visible mine counter). An action listener seemed to be the obvious choice for me, but there is no action listener for a JPanel.

    I have tried a mouse listener, but in order for that to work, I have to depress a mine first and click on the empty places for the count to be updated. Furthermore, when I right click on a square, the mine count should lower by one and raise by one when I click it again, which a mouse listener does not work for me, to the best of my knowledge.

    I am unsure on how to listen for a type of event that would suit my needs. Any help would be appreciated.

    Edit: Maybe, could I make it so my JPanel that holds the board "throws" or fires an action and then I could listen for that?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: JPanel "ActionListener"

    It sounds to me like there may be an issue in the organization. Where data is stored vs where access to said data is needed.
    The mouse listener should be able to do the things you talked about.
    Post code that shows the problem, at least a sample to go on, no need to post the entire game or anything.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel "ActionListener"

    public class LargeMinesweeperPanel extends JPanel {
     
    	private MinesweeperPanel panel;
    	private JLabel mineCount;
     
    	public LargeMinesweeperPanel(int numOfRows, int numOfCols, int minePercAsInt, int sizeOfButton) {
    		panel = new MinesweeperPanel(numOfRows, numOfCols, minePercAsInt, sizeOfButton);
                    //This is the panel I want to listen to. Specifically, I want to know when a button in this
                    //panel is right clicked.
     
    		mineCount = new JLabel("" + panel.getProposedMines());	
     
    		mineCount.setHorizontalAlignment(SwingConstants.CENTER);
     
    		this.setLayout(new BorderLayout());
    		this.add(mineCount, BorderLayout.NORTH);
     
    		this.add(panel);
    	}
     
            public MinesweeperPanel getPanel() {
    		return panel;
    	}
    }

    This is what I add to my JFrame, along with a MenuBar.
    I've realized this is badly organized for the problem stated above as well as how to get the MenuBar to interact with the board. That's where the getPanel() method comes into play.

    Note: The reason I have it situated like this is because I like the look I achieved and I couldn't really find a way to achieve this without this class. I'm kind of bad with layout managers.

    I also tried having the BoardPanel declared in the MinesweeperPanel and added, but I couldn't figure out how to set which panel the paintComponent() method draws to. If I could figure that out, I could get it to work.


    Here's the organization:
    JFrame contains a JMenuBar and a JPanel (I'll call this TopPanel)
    TopPanel contains a Label and another Panel

    Problem: I want to be able to listen to a button click (left and right) from Panel in TopPanel

    Possible solutions to my problem:
    1. Figure out how to listen to the left and right click from Panel in TopPanel
    2. Get rid of TopPanel and have Panel contain the "label" from top and the array of buttons. My problem here was I did not know how draw on the panel that contains the buttons apart from the panel that contains it. I tried this earlier, but it would clear the numbers that are supposed to lie behind the buttons after they were drawn (I think). Here's the paintComponent() method I tried from before.

    public class MinesweeperPanel extends JPanel implements ActionListener, MouseListener {
     
         ...
     
         public void paintComponent(Graphics g) {
    		super.paintComponent(g);
     
    		g = boardPanel.getGraphics();
    		super.paintComponent(g);
     
                    //The ugly looking code here below paints the lines and numbers onto the panel.
                    //It works fine, my problem would lie above here I believe.
     
    		g.setColor(Color.LIGHT_GRAY);
    		Tile button = board[0][0];
    		for(int i = 0; i < rows; i++) {
    			g.drawLine(0, (int)(i * button.getSize().getHeight()), getWidth(), (int)(i * button.getSize().getHeight()));
    		}
     
    		for(int i = 0; i < cols; i++) {
    			g.drawLine((int)(i * button.getSize().getWidth()), 0, (int)(i * button.getSize().getWidth()), getHeight());
    		}
     
    		g.setColor(Color.BLACK);
    		for(int r = 0; r < rows; r++) {
    			for(int c = 0; c < cols; c++) {
    				button = board[r][c];
    				if(button.getLocalMines() != 0)
    					g.drawString(button.getLocalMines() + "", 
    							(int) Math.ceil(button.getX() + button.getSize().getWidth() / 2 - g.getFont().getSize() / 2 + 3),
    							(int) Math.ceil(button.getY() + button.getSize().getHeight() / 2 + 3));
    			}
    		}
    	}
     
            ...
     
    }

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: JPanel "ActionListener"

    Quote Originally Posted by CallMeCrazySam View Post
    Problem: I want to be able to listen to a button click (left and right) from Panel in TopPanel

    Possible solutions to my problem:
    1. Figure out how to listen to the left and right click from Panel in TopPanel
    2. Get rid of TopPanel and have Panel contain the "label" from top and the array of buttons. My problem here was I did not know how draw on the panel that contains the buttons apart from the panel that contains it. I tried this earlier, but it would clear the numbers that are supposed to lie behind the buttons after they were drawn (I think). Here's the paintComponent() method I tried from before.
    1. TopPanel extends JFrame implements MouseListener, MouseMotionListener, MouseWheelListener .....listen to suit your needs
    2.You can work it with or without the JPanel class, but a JPanel is not a top level container and must be contained within one. (ie your TopPanel is a JFrame, which is one of the top level containers)
    You can add listeners to the JPanel, or the JFrame, or both to suit your needs

    Try to keep the logic separate from the GUI. The "driver" class(es) should contain the logic and control the flow and respond to the user. The GUI class(es) should display the results of the driver(s) in pretty colors and provide the interface to interact with the user. The GUI should do nothing more than report user interaction to the driver(s) and continue waiting for user inputs, while the drivers do what was requested in the background
    Last edited by jps; May 28th, 2013 at 08:18 PM. Reason: Corrected typo

Similar Threads

  1. Conceptual Questions Related To "super" keyword and "constructor".
    By wikki2013 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2013, 02:14 PM
  2. how can I "draw" images on a JPanel?
    By kaskoepoes112 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 3rd, 2013, 02:51 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM