2

im wondering how a jcomponent gets painted on the screen, is it painted inside paintComponent() from Graphics? Or is it painted separately. Im asking this because its weird how a jbutton changes color on mousehover even though repaint() is never called.

Thanks for your time.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ewen
  • 1,008
  • 2
  • 16
  • 24
  • 1) *"im wondering how a jcomponent gets.."* I'm wondering if your keyboard has a shift key. If so, please use it at the start of sentences, and where appropriate for things like `JComponent`. It makes the text easier to read. 2) It is a good idea to add the Swing tag to Swing related questions. – Andrew Thompson Aug 25 '12 at 01:53
  • But `repaint()` certainly *is* called, although maybe not in the way you're imagining it. It may be the JButton's delegate that has repaint called on it. Please read [Painting in AWT and Swing](http://java.sun.com/products/jfc/tsc/articles/painting/index.html) for some of the details on painting mechanics. – Hovercraft Full Of Eels Aug 25 '12 at 01:56
  • 1
    Sorry Im on kindle fire so its hard for me to do it, but ill try my best. – Ewen Aug 25 '12 at 01:56
  • So it's called separately from paintComponent()? – Ewen Aug 25 '12 at 01:58

3 Answers3

6

Components are painted with their paint method. repaint is just a useful method that will call paint at some point in the near future on the Event Dispatch Thread.


When the mouse enters a JButton, the following method is called (for JButtons with a default UI):

public void mouseEntered(MouseEvent e) {
    AbstractButton b = (AbstractButton) e.getSource();
    ButtonModel model = b.getModel();
    if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e)) {
        model.setRollover(true);
    }
    if (model.isPressed())
            model.setArmed(true);
}

ButtonModel.setRollover will fire a ChangeEvent, which is handled by AbstractButton in the following way:

public void stateChanged(ChangeEvent e) {
    Object source = e.getSource();

    updateMnemonicProperties();
    if (isEnabled() != model.isEnabled()) {
        setEnabled(model.isEnabled());
    }
    fireStateChanged();
    repaint();
}

So repaint is called when the mouse enters a JButton.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • 1
    Ok does the repaint() only apply for the JButton itself or all other JComponents and paintComponent()? – Ewen Aug 25 '12 at 02:07
  • @Ewen `repaint` is a method of all `Component`s. `repaint` will call `paint`, which will in turn call `paintComponent` among other painting methods. – Jeffrey Aug 25 '12 at 02:10
  • Thanks, i believe this is the best answer. – Ewen Aug 25 '12 at 02:17
5

..a jbutton changes color on mousehover even though repaint() is never called.

Sure it is. And this code is evidence of that. Of course, it is not evidence on a Kindle Fire that most probably has no JRE, but then, a Kindle Fire is an entirely inappropriate tool to be using to communicate to a Q&A site while discussing technical points of a programming language that does not run on the device.

import javax.swing.*;

public class ButtonRepaint {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override 
            public void run() {
                JButton b = new JButton("Hover Over Me!") {
                    @Override
                    public void repaint() {
                        super.repaint();
                        System.out.println("Repaint");
                    }
                };
                JOptionPane.showMessageDialog(null, b);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • +1, discussing technical points on a mobile device is generally not the best idea. – Jeffrey Aug 25 '12 at 02:05
  • @Hover I was responding to the exact statement *"a jbutton changes color on mousehover even though repaint() is never called."* admittedly I read 'mousehover' as 'mouse over' since the Kindle typing was making my eyes water. Is there some specific inclusion of `paintComponent(Graphics)` I should consider? BTW - liked your comment as to why this type of question should not be asked from a Kindle. This series of questions seems to be tying up a lot of our time. – Andrew Thompson Aug 25 '12 at 02:10
  • Im sorry but i cant use my computer and this is the only way. – Ewen Aug 25 '12 at 02:15
  • *"Ok what is the code for?"* Try to figure it out when you get to a desktop that has a decent keyboard and a JDK. I (and a plethora of others) are wasting too much time on this. *"i cant use my computer"* Not my problem, and not a sufficient excuse for wasting the time of others. – Andrew Thompson Aug 25 '12 at 02:17
3

Note that the paint() method that gets called belongs to the button's UI delegate, typically derived from BasicButtonUI. There's an example here using MetalButtonUI.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045