How to check how many characters will fit in a title in a `JFrame`?

こ雲淡風輕ζ 提交于 2021-01-29 06:11:22

问题


I have a JFrame as a top-level window of a Swing application used for editing files. I want to display a filename as editors usually do. My current code is a simple:

    frame.setTitle(appTitle + " - " + filePath)

Including a full path is desired, as I often open the file with the same filename from different directories. Sometimes however, when the window is narrow and the path long, the path will not fit into the space available and it is shown shortened, ending with ellipsis:

In such situation I would like to shorten the title myself. How can I check if the title I have provided is too long and would be shortened, so that I can shorten it myself instead, recursively if necessary, and dropping the information I consider least important (which is not the title end, containing the filename)?

来源:https://stackoverflow.com/questions/65595184/how-to-check-how-many-characters-will-fit-in-a-title-in-a-jframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!