Results 1 to 4 of 4
  1. #1
    fossiefos is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default paint on JButton event

    Hello everyone,

    i have this problem and i hope you can help me solve it:

    This is is my main class:

    Java Code:
    package manvrouw;
    
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class Main extends JFrame
    {
        public JButton buttonMan = new JButton("Man");
        public JButton buttonVrouw = new JButton("Vrouw");
        private PanelManVrouw panel = new PanelManVrouw(this);
    
    
        public Main()
        {
            this.setTitle("ManVrouw");
            this.setSize(200, 200);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(new GridLayout(2, 2));
    
            this.add(buttonMan,0);
            this.add(buttonVrouw,1);
            this.add(new PanelManVrouw(this), 2);
            this.add(new PanelManVrouw(this),3);
    
            buttonMan.addActionListener(panel);
            buttonVrouw.addActionListener(panel);
    
            this.setVisible(true);
        }
    
    
        public static void main(String[] args)
        {
            Main main = new Main();
        }
    
    }
    and this is my Jpanel / actionlistener class:

    Java Code:
    package manvrouw;
    
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JPanel;
    
    
    class PanelManVrouw extends JPanel implements ActionListener
    {
        Main main;
        int man = 0;
        int vrouw = 0;
    
        PanelManVrouw(Main main)
        {
            this.main = main;
        }
    
        @Override
        public void paint(Graphics g)
        {
            g.drawString(""+man, 20, 20);
        }
    
    
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == main.buttonMan)
            {
                man++;
            }
            else if(e.getSource() == main.buttonVrouw)
            {
                vrouw++;
            }
            repaint();
        }
        
    }
    the problem is that man and vrouw always stays 0 on the Jpanel, if i debug the code i can see that the int man or vrouw is incrementing by one, but it just doesn't repaint. ideas anyone ?
    Last edited by fossiefos; 09-24-2010 at 04:04 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Where in your code do you change some component being shown on the GUI to display the contents of those variables?
    You must write some code to change what is displayed.

  3. #3
    fossiefos is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    Well if i run the program the number in the Jpanel is showing: 0.
    i use the g.drawString to display the value of man and Vrouw

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Is the paint method being called? Add a print out to see if it is.
    It is better to override paintComponent for Swing components vs paint

    To see what your problem is you need to add some print outs for debugging.

    Add some print outs to your JPanel extension:
    Java Code:
     class PanelManVrouw extends JPanel implements ActionListener  {
        UpdateJPanel main;
        int man = 0;
        int vrouw = 0;
        static int id = 0;
        int thisID = 0;    // our id
    
        PanelManVrouw(UpdateJPanel main)     {
            System.out.println("constructor for id=" + id);
            thisID = id++;  // save our id
            this.main = main;
        }
    
    //    @Override
        public void paintComponent(Graphics g)     {
            System.out.println("pC man=" + man + ", id=" + thisID);
            g.drawString(""+man + ", id=" + thisID, 20, 20);
        }
    
    
        public void actionPerformed(ActionEvent e)      {
            System.out.println("aP in man=" + man+ ", id=" + thisID);
            if(e.getSource() == main.buttonMan)  {
                man++;
            }
            else if(e.getSource() == main.buttonVrouw)  {
                vrouw++;
            }
            System.out.println("aP out man=" + man);
            main.repaint();
        }
    }
    Last edited by Norm; 09-24-2010 at 09:15 PM.

Similar Threads

  1. Event On jButton
    By amitabh in forum NetBeans
    Replies: 1
    Last Post: 06-05-2010, 03:42 AM
  2. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 07:07 AM
  3. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  4. JList and JButton event handler not working
    By H3rtaherta in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2008, 12:00 AM
  5. Need help with JButton event
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 01:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •