0

How can i get the Dimension (no matter if prefferedSize or Size) of a JPanel which is build in another class?

in the class where i build the GUI i instanziate a new Panel like this:

PlotPanel specificPlot = gui.BuildPlottingData.makeImagePlot(discDbSpecific, sections,
windowLength, 1, algoParam1, algoParam2, leadWindowSize, 0, errorAcc);

As you can see this panel is to behave like a normal JPanel:

public class PlotPanel extends JPanel {...

I set its preferredSize correctly (it is visualized correctly).

Calling specificPlot.getPreferredSize() gives 'java.awt.Dimension[width=10,height=10]' thats obviously wrong..

Btw: Do you have any idea how to handle this Bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4915023

BoxLayout sometimes cutting off the Panel.. thats the reason why i need to have access to the Size... i tried everything so far... or do you know an alternative Layoutmanager with same resuts like vertical BoxLayout w/o need to manualy reading the Size?

Earlier post reffer to same problem.. Edit: I override the getPreferredSize-method. Problem is that i dont have access to all parameters wich influencing the Size as follows

package getdata;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class Test extends JPanel{

@Override
public Dimension getPreferredSize(){
    return new Dimension(10,20);

}
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);
    Graphics2D paint = (Graphics2D) g;

    paint.drawString("test", 0, 10);
    int strWdt = paint.getFontMetrics().stringWidth("test");
}

}

how to read strWdt? even if instanziate in class it will have initial value.

Jan S
  • 117
  • 3
  • 14

1 Answers1

2

Do you have any idea how to handle this Bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4915023

answer is described in evaluation, and rest is question - (Should I avoid the use of set[Preferred|Maximum|Minimum]Size methods in Java Swing?)

How can i get the Dimension (no matter if prefferedSize or Size) of a JPanel which is build in another class?

  1. JPanel (valid for all JComponents) returns own Dimension in the case that
    • is visible on the screen
    • after JFrame(JDialog, etc.).pack() is called
  2. override getPreferredSize for JPanel
  3. JPanel has FlowLayout implemented in API
  4. BoxLayout accepting (in compare with rest of LayoutManagers) min, max and preferred size
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thank you so far and sorry for missing SSCCE.. its simply a jpanel from another class.. When i override `getPreferredSize()` i get the correct Size. The problem is that in this method i dont have access to all parameters which influence the hight of the Panel.. (setting them in the overritten ´paintComponent()`-method ... – Jan S Jun 09 '13 at 23:15
  • overload those methods, create private voids, constructor – mKorbel Jun 10 '13 at 06:21