- Trending Categories
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?n
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
- Related Articles
- How can we implement editable JComboBox in Java?
- How can we implement a moving text using a JLabel in Java?
- How can we implement a JLabel text with different color and font in Java?
- How can we rotate a JLabel text in Java?
- How can we implement a JToggleButton in Java?
- How can we implement transparent JDialog in Java?
- How can we implement a Custom HashSet in Java?
- How can we implement a scrollable JPanel in Java?
- How can we implement a rounded JTextField in Java?
- How can we implement a timer thread in Java?
- How can we implement a custom iterable in Java?
- How can we implement auto-complete JComboBox in Java?\n
- How can we implement the SubmissionPublisher class in Java 9?
- How can we implement the Subscriber interface in Java 9?
- How can we Implement a Stack using Queue in Java?

Advertisements