-1

So, there's a few other posts on Stackoverflow like Calculate the display width of a string in Java and How to calculate length of string in pixels for specific font and size? but the first one only calculates the width of the string from some pre-determined font and size, and the latter one is in Python.

Basically my goal is I want to make an application where there's a username, and the JLabel on it is exactly the length of the text. Since I don't know what the username is while designing the program, I can't know how far it is, but I want to know how to calculate that width of text with my font and size specified (and then I already know how to adjust the size of a JLabel).

Any ideas? Thanks!

camickr
  • 321,443
  • 19
  • 166
  • 288
tdog
  • 169
  • 2
  • 11
  • 1
    `when I place a JLabel and then edit the text, the JLabel does not stretch` - Post your [mcve] demonstrating the problem. So what you need to do is have 1) a JFrame 2) a JLabel with a default value 3) a JButton, that when clicked with display a JOptionPane to prompt for the user name. Then when you get the user name you update the label. The whole example will be about 20 -30 lines of code. – camickr Sep 03 '18 at 21:25

1 Answers1

1

Don't try to calculate the size of the label!!!

and the JLabel on it is exactly the length of the text.

This is exactly the default behaviour of how a JLabel works.

Each Swing component is responsible for determining its own preferred size based on the properties of the component.

In the case of a JLabel it will look at the font, text border etc. to determine it's preferred size.

Then you just add the label to the frame using an appropriate layout manager and the label will be displayed at its preferred size.

Read the Swing tutorial on Layout Managers so you can use the appropriate layout manager to display your label at its preferred size.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • No, when I place a JLabel and then edit the text, the JLabel does not stretch. – tdog Sep 03 '18 at 21:21
  • Yes the label will automatically resize when the test is changed if you are using and appropriate layout manager. Don't ever attempt to set the size and location yourself. – camickr Sep 03 '18 at 21:22
  • I'm using a drag and drop absolute layout, not a regular one. Behavior is different in those. – tdog Sep 03 '18 at 21:24
  • 2
    @tdog, Don't use drag and drop with an AbsoluteLayout. I have already told you twice it will work when you use the appropriate layout manager. And I started my answer be telling your NOT to calculate the size manually. – camickr Sep 03 '18 at 21:26
  • @camickr Drag and drop is by default in AbsoluteLayout. You're crazy – tdog Sep 03 '18 at 21:27
  • @Strelok, I did give a code example. The tutorial is loaded with working examples. – camickr Sep 03 '18 at 21:27
  • 2
    @tdog, 1) I never said you have to use the IDE to generate your GUI code. I never use an IDE for that because you spend time learning the IDE and not Swing. 2) if that is the default then change it or use a different IDE. I can only stress you should NOT be using an AbsouteLayout. You spend more time trying to solve unnecessary problems like this. Swing was designed to use dynamic forms that resize automatically as the frame size changes or the data changes. You are trying to reinvent the wheel by doing this yourself when there is no need to so. – camickr Sep 03 '18 at 21:32
  • @camickr I could care less about which IDE I'm using at this current moment, you've published four comments and one answer all with this one belief that "oh you don't have to". Well in my instance I do have to, so answer that. – tdog Sep 03 '18 at 21:34
  • 1
    I have already answered your question. In addition I gave you a better solution so you don't have to worry about this. – camickr Sep 03 '18 at 21:46
  • 2
    To expand on what @camickr has been saying: Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). As an aside: None of the [top Swing programmers on SO](https://stackoverflow.com/tags/swing/topusers) would tell you different. It's just the right approach. – Andrew Thompson Sep 03 '18 at 22:09
  • @tdog *"No, when I place a JLabel and then edit the text, the JLabel does not stretch"* - Really, because you question doesn't contain any example code which demonstrates this issue, so it's impossible for use to provide you any additional assistance. *"Drag and drop is by default in AbsoluteLayout."* Then change it. *"You're crazy"* You're the one with the problem ask the questioning - provide a better baseline which demonstrates your current approach and problem so we can provide you with better assistance, the answer you get will only be as good as the question you ask – MadProgrammer Sep 03 '18 at 22:29
  • @Strelok I think a code example would enhance the question, everything else is guess work – MadProgrammer Sep 04 '18 at 04:35