Jump to content
  • Advertisement
Sign in to follow this  

JLabel with pack not working

This topic is 4013 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hell, I am trying to make a program that adds buttons, then u use a JFileChooser to open a file and add it as a JLabel to the JFrame and then use the pack so that it resizes the screen to add it. However with my code it resizes to just the buttons above the picture width and the picture, and I must resize it to make all buttons show. I have been told that it is packing the JLabel, but I thought it would re-pack the JFrame since it is all added to it. Can any one please help me. Also if any one knows a better way to create my buttons please assist.
public class SnapShopGUI extends JFrame {

  private PixelImage my_pixel_image = null;
  private JFrame frame;

  public SnapShopGUI() {
    frame = new JFrame();


  public void start() {
    setTitle("TCSS 305 SnapShop");
    final ButtonPanel panel = new ButtonPanel();



  private class ButtonPanel extends JPanel {
    // constructor
    public ButtonPanel() {

      add(createButton("Edge Detect"));
      add(createButton("Edge Highlight"));
      add(createButton("Flip Horizontal"));
      add(createButton("Flip Vertical"));
      add(createButton("Gray Scale"));
      final JButton saveAs = new JButton("Save As...");



  public JButton createButton(final Object the_object) {

    final JButton button = new JButton(the_object.toString());

    class MyActionListener implements ActionListener {

      public void actionPerformed(final ActionEvent the_event) {
        if (the_object.equals("Open...")) {

          final JFileChooser chooser = new JFileChooser();
          final int result = chooser.showOpenDialog(null);
          if (result == JFileChooser.APPROVE_OPTION) {

            final File file = chooser.getSelectedFile();

            try {
              my_pixel_image = PixelImage.load(file);
              final JLabel label = new JLabel(new ImageIcon(my_pixel_image), JLabel.CENTER);
              // label.setOpaque(false);

            } catch (Exception err) {


        if (the_object.equals("Edge Detect")) {
          final EdgeDetectFilter filter = new EdgeDetectFilter();
        if (the_object.equals("Edge Highlight")) {
          final EdgeHighlightFilter filter = new EdgeHighlightFilter();
        if (the_object.equals("Flip Horizontal")) {
          final FlipHorizontalFilter filter = new FlipHorizontalFilter();
        if (the_object.equals("Flip Vertical")) {
          final FlipVerticalFilter filter = new FlipVerticalFilter();
        if (the_object.equals("Gray Scale")) {
          final GrayscaleFilter filter = new GrayscaleFilter();
        if (the_object.equals("Sharpen")) {
          final SharpenFilter filter = new SharpenFilter();
        if (the_object.equals("Soften")) {
          final SoftenFilter filter = new SoftenFilter();
    button.addActionListener(new MyActionListener());
    return button;
Also in the main which is a different file it does final SnapShopGUI gui = new SnapShopGUI(); gui.start();

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!