I am setting background image for JButton
or say JTableHeader
. When I do paintComponent
on the same, it's removing text value set for that component.
Any idea where I am going wrong?
JButton btn = new JButton(){
@Override
public void paintComponent(Graphics g){
Dimension size = this.getSize();
g.drawImage(Toolkit.getDefaultToolkit().getImage("C:\\User\\Downloads\\MainMenu.jpg"), 0, 0, size.width, size.height, this);
}
};
btn.setText("TEST WITH ME");
btn.setOpaque(true);
I may have not specified that much correct what I really wanted. But I figured out answer for that.
@Override
public void paintComponent(Graphics g){
Dimension size = this.getSize();
g.drawImage(Toolkit.getDefaultToolkit().getImage("C:\\User\\Downloads\\MainMenu.jpg"), 0, 0, size.width, size.height, this);
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth("String Value To Set")) / 2;
int y = ( (getHeight() - fm.getHeight() ) / 2) + fm.getAscent() ;
g.drawString(String Value To Set, x, y);
}