The following program code is a boiled down version of my application. The changes to the background color in lines 45, 47,48 are not shown though the print command show that they have been effected.
As you see, I have tried several repaint, pack etc commands without success. What am I missing?
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ButtonColor extends JFrame implements ActionListener {
public int Lifecols, Liferows;
private JPanel zellPanel,FertigPanel;
private JButton ChosenB;
public JButton[][] zellen;
ButtonColor(){
this.setLocationByPlatform(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setCellPanel(10,10);
FertigPanel = new JPanel();
ChosenB = new JButton("Auswahl bestätigen");
ChosenB.setFont(new Font("Serif", Font.BOLD, 20));
ChosenB.setPreferredSize(new Dimension(600, 50));
ChosenB.addActionListener(this);
FertigPanel.add(ChosenB);
getContentPane().add(zellPanel);
add(Box.createRigidArea(new Dimension(0,20)));
getContentPane().add(FertigPanel);
add(Box.createRigidArea(new Dimension(0,10)));
pack();
repaint();
setVisible ( true);}
public void setCellPanel(int Lifecols,int Liferows){
JButton zelle;
zellPanel = new JPanel();
zellPanel.setLayout(new GridLayout(Liferows, Lifecols));
zellPanel.setPreferredSize(new Dimension(1100, 1100));
zellen = new JButton[Liferows][Lifecols];
for (int rowIndex=0; rowIndex< Liferows; rowIndex++)
{ for (int colIndex =0; colIndex < Lifecols; colIndex++)
{zellen[rowIndex][colIndex] = new JButton();
zellen[rowIndex][colIndex].setBackground(Color.WHITE);
zellPanel.add(zellen[rowIndex][colIndex]);
}}
return;}
public void SetBlinker1(){
System.out.println(zellen[3][3].getBackground());
zellen[3][3].setBackground(Color.BLACK);
System.out.println(zellen[3][3].getBackground());
zellen[3][4].setBackground(Color.BLACK);
zellen[3][5].setBackground(Color.BLACK);
zellen[3][3].repaint();
zellPanel.repaint();
getContentPane().repaint();
pack();
return;}
public void actionPerformed(ActionEvent e){
if (e.getSource() == ChosenB) {
setCellPanel(10,10);
SetBlinker1();
pack();
revalidate();
repaint();
return;
}}
public static void main(String[] args) {
new ButtonColor();
}}
On most platforms, JButton
renders a platform specific style button. This tends to ignore the background
property.
I would suggest making sure that the button is opaque and change the border used by the button...
zellen[rowIndex][colIndex].setOpaque(true);
zellen[rowIndex][colIndex].setBorder(new LineBorder(Color.BLACK));
This will, in most cases, ensure that you've overridden the "platform stylings".
nb: I resized the frame to make the image smaller
You're next problem is, you're calling setCellPanel(10, 10);
in your actionPerformed
method. This is causing all sorts of issues. If you MUST do this, first remove all the pre-existing buttons from the zellPanel
, but, I'd just not call it at all...
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ChosenB) {
SetBlinker1();
}
}
I would also recommend taking a look at Code Conventions for the Java Programming Language. It will make it easier for you to read/understand other peoples code and for other's to read/understand yours.