Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Repaint JPanel problem in java

Repaint JPanel problem in java

Scheduled Pinned Locked Moved Java
javagraphicshelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flashery
    wrote on last edited by
    #1

    Hi to all I have a problem in repainting my JPanel. I am creating a ShapeApplication program that can draw a shape on a click of a button. I have different classes in my application. JPanel1 extends JPanel = use to draw different shape JPanel2 extends JPanel= use to put my components such as buttons, label, and etc.. JFrame extends JFrame= use to put my JPanel1 and JPanel2 I have also normal classes which I implemented on my own Shape = my Parent Class Rectangle extends Shape = subclass of Shape and having an attribute of a Rectangle shape such as height and width Square extend Shape = subclass of Shape and having an attribute of a Square shape such as height and width My problem now is that when I use a repaint in actionPerformed method under my JPanel2 which contain my button component there is no effect on my drawing class which is my JPanel1 class I implemented it this way JPanel1.repaint(); I initialize my button's ActionListener like this.. button.addActionListener(this);//I am confuse what would be in the parameter. Can you give a hint on this one? Where do I get wrong?

    T 1 Reply Last reply
    0
    • F flashery

      Hi to all I have a problem in repainting my JPanel. I am creating a ShapeApplication program that can draw a shape on a click of a button. I have different classes in my application. JPanel1 extends JPanel = use to draw different shape JPanel2 extends JPanel= use to put my components such as buttons, label, and etc.. JFrame extends JFrame= use to put my JPanel1 and JPanel2 I have also normal classes which I implemented on my own Shape = my Parent Class Rectangle extends Shape = subclass of Shape and having an attribute of a Rectangle shape such as height and width Square extend Shape = subclass of Shape and having an attribute of a Square shape such as height and width My problem now is that when I use a repaint in actionPerformed method under my JPanel2 which contain my button component there is no effect on my drawing class which is my JPanel1 class I implemented it this way JPanel1.repaint(); I initialize my button's ActionListener like this.. button.addActionListener(this);//I am confuse what would be in the parameter. Can you give a hint on this one? Where do I get wrong?

      T Offline
      T Offline
      twseitex
      wrote on last edited by
      #2

      Hi, JFrame is the container of JPanels (Jframe.add(child);) JPanel1 with own event listener JPanel2 with own event listener You must call JPanel1.repaint() in event listener of JPanel 2 You must call JPanel2.repaint() in event listener of JPanel 1 ----------------------------------------------------- referenz_of_button.addActionListener // no action commands, but all action vvents ( new ActionListener() {public void actionPerformed(ActionEvent e) // e is placeholder form actionevent type (see Class ActionEvent) { // do something ony for object with referenz_of_button } } ); JButton supports ActionListener JLabel supports ActionListener JButton and JLabel support MouseListener , KeyListener etc. ---------------------------------------------------- only if you want to use labels (action commands) of buttons public void actionPerformed(ActionEvent event) { String cmd = event.getActionCommand(); // get label of button, label is the action command if (cmd.equals("label_of_button1")) { // do something für button1 } if (cmd.equals("label_of_button2")) { // do something für button1 } } referenz_of_button1.addActionListener(this); // label of button must be "label_of_button1" referenz_of_button2.addActionListener(this); // label of button must be "label_of_button2" JButton supports ActionListener JLabel supports ActionListener

      F 2 Replies Last reply
      0
      • T twseitex

        Hi, JFrame is the container of JPanels (Jframe.add(child);) JPanel1 with own event listener JPanel2 with own event listener You must call JPanel1.repaint() in event listener of JPanel 2 You must call JPanel2.repaint() in event listener of JPanel 1 ----------------------------------------------------- referenz_of_button.addActionListener // no action commands, but all action vvents ( new ActionListener() {public void actionPerformed(ActionEvent e) // e is placeholder form actionevent type (see Class ActionEvent) { // do something ony for object with referenz_of_button } } ); JButton supports ActionListener JLabel supports ActionListener JButton and JLabel support MouseListener , KeyListener etc. ---------------------------------------------------- only if you want to use labels (action commands) of buttons public void actionPerformed(ActionEvent event) { String cmd = event.getActionCommand(); // get label of button, label is the action command if (cmd.equals("label_of_button1")) { // do something für button1 } if (cmd.equals("label_of_button2")) { // do something für button1 } } referenz_of_button1.addActionListener(this); // label of button must be "label_of_button1" referenz_of_button2.addActionListener(this); // label of button must be "label_of_button2" JButton supports ActionListener JLabel supports ActionListener

        F Offline
        F Offline
        flashery
        wrote on last edited by
        #3

        I am confuse about your statement below JPanel1 with own event listener JPanel2 with own event listener You must call JPanel1.repaint() in event listener of JPanel 2 You must call JPanel2.repaint() in event listener of JPanel 1 Can you give me some example on this one just a short code. I really don't get it.. Hi here is my code of my actionPerformed under JPanel2

        JPanel1 jpanel1 = new JPanel1();
        @Override
        public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnOK) {

                jpanel1.repaint();
        
            } 
        }
        
        1 Reply Last reply
        0
        • T twseitex

          Hi, JFrame is the container of JPanels (Jframe.add(child);) JPanel1 with own event listener JPanel2 with own event listener You must call JPanel1.repaint() in event listener of JPanel 2 You must call JPanel2.repaint() in event listener of JPanel 1 ----------------------------------------------------- referenz_of_button.addActionListener // no action commands, but all action vvents ( new ActionListener() {public void actionPerformed(ActionEvent e) // e is placeholder form actionevent type (see Class ActionEvent) { // do something ony for object with referenz_of_button } } ); JButton supports ActionListener JLabel supports ActionListener JButton and JLabel support MouseListener , KeyListener etc. ---------------------------------------------------- only if you want to use labels (action commands) of buttons public void actionPerformed(ActionEvent event) { String cmd = event.getActionCommand(); // get label of button, label is the action command if (cmd.equals("label_of_button1")) { // do something für button1 } if (cmd.equals("label_of_button2")) { // do something für button1 } } referenz_of_button1.addActionListener(this); // label of button must be "label_of_button1" referenz_of_button2.addActionListener(this); // label of button must be "label_of_button2" JButton supports ActionListener JLabel supports ActionListener

          F Offline
          F Offline
          flashery
          wrote on last edited by
          #4

          Hello twseitex I manage to solve my issue on "Repaint JPanel problem in java". The problem is I just put all of my two classes (JPanel1 and JPanel2) directly on a JFrame which make my button doesn't send any event on my JPanel1 class because they are on separate classes. Yes your right I now understand what you mean on your statement. JFrame is the container of JPanels (Jframe.add(child);) JPanel1 with own event listener JPanel2 with own event listener You must call JPanel1.repaint() in event listener of JPanel 2 You must call JPanel2.repaint() in event listener of JPanel 1 This makes me think if I will just and my JPanel1 class on my JPanel2 class which handles all the event this will be done. In this case I will shorten my codes by not implementing each class with their own event listener and I am right I have fix the issue. This is really a great lesson for me as a newbie programmer. If you want to see my initial ShapeApplication heres the link for the jar file Click Here

          T 1 Reply Last reply
          0
          • F flashery

            Hello twseitex I manage to solve my issue on "Repaint JPanel problem in java". The problem is I just put all of my two classes (JPanel1 and JPanel2) directly on a JFrame which make my button doesn't send any event on my JPanel1 class because they are on separate classes. Yes your right I now understand what you mean on your statement. JFrame is the container of JPanels (Jframe.add(child);) JPanel1 with own event listener JPanel2 with own event listener You must call JPanel1.repaint() in event listener of JPanel 2 You must call JPanel2.repaint() in event listener of JPanel 1 This makes me think if I will just and my JPanel1 class on my JPanel2 class which handles all the event this will be done. In this case I will shorten my codes by not implementing each class with their own event listener and I am right I have fix the issue. This is really a great lesson for me as a newbie programmer. If you want to see my initial ShapeApplication heres the link for the jar file Click Here

            T Offline
            T Offline
            twseitex
            wrote on last edited by
            #5

            Hi, JFrame have repaint from Component. JPanel have repaint form JComponent,and Component. Repaint JFrame (contianer) or repaint Jframes. You want repaint JFrames. Change GUI of JPanel1 and call Jpanel1.repaint() ... Same thing with other words. To connect 2 JPanels in (a) different classes you must have references of this objects to create a handler to manage events of this JPanels or (b) put all classes into same container class. (a) Constructor create instancte of class. Inside of class you can save pointer of class itself into a global variable. Read out this variable after create instance of class: use a global function of class to get pointer of class istself. And this pointer use for e.g. repaint() for this instance. (b) Or put all classes into same container class. I use (a) in my java audio player. 1 main class for audio, 1 main class for GUI (JPanels etc.). 1 main class for applet. Applet call constructors of Audio and GUI, and after call functions of classes to get references of instances of audio and GUI and save this pointers inside of applet class. To be shure that classes have a pointer i read out saved pointer in applet. (Pointer default is null bevore instances of audio an GUI are created.) So applet class can manage connections between Adio and GUI classes (instances). And to connect this different classes i use references of instances. Other reason for that: I must synchronize events of audio with events of GUI (e.g. change tooltip text of buttons if audio change). It is for me not possbile to put all classes into same container class. In Audio and GUI classes i use threads in own content of class. Mixing of ressources (like threads) of classes not possible. And: If create of Audio instance and or GUI instance failed, applet ends. This i check inside of applet class. P.S. Sorry for my english. I come from germany.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups