All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.becausetesting.commonswindow.utils.ui.ClosableTabPane Maven / Gradle / Ivy

package com.github.becausetesting.commonswindow.utils.ui;

/*-
 * #%L
 * commons-window
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2016 Alter Hu
 * %%
 * 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.
 * #L%
 */


import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;



public class ClosableTabPane extends JPanel {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel labelTitle;
	private JButton tabCloseButton;
	
	public ClosableTabPane(String title,JCommonWindowFrame frame) {
		initComponents(title);
	}
	
	public void initComponents(String title){ 
		labelTitle = new JLabel(title);
		labelTitle.setBorder(new EmptyBorder(0, 0, 0, 5));

		tabCloseButton = new JButton();
		tabCloseButton.setToolTipText("Close Tab");

		final ImageIcon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(
				getClass().getResource("images/tab/close_tab.png")));

		final ImageIcon rollOverIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(
				getClass().getResource("images/tab/close_tab_rollover.png")));

		tabCloseButton.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
		tabCloseButton.setIcon(icon);

		// Do our own roll-over using mouse and action event handlers
		tabCloseButton.setRolloverEnabled(false);

		// Remove un-needed button functionality and decoration
		tabCloseButton.setMargin(new Insets(0, 0, 0, 0));
		tabCloseButton.setBorderPainted(false);
		tabCloseButton.setBorder(null);
		tabCloseButton.setText(null);
		tabCloseButton.setContentAreaFilled(false);
		tabCloseButton.setFocusPainted(false);
		tabCloseButton.setFocusable(false);

		tabCloseButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent evt) {
				tabCloseButton.setIcon(rollOverIcon);
			}

			@Override
			public void mouseExited(MouseEvent evt) {
				tabCloseButton.setIcon(icon);
			}
		});

		tabCloseButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				tabCloseButton.setIcon(icon);
				
			}
		});

		setOpaque(false);
		setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

		add(labelTitle);
		add(tabCloseButton);
	}
	
	/**
	 * Update the tab's title.
	 *
	 * @param title
	 *            New tab title
	 */
	public void updateTitle(String title) {
		labelTitle.setText(title);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy