/** * Copyright 1999-2009 The Pegadi Team * * 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. */ package org.pegadi.artis.text; import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.*; public class URLComponent extends JComponent { JComboBox comboBox; JLabel label; JLabel smile; public URLComponent() { Border emptyBorder = new EmptyBorder(0, 0, 0, 0); this.setBorder(emptyBorder); String[] objects = new String[] {"EIrik"}; comboBox = new JComboBox(objects); comboBox.setBorder(emptyBorder); setAlignmentY((float) 0.79); label = new JLabel(); label.setBorder(emptyBorder); label.setForeground(Color.BLUE); label.setFont(label.getFont().deriveFont(Font.BOLD)); if (objects.length > 0) { label.setText(objects[0]); } comboBox.setEditable(true); ((JComponent) comboBox.getEditor().getEditorComponent()).setRequestFocusEnabled(true); BorderLayout layout = new BorderLayout(); layout.setVgap(0); setLayout(layout); smile = new JLabel("\u2022"); smile.setBorder(emptyBorder); add(smile, BorderLayout.EAST); add(label, BorderLayout.CENTER); label.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { label_mousePressed(); } public void mouseEntered(MouseEvent e) { label_mouseEntered(); } public void mouseExited(MouseEvent e) { label_mouseExited(); } }); Component[] c = comboBox.getComponents(); FocusListener fl = new FocusAdapter() { public void focusLost(FocusEvent e) { comboBox_focusLost(); } }; for (Component aC : c) { if (aC instanceof JTextField) { aC.addFocusListener(fl); } } comboBox.addFocusListener(fl); } public String getPersonName() { return comboBox.getSelectedItem().toString(); } public void label_mousePressed() { remove(label); remove(smile); add(comboBox, BorderLayout.CENTER); comboBox.getEditor().getEditorComponent().requestFocus(); } public void label_mouseEntered() { label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } public void label_mouseExited() { label.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } public void comboBox_focusLost() { remove(comboBox); add(label, BorderLayout.CENTER); add(smile, BorderLayout.EAST); label.setText(comboBox.getSelectedItem().toString()); } public void setOpaque(boolean opaque) { comboBox.setOpaque(opaque); super.setOpaque(opaque); } public void setFont(Font f) { comboBox.setFont(f); label.setFont(f); smile.setFont(f); } }