JButton() only working when mouse hovers
Asked Answered
O

4

3
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.lang.*;
    import java.io.*;
    import javax.swing.*;
    public class MainClass extends Component{
       private Image bg;
       private ImageIcon newgame;
       private ImageIcon quit;
       private ImageIcon options;
       private JButton bquit;
       private JButton boptions;
       private JButton bnewgame;
       private static Container pane; //Container

    public void loadImage() {
        try {
            bg=ImageIO.read(new File("bg1.png"));
        } catch (Exception e) {
        }
        if(bg!=null)
            repaint();

    }
    public void paint(Graphics g) {
        g.drawImage(bg,0,0,null);
    }
    public void imageButtons(JFrame f) {
        try {
            quit= new ImageIcon("quit.png");
            options=new ImageIcon("options.png");
            newgame= new ImageIcon("newgame.png");
        }catch(Exception e){}    
        bnewgame= new JButton(newgame);
        boptions= new JButton(options);
        bquit= new JButton(quit);
        bnewgame.setBounds(150,100,400,89);
        boptions.setBounds(150,200,400,89);
        bquit.setBounds(150,300,400,89);
        pane.add(bquit);
        pane.add(boptions);
        pane.add(bnewgame);
    }   


    public static void main(String args[]) {

        MainClass o=new MainClass();    
        FullScreen fs=new FullScreen(); 
        JFrame f1=new JFrame("TITLE");
        pane=f1.getContentPane();
        fs.fullScreenIt(f1);
        pane.add(o);
        f1.setVisible(true);
        o.loadImage();
        o.imageButtons(f1);
    }
}

The Fullscreen is another class which gives a full screen Frame. JButton have ImageIcon on them. bg1.png is a background image Problem is these JButton become visible only when mouse hovers else they do not appear.

Organist answered 27/10, 2011 at 16:12 Comment(0)
L
5

Likely you have a layout problem where you're trying to add JButtons with absolute bounds into a container that uses a non-null layout manager. Suggestions

  • Do not use setBounds and absolute positioning to size and place your components.
  • Read up on and use the layout managers to do this heavy lifting for you: Lesson: Laying Out Components Within a Container
  • Don't forget to call pack() on your JFrame after adding all components
  • Call setVisible(true) after calling pack() and again call both only after adding all components to your GUI.
  • A null layout is available if you absolutely need to use absolute positioning of components, but regardless, you should strive to avoid using it.
Lucknow answered 27/10, 2011 at 16:16 Comment(4)
Ok one more thing where should i add that background Image? To pane // a Container or To Frame f1?Organist
null layout gives more problems because it makes first Button(which is bnewgame) to draw on whole screen, A full screen button nothing else on screen which is horrible, i tried using Layout managers but they complicate the thing and background Image won't drawOrganist
that's not really part of this question but a separate question. I suggest that you search this forum for adding background images to a Swing GUI and then come back if you're still stuck. Regarding your second statement, it only means that you're using them wrong, but doesn't mean that you shouldn't use them.Lucknow
Ok thanks for helping but problem was not of Layout manager, i solved it.Organist
S
7

add Icon/ImageIcon directly to the JButton instead of paint() for AWT or paintComponent() for Swing JComponents

Contructor JButton(Icon) knows Icon or ImageIcon

enter image description here

from code

import java.awt.*;
import javax.swing.*;

public class ButtonsIcon extends JFrame {

    private static final long serialVersionUID = 1L;
    private ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon =  UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon =  UIManager.getIcon("OptionPane.warningIcon");

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ButtonsIcon t = new ButtonsIcon();
            }
        });
    }

    public ButtonsIcon() {
        setLayout(new GridLayout(0, 2, 4, 4));

        JButton button = new JButton();
        button.setBorderPainted(false);
        button.setBorder(null);
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setIcon((errorIcon));
        button.setRolloverIcon((infoIcon));
        button.setPressedIcon(warnIcon);
        button.setDisabledIcon(warnIcon);
        add(button);

        JButton button1 = new JButton();
        button1.setBorderPainted(false);
        button1.setBorder(null);
        button1.setFocusable(false);
        button1.setMargin(new Insets(0, 0, 0, 0));
        button1.setContentAreaFilled(false);
        button1.setIcon((errorIcon));
        button1.setRolloverIcon((infoIcon));
        button1.setPressedIcon(warnIcon);
        button1.setDisabledIcon(warnIcon);
        add(button1);
        button1.setEnabled(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}
Spherical answered 27/10, 2011 at 16:36 Comment(4)
i used paint for drawing that background Image bg1.pngOrganist
@Harvinder Singh Sidhu you have two correct answer (+1) to your question, then why post another suggestion to two correct answersSpherical
@Spherical : This is wonderful, never tried border and content area thingy ever. You are always there to teach something new :-) Thankyou.Contrabass
glad to see you here again :-)Spherical
L
5

Likely you have a layout problem where you're trying to add JButtons with absolute bounds into a container that uses a non-null layout manager. Suggestions

  • Do not use setBounds and absolute positioning to size and place your components.
  • Read up on and use the layout managers to do this heavy lifting for you: Lesson: Laying Out Components Within a Container
  • Don't forget to call pack() on your JFrame after adding all components
  • Call setVisible(true) after calling pack() and again call both only after adding all components to your GUI.
  • A null layout is available if you absolutely need to use absolute positioning of components, but regardless, you should strive to avoid using it.
Lucknow answered 27/10, 2011 at 16:16 Comment(4)
Ok one more thing where should i add that background Image? To pane // a Container or To Frame f1?Organist
null layout gives more problems because it makes first Button(which is bnewgame) to draw on whole screen, A full screen button nothing else on screen which is horrible, i tried using Layout managers but they complicate the thing and background Image won't drawOrganist
that's not really part of this question but a separate question. I suggest that you search this forum for adding background images to a Swing GUI and then come back if you're still stuck. Regarding your second statement, it only means that you're using them wrong, but doesn't mean that you shouldn't use them.Lucknow
Ok thanks for helping but problem was not of Layout manager, i solved it.Organist
P
3

It looks like you're never repainting after you add your buttons.

I would add a repaint in there after you add them.

Pat answered 27/10, 2011 at 16:16 Comment(0)
K
1

Just had a similar problem...

I believe that the glitch is caused by overriding the paint() method. Default paint() method automatically calls repaint() on all component, but by overriding the paint() method, components stop being repainted. So, the solution is to call repaint() on all components int the overriden paint() method.

Worked for me, hope it'll work for others ;)..

Knot answered 25/5, 2014 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.