/** * MIT License * Copyright (c) 2017 Apollo Zhu (朱智语). *

* Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package io.github.apollozhu.swing; import io.github.apollozhu.util.AZListenable; import javax.swing.*; import javax.swing.event.EventListenerList; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * A subclass of JLabel with {@link #setText(String)} working with \n. * * @author Apollo Zhu * * @version 1.0 */ @SuppressWarnings("serial") public class AZJLabel extends JLabel implements AZListenable { /** * Creates a JLabel instance with the specified * text, image, and horizontal alignment. * The label is centered vertically in its display area. * The text is on the trailing edge of the image. * * @param text The text to be displayed by the label. * @param icon The image to be displayed by the label. * @param horizontalAlignment One of the following constants * defined in SwingConstants: * LEFT, * CENTER, * RIGHT, * LEADING or * TRAILING. */ public AZJLabel(String text, Icon icon, int horizontalAlignment) { super(text, icon, horizontalAlignment); } /** * Creates a JLabel instance with the specified * text and horizontal alignment. * The label is centered vertically in its display area. * * @param text The text to be displayed by the label. * @param horizontalAlignment One of the following constants * defined in SwingConstants: * LEFT, * CENTER, * RIGHT, * LEADING or * TRAILING. */ public AZJLabel(String text, int horizontalAlignment) { super(text, horizontalAlignment); } /** * Creates a JLabel instance with the specified text. * The label is aligned against the leading edge of its display area, * and centered vertically. * * @param text The text to be displayed by the label. */ public AZJLabel(String text) { super(text); } /** * Creates a JLabel instance with the specified * image and horizontal alignment. * The label is centered vertically in its display area. * * @param image The image to be displayed by the label. * @param horizontalAlignment One of the following constants * defined in SwingConstants: * LEFT, * CENTER, * RIGHT, * LEADING or * TRAILING. */ public AZJLabel(Icon image, int horizontalAlignment) { super(image, horizontalAlignment); } /** * Creates a JLabel instance with the specified image. * The label is centered vertically and horizontally * in its display area. * * @param image The image to be displayed by the label. */ public AZJLabel(Icon image) { super(image); } /** * Creates a JLabel instance with * no image and with an empty string for the title. * The label is centered vertically * in its display area. * The label's contents, once set, will be displayed on the leading edge * of the label's display area. */ public AZJLabel() { super(); } /** * Defines the single line of text this component will display. If the value * of text is null or empty string, nothing is displayed. If the text * contains \n, it is converted to
* tag, and the whole text is wrapped around by \ tag. Then fires an * action event after text changed. *

* The default value of this property is null. *

* This is a JavaBeans bound property. * * @see #setVerticalTextPosition * @see #setHorizontalTextPosition * @see #setIcon * @beaninfo * preferred: true * bound: true * attribute: visualUpdate true * description: Defines the single line of text this component will display. */ @Override public void setText(String text) { if (text != null && !text.startsWith("") && !text.endsWith("") && text.contains("\n")) text = "" + text.replaceAll("\n", "
") + ""; super.setText(text); forEachListener(listener -> listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "setText"))); } private EventListenerList listenerList = new EventListenerList(); @Override public EventListenerList getListenerList() { return listenerList; } }