Java JLabel getTimeWidth(JLabel label)

Here you can find the source of getTimeWidth(JLabel label)

Description

get Time Width

License

Apache License

Declaration

public static int getTimeWidth(JLabel label) 

Method Source Code

//package com.java2s;
/**/*  w ww  .  ja  v  a  2s  . c o m*/
 * Copyright (C) 2008 Atlassian
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.JLabel;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Locale;

public class Main {
    private static final int FALLBACK_DATE_WIDTH = 180;
    private static int dateWidth = -1;

    public static int getTimeWidth(JLabel label) {
        if (dateWidth == -1) {
            try {
                DateFormat dfi = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US);
                String t = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
                        .format(dfi.parse("22 Dec 2008 11:22:22"));
                final JLabel jLabel = new JLabel(t);
                jLabel.setFont(label.getFont());
                dateWidth = jLabel.getPreferredSize().width;
            } catch (ParseException e) {
                dateWidth = FALLBACK_DATE_WIDTH;
            }
        }
        return dateWidth;
    }
}

Related

  1. findMaxLabelWidth(JLabel... jLabels)
  2. getLabelValue(JLabel lbl)
  3. getRoughStroke(final JLabel _jlbl_stroke)
  4. getStoredBackground(JLabel comp)
  5. getStroke(final JLabel _jlbl_stroke)
  6. highlightLabel(JLabel label)
  7. leftAlignJLabel(JLabel label)
  8. parseIntFromLabel(JLabel jLabel)
  9. setBoundsForJLabel(JLabel label, int xOffset, int yOffset)