I have a situation where the user enters String, and my code makes Jlabel for him and tries to center it on a fullscreen JFrame / Pane. My problem is to be able to accurately center it, I need to know its size.
I need a way to figure out how long (wide) the JLabel should be so that it fits the length of the string and nothing else. I tried using the preferredSize () method that the components have and it looked awful (I passed an empty value so that my user interface would determine the preferred size). I know how to determine the height that works (fontSize + 10px size is usually good), but I can't get the width.
I use absolute positioning (no LayoutManager).
thank
source
share