• Advertisement
Sign in to follow this  

JLabel with pack not working

This topic is 3739 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();
    add(panel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    pack();

  }

  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"));
      add(createButton("Sharpen"));
      add(createButton("Soften"));
      add(createButton("Open..."));
      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);
              add(label);             
              System.out.println(file.getName());
              pack();

            } catch (Exception err) {
              System.err.println(err);
            }

          }

          
        }
        if (the_object.equals("Edge Detect")) {
          final EdgeDetectFilter filter = new EdgeDetectFilter();
          filter.filter(my_pixel_image);
        }
        if (the_object.equals("Edge Highlight")) {
          final EdgeHighlightFilter filter = new EdgeHighlightFilter();
          filter.filter(my_pixel_image);
        }
        if (the_object.equals("Flip Horizontal")) {
          final FlipHorizontalFilter filter = new FlipHorizontalFilter();
          filter.filter(my_pixel_image);
        }
        if (the_object.equals("Flip Vertical")) {
          final FlipVerticalFilter filter = new FlipVerticalFilter();
          filter.filter(my_pixel_image);
        }
        if (the_object.equals("Gray Scale")) {
          final GrayscaleFilter filter = new GrayscaleFilter();
          filter.filter(my_pixel_image);
        }
        if (the_object.equals("Sharpen")) {
          final SharpenFilter filter = new SharpenFilter();
          filter.filter(my_pixel_image);
        }
        if (the_object.equals("Soften")) {
          final SoftenFilter filter = new SoftenFilter();
          filter.filter(my_pixel_image);
        }
      }
    }
    button.addActionListener(new MyActionListener());
    frame.pack();
    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
Advertisement
Sign in to follow this  

  • Advertisement