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.