- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we implement an editable JLabel in Java?
JLabel
- A JLabel class can extend JComponent class and an object of JLabel provides text instructions or information on a GUI.
- A JLabel can display a single line of read-only text, an image or both text and image.
- The important methods of a JLabel are setText(), setIcon(), setBackground(), setOpaque(), setHorizontalAlignment(), setVerticalAlignment() and etc.
- A JLabel can explicitly generate a PropertyChangeListener interface.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; public class JEditableLabel extends JFrame { public JEditableLabel() { setTitle("JEditableLabel"); setLayout(new FlowLayout()); final JLabel label = new JLabel(" Welcome to Tutorials Point"); final JTextField textfield = new JTextField(); final CardLayout cl = new CardLayout(); final JPanel panel = new JPanel(cl); panel.add(label, "label component"); panel.add(textfield, "textfield component"); add(panel); label.addMouseListener(new MouseAdapter() { public final void mouseEntered(MouseEvent evt) { textfield.setText(label.getText()); cl.show(panel, "textfield component"); } }); textfield.addMouseListener(new MouseAdapter() { public final void mouseExited(MouseEvent evt) { label.setText(textfield.getText()); cl.show(panel, "label component"); } }); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JEditableLabel(); } }
Output
Advertisements