Saturday, 24 August 2013

Swing application not displaying certain buttons

Swing application not displaying certain buttons

I've been rebuilding an old MS-paint copy cat that I did a few months ago,
and Swing has once again been giving me some old problems. One of which
has had me and a few other people stumped for a few days now. I've got a
custom JFrame that I'm using to lay all of my components on, and custom
JButtons that are used to represent the various colors and tools that the
user can choose from. Right now, the problem is that my program isn't
displaying most of my buttons. Here's my ColorButton class:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class ColorButton extends JButton implements ActionListener
{
private static final long serialVersionUID = 1L;
Color color;
public ColorButton(Color color, String name)
{
this.color = color;
setButtonIcon(name);
}
private void setButtonIcon(String name)
{
ImageIcon icon = new ImageIcon("images/" + name);
setIcon(icon);
System.out.println("Icon set.");
}
@Override
public void actionPerformed(ActionEvent event)
{
}
}
Basically, this class is just a better button that I could reuse and
dynamically place onto the main frame. I have it set up so that it takes a
Color (to set the user's cursor color) and a string (in order to get the
ImageIcon from the resources folder). Here's the JFrame that I have to add
everything to.
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class PaintFrame extends JFrame // Frame to place all items on
{
private static final long serialVersionUID = 1L;
public PaintFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the program
to close if the user wishes
setVisible(true); // Set screen to visible
setSize(1150, 650); // Set screen size
setLocationRelativeTo(null); // Set frame to start in middle of
screen
setResizable(false);
setLayout(new BorderLayout()); // Set a suitable layout for the panel
setTitle("Paint 2.0");
addComponents(); // Add everything to the JFrame
}
// List to hold all of the color buttons
ArrayList<ColorButton> colorButtons;
private void createColorButtons() // Create and add all color buttons
{
colorButtons = new ArrayList<ColorButton>();
colorButtons.add(new ColorButton(Color.BLACK, "black_paint.png"));
colorButtons.add(new ColorButton(Color.WHITE, "white_paint.png"));
// colorButtons.add(new ColorButton(new Color(22, 10, 255),
"blue_paint.png"));
// colorButtons.add(new ColorButton(new Color(163, 92, 45),
"brown_paint.png"));
// colorButtons.add(new ColorButton(new Color(19, 175, 50),
"dark_green_paint.png"));
// colorButtons.add(new ColorButton(new Color(22, 255, 34),
"green_paint.png"));
// colorButtons.add(new ColorButton(new Color(58, 209, 255),
"light_blue_paint.png"));
// colorButtons.add(new ColorButton(new Color(255, 84, 33),
"orange_paint.png"));
// colorButtons.add(new ColorButton(new Color(255, 86, 243),
"pink_paint.png"));
// colorButtons.add(new ColorButton(new Color(168, 11, 121),
"purple_paint.png"));
// colorButtons.add(new ColorButton(new Color(255, 0, 0),
"red_paint.png"));
// colorButtons.add(new ColorButton(new Color(255, 241, 45),
"yellow_paint.png"));
colorButtons.add(new ColorButton(Color.WHITE, "eraser.png"));
}
JPanel colorButtonPanel;
private void addComponents() // Add all the components to the screen
{
createColorButtons();
colorButtonPanel = new JPanel();
colorButtonPanel.setLayout(new BoxLayout(colorButtonPanel,
BoxLayout.X_AXIS));
colorButtonPanel.setBorder(new TitledBorder("Colors & Tools"));
for (ColorButton button : colorButtons)
{
colorButtonPanel.add(button);
}
// Add Panels
add(BorderLayout.SOUTH, colorButtonPanel);
}
}
So, as you can see, the class inherits from JFrame, and all the components
are added to the frame in addComponents(). The method that I believe the
problem lies in is in createColorButtons(). Right now, all the buttons
that aren't displaying are commented out and the other ones are the only
buttons that work. The problem here is very specific. If I start the
program with any button implemented that doesn't work (ie. any
colorButtons.add(foo) that is commented out), then the frame comes up
completely empty. None of the buttons appear, just a blank frame. However,
if I start the program with all of those buttons commented out, then I can
still get the three buttons.
The only thing that is different from these buttons versus the others is
that the buttons that won't display use custom created colors, while
others use pre-set colors included in the Java API. I can't think of any
reason why that should cause any problem, but let me know if you think
otherwise. Also let me know if you need any more details, code, or
anything else that you can think of that might help you answer the
question. Thanks.
Added Main Method for added measures:
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Start // Start the program
{
public static void main(String[] args)
{
try // Get default system look & feel
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (
UnsupportedLookAndFeelException |
ClassNotFoundException |
InstantiationException |
IllegalAccessException e)
{
e.printStackTrace();
}
new PaintFrame();
}
}

No comments:

Post a Comment