Results 1 to 3 of 3
  1. #1
    lmsook10 is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default How can I set the frame's size flexible to show it's title(variable length) in full?

    Hello,

    I have a frame that has a table in it.

    This frame has a title, something like,

    "Find results for test.html - 3 item(s)"
    And in the table, it shows 3 results.

    My problem is that I don't know how long the frame's title will be. Sometimes it will be short like "Find results for test.html - 3 item(s)"
    and sometimes it will be long like, "Find results for asdfdfdfdfdfsdfdfdfdf.html - 3 item(s)"

    Currently, what I do is

    Java Code:
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    It shows frame's title full in case of "Find results for test.html - 3 item(s)"
    but it doesn't show frame's title full for "Find results for asdfdfdfdfdfsdfdfdfdf.html - 3 item(s)"

    How can I set the frame's size flexible to show it's title in full?

    Thanks,

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Is the problem not knowing how wide in pixels the title is so that you can make the frame big enough to show ALL of the title?
    I think the class FontMetrics has methods to get the width of a String.

  3. #3
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    You need to call a method after setting the title of the Frame. The one I supplied should work. However I think the user should decide if they want to resize the frame.

    Java Code:
    	private static void fitFrameTitle(JFrame frame) {		
    		FontUIResource font = (FontUIResource) UIManager.get("InternalFrame.titleFont");
    		if (font != null) {
    			int prefferedWidth = frame.getWidth();
    			int prefferedHeight = frame.getHeight();
    			FontMetrics fm = frame.getFontMetrics(font);
    			Rectangle2D bnds = fm.getStringBounds(frame.getTitle(), frame.getGraphics());
    			prefferedWidth = (int) bnds.getWidth() + 50;//50 is space for the maximise and minimise buttons might need to increase this
    			Dimension dimension = new Dimension(prefferedWidth, prefferedHeight);
    			frame.setPreferredSize(dimension);
    			frame.setMinimumSize(dimension);
    			frame.pack();
    		}		
    	}
    HTH
    Stephen

Similar Threads

  1. Demonstration of the variable length arguments
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:38 PM
  2. Replies: 0
    Last Post: 04-07-2008, 08:14 PM
  3. bean with flexible version ability
    By nineball in forum Advanced Java
    Replies: 0
    Last Post: 03-26-2008, 09:59 AM
  4. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM
  5. Full screen
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 05:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •