Note that there are some explanatory texts on larger screens.

plurals
  1. POSwing application not displaying certain buttons
    primarykey
    data
    text
    <p>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: </p> <pre><code>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) { } } </code></pre> <p>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.</p> <pre><code>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&lt;ColorButton&gt; colorButtons; private void createColorButtons() // Create and add all color buttons { colorButtons = new ArrayList&lt;ColorButton&gt;(); 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 &amp; Tools")); for (ColorButton button : colorButtons) { colorButtonPanel.add(button); } // Add Panels add(BorderLayout.SOUTH, colorButtonPanel); } } </code></pre> <p>So, as you can see, the class inherits from JFrame, and all the components are added to the frame in <code>addComponents()</code>. The method that I believe the problem lies in is in <code>createColorButtons()</code>. 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 <code>colorButtons.add(foo)</code> 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.</p> <p>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.</p> <p>Added Main Method for added measures:</p> <pre><code>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 &amp; feel { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch ( UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } new PaintFrame(); } } </code></pre> <p>EDIT: Yes, all of the images are in the correct folder and are named correctly. No worries there.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload