Showing posts with label Swing. Show all posts
Showing posts with label Swing. Show all posts

Swing LinkLabel


It would be very helpful to have a label that can work link HTML A tag. The following child of JLabel have this functionality and the hover color can be customized also.


Download Source


package com.blogspot.codetoearn.linklabel;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.swing.Icon;
import javax.swing.JLabel;

/**
 * http://codetoearn.blogspot.com/
 *
 * @author ehsun7b
 */
public class LinkLabel extends JLabel {

  private String url;
  private Color hoverColor = Color.blue;
  private Color forgroundColor;

  public LinkLabel(String url, String text, Icon icon, int horizontalAlignment) {
    super(text, icon, horizontalAlignment);
    this.url = url;
    init();
  }

  public LinkLabel(String url, String text, int horizontalAlignment) {
    super(text, horizontalAlignment);
    this.url = url;
    init();
  }

  public LinkLabel(String url, String text) {
    super(text);
    this.url = url;
    init();
  }

  public LinkLabel(String url, Icon image, int horizontalAlignment) {
    super(image, horizontalAlignment);
    this.url = url;
    init();
  }

  public LinkLabel(String url, Icon image) {
    super(image);
    this.url = url;
    init();
  }

  public LinkLabel(String url) {
    this.url = url;
    init();
  }

  private void init() {
    addMouseListener(new MouseListener() {
      @Override
      public void mouseClicked(MouseEvent e) {
        openUrl();
      }

      @Override
      public void mousePressed(MouseEvent e) {
      }

      @Override
      public void mouseReleased(MouseEvent e) {
      }

      @Override
      public void mouseEntered(MouseEvent e) {
        if (forgroundColor == null) {
          forgroundColor = getForeground();
        }
        setForeground(hoverColor);
      }

      @Override
      public void mouseExited(MouseEvent e) {
        setForeground(forgroundColor);
      }
    });
    
    setCursor(new Cursor(Cursor.HAND_CURSOR));
  }

  private void openUrl() {
    openURL(url);
  }

  public static void openURL(String url) {
    if (!java.awt.Desktop.isDesktopSupported()) {
      return;
    }

    java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

    if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
      return;
    }

    try {
      java.net.URI uri = new java.net.URI(url);
      desktop.browse(uri);
    } catch (URISyntaxException | IOException e) {
      System.err.println(e.getMessage());
    }
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public Color getHoverColor() {
    return hoverColor;
  }

  public void setHoverColor(Color hoverColor) {
    this.hoverColor = hoverColor;
  }

  public Color getForgroundColor() {
    return forgroundColor;
  }

  public void setForgroundColor(Color forgroundColor) {
    this.forgroundColor = forgroundColor;
  }
}

Swing Fantasy Checkbox with Customized Icons


This customized Checkbox let you have different labels for select or unselected states and you can have different icons also. If you want to have the same label at both states, you can pass the same strings for both to the constructor.


Download Source


package com.blogspot.codetoearn.fantasycheckbox;

import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;

/**
 * http://codetoearn.blogspot.com/
 *
 * @author ehsun7b
 */
public class FantasyCheckbox extends JComponent implements MouseListener {

  private boolean selected;
  private String selectedLabel;
  private String unSelectedLabel;
  private ImageIcon selectedIcon;
  private ImageIcon unSelectedIcon;
  private JLabel lblIcon;
  private JLabel lblText;

  public FantasyCheckbox(String selectedLabel, String unSelectedLabel, 
          ImageIcon selectedIcon, ImageIcon unSelectedIcon) {
    this.selectedLabel = selectedLabel;
    this.unSelectedLabel = unSelectedLabel;
    this.selectedIcon = selectedIcon;
    this.unSelectedIcon = unSelectedIcon;
    
    createComponents();
  }  
  
  private void createComponents() {
    selected = false;    
    lblIcon = new JLabel(unSelectedIcon);
    lblText = new JLabel(unSelectedLabel);
    setLayout(new FlowLayout());
    add(lblIcon);
    add(lblText);
    lblIcon.addMouseListener(this);
    lblText.addMouseListener(this);
  }  

  private void updateLabels() {
    lblText.setText(selected ? selectedLabel : unSelectedLabel);
    lblIcon.setIcon(selected ? selectedIcon : unSelectedIcon);
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    Object source = e.getSource();
    if (source == lblIcon || source == lblText) {
      selected = !selected;
      updateLabels();      
    }
  }

  @Override
  public void mousePressed(MouseEvent e) {
    
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    
  }

  @Override
  public void mouseEntered(MouseEvent e) {
    
  }

  @Override
  public void mouseExited(MouseEvent e) {
    
  }

  @Override
  public void addMouseListener(MouseListener listener) {
    super.addMouseListener(listener);
    lblIcon.addMouseListener(listener);
    lblText.addMouseListener(listener);
  }
  
  @Override
  public void removeMouseListener(MouseListener listener) {
    super.removeMouseListener(listener);
    lblIcon.removeMouseListener(listener);
    lblText.removeMouseListener(listener);
  }
  
  public boolean isSelected() {
    return selected;
  }

  public void setSelected(boolean selected) {
    this.selected = selected;
    updateLabels();
  }

  public String getSelectedLabel() {
    return selectedLabel;
  }

  public String getUnSelectedLabel() {
    return unSelectedLabel;
  }

  public ImageIcon getSelectedIcon() {
    return selectedIcon;
  }

  public ImageIcon getUnSelectedIcon() {
    return unSelectedIcon;
  }

  public JLabel getLblIcon() {
    return lblIcon;
  }

  public JLabel getLblText() {
    return lblText;
  }

  public void setSelectedLabel(String selectedLabel) {
    this.selectedLabel = selectedLabel;
    updateLabels();
  }

  public void setUnSelectedLabel(String unSelectedLabel) {
    this.unSelectedLabel = unSelectedLabel;
    updateLabels();
  }

  public void setSelectedIcon(ImageIcon selectedIcon) {
    this.selectedIcon = selectedIcon;
    updateLabels();
  }

  public void setUnSelectedIcon(ImageIcon unSelectedIcon) {
    this.unSelectedIcon = unSelectedIcon;
    updateLabels();
  }

  public void setLblIcon(JLabel lblIcon) {
    this.lblIcon = lblIcon;
    updateLabels();
  }

  public void setLblText(JLabel lblText) {
    this.lblText = lblText;
    updateLabels();    
  }
    
}