0

Edit: I have solved the underlying problem. I used SwingUtilities.invokeLater() to solve the issue. My other question provides more information for those who are interested.

I have a GUI that displays an image on a JPanel in paintComponent() with g.drawImage(). I wrote a subclass of JPanel called CanvasPanelView to override paintComponent() and do a few other things, like set the bounds of where the image is drawn. The problem is that I need to get the JPanel's width and height and when I call this.getWidth() and this.getHeight() in the class that extends JPanel, they both return 0.

The process starts in an action listener inner class:

class MenuBarFileOpenListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        File fileChooserReturnValue = view.showAndGetValueOfFileChooser();

        if (fileChooserReturnValue != null) {
            try {
                DocumentModel newDocument = new DocumentModel(ImageIO.read(fileChooserReturnValue), fileChooserReturnValue.getAbsolutePath(), fileChooserReturnValue.getName());
                model.addDocument(newDocument);
                view.addDocument(newDocument);
            } catch(IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Then, addDocument() is called:

public void addDocument(DocumentModel document) {
    menuBar_file_close.setEnabled(true);

    DocumentView newDocumentView = new DocumentView(document.getTitle(), documentsTabbedPaneCloseButtonListener);

    documentViews.add(newDocumentView); // add newDocumentView to ArrayList<DocumentView>
    newDocumentView.setDocument(document);
    documentsTabbedPane.add(newDocumentView.getCanvasPanelView());

    int newDocumentIndex = documentsTabbedPane.indexOfComponent(newDocumentView.getCanvasPanelView());

    documentsTabbedPane.setTabComponentAt(newDocumentIndex, newDocumentView.getTabPanel());
    documentsTabbedPane.setSelectedIndex(newDocumentIndex);
    newDocumentView.setBounds(document.getImageWidth(), document.getImageHeight());
}

public DocumentView(String title, ActionListener listener) {
    canvas = new CanvasPanelView();
    // more code...
}

setBounds() is called:

public void setBounds(int imageWidth, int imageHeight) {
    sourceX1 = 0;
    sourceY1 = 0;
    sourceX2 = imageWidth;
    sourceY2 = imageHeight;

    // some math...

    destinationX1 = 0 + xMargin;
    destinationY1 = 0 + yMargin;
    destinationX2 = drawWidth - xMargin;
    destinationY2 = drawHeight - yMargin;
}

DocumentView is a wrapper class for CanvasPanel and a few other things - it just groups together things that go with each open document.

Everything seems to be instantiated and used or added to the JTabbedPane, so I don't know why this.getWidth() and this.getHeight() return 0. Maybe something is happening between the end of setBounds() and paintComponent().

Why do this.getWidth() and this.getHeight() return 0?

Community
  • 1
  • 1
joshreesjones
  • 1,934
  • 5
  • 24
  • 42
  • `getWith()` returns 0 because the component at that time *is* zero sized, but why, I'm not sure. I don't think that you've asked an answerable question yet as you may have left out too much that would be necessary for knowing why you're seeing your error as well as left in a lot of code unrelated to the problem. If you don't get a great answer soon, consider creating and posting an [sscce](http://sscce.org). – Hovercraft Full Of Eels May 11 '13 at 14:24
  • 1
    Pertinent things to mention and show with your sscce include the layout of the container that holds your image displaying component and how you add the component to the container. – Hovercraft Full Of Eels May 11 '13 at 14:31
  • agreed, right, then answer could be simple – mKorbel May 11 '13 at 15:00
  • I must assume that the two answers that you've received have adequately answered your question. Best of luck. – Hovercraft Full Of Eels May 11 '13 at 15:19
  • In your [sscce](http://sscce.org/), access posted images via `URL`, as shown [here](http://stackoverflow.com/a/10862262/230513), or use synthetic images as shown [here](http://stackoverflow.com/a/15982915/230513). – trashgod May 11 '13 at 16:52

2 Answers2

1

Instead of doing a 'setBounds', why don't you put your documentView inside a Panel(BorderLayout), at a position such as BorderLayout.CENTER?

Alternatively, you can set the minimum and preferred sizes of your documentview based on the image dimensions.

rimero
  • 2,383
  • 1
  • 14
  • 8
0

You have written a class, CanvasPanelView. This class extends JPanel. It appears, that in your implementation of setBounds you make no reference to the members of JPanel that getHeight() and getWidth() refer to. Hence, the underlying object is of 0 height, and 0 width.

If you wish to use the JPanel values for height and width, then you simply need to ensure that you are assigning values to them.

christopher
  • 26,815
  • 5
  • 55
  • 89