Java GUI – JTextField

Το JTextField είναι ένα πολύ σημαντικό εργαλείο για να λαμβάνουμε δεδομένα από τον χρήστη. Πάμε να δούμε πιο αναλυτικά τι μπορούμε να κάνουμε με αυτό.

Τι ακριβώς είναι ένα JTextField;

Στην ουσία, ένα JTextField είναι ένα component Swing που επιτρέπει στον χρήστη να εισάγει και να επεξεργάζεται μια γραμμή απλού κειμένου. Μπορεί να το σκεφτείτε σαν ένα μικρό “κουτάκι” όπου ο χρήστης μπορεί να πληκτρολογήσει οτιδήποτε, από ένα όνομα χρήστη μέχρι μια σύντομη αναζήτηση.

Δημιουργία ενός JTextField: Οι βασικοί τρόποι.

Όπως και με τα άλλα Swing components, δημιουργούμε ένα JTextField φτιάχνοντας ένα αντικείμενο από την κλάση javax.swing.JTextField. Υπάρχουν μερικοί constructors που μπορούμε να χρησιμοποιήσουμε:

  • JTextField(): Δημιουργεί ένα άδειο πεδίο κειμένου με προκαθορισμένο μέγεθος.
JTextField emptyTextField = new JTextField();
  • JTextField(int columns): Δημιουργεί ένα άδειο πεδίο κειμένου με καθορισμένο πλάτος, σε μονάδες “στηλών”. Αυτή είναι μια ένδειξη για το πόσο χώρο θα καταλάβει οπτικά το πεδίο, αλλά δεν περιορίζει τον αριθμό των χαρακτήρων που μπορεί να εισάγει ο χρήστης.
JTextField sizedTextField = new JTextField(20); // Ένα πεδίο 20 χαρακτήρων
  • JTextField(String text): Δημιουργεί ένα πεδίο κειμένου που περιέχει καθορισμένο κείμενο.
JTextField initialTextField = new JTextField("Εισάγετε κείμενο εδώ");
  • JTextField(String text, int columns): Συνδυάζει τις δύο προηγούμενες επιλογές, δημιουργώντας ένα πεδίο κειμένου με αρχικό κείμενο και καθορισμένο πλάτος.
JTextField completeTextField = new JTextField("Αρχικό κείμενο", 15);

Πώς παίρνουμε το κείμενο που γράφει ο χρήστης;

Η βασική λειτουργία ενός JTextField είναι να λαμβάνει εισαγωγή από τον χρήστη. Για να πάρουμε αυτό το κείμενο στην εφαρμογή μας, χρησιμοποιούμε τη μέθοδο:

  • getText(): Αυτή η μέθοδος επιστρέφει ένα String.
String inputText = completeTextField.getText();
System.out.println("Ο χρήστης έγραψε: " + inputText);

JTextField: Event Handling.

Μερικές φορές θέλουμε να κάνουμε κάτι όταν ο χρήστης τελειώσει την εισαγωγή του κειμένου και πατήσει Enter, ή όταν αλλάζει το κείμενο στο πεδίο. Για να το πετύχουμε αυτό, χρησιμοποιούμε πάλι το Event Handling.

  • ActionListener για Enter: Για να αντιδράσουμε όταν ο χρήστης πατήσει το Enter και το πεδίο είναι focus, προσθέτουμε έναν ActionListener στο JTextField. Η μέθοδος actionPerformed(ActionEvent e) του listener θα εκτελεστεί όταν συμβεί αυτό.
JTextField enterTextField = new JTextField(10);
enterTextField.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String enteredText = enterTextField.getText();
        System.out.println("Ο χρήστης πάτησε Enter και έγραψε: " + enteredText);
        // Εδώ μπορείτε να κάνετε κάτι με το enteredText
    }
});
  • DocumentListener για αλλαγές κειμένου: Αν θέλουμε να παρακολουθούμε κάθε αλλαγή στο κείμενο καθώς ο χρήστης πληκτρολογεί, μπορούμε να χρησιμοποιήσουμε έναν DocumentListener. Πρέπει να πάρουμε το Document που είναι συνδεδεμένο με το JTextField και να του προσθέσουμε τον listener. Το DocumentListener έχει τρεις μεθόδους: insertUpdate(), removeUpdate(), και changedUpdate(), οι οποίες καλούνται όταν εισάγεται, διαγράφεται ή αλλάζει το κείμενο αντίστοιχα.
JTextField changeTextField = new JTextField(10);
changeTextField.getDocument().addDocumentListener(new DocumentListener() {
    @Override
    public void insertUpdate(DocumentEvent e) {
        updateText();
    }
    @Override
    public void removeUpdate(DocumentEvent e) {
        updateText();
    }
    @Override
    public void changedUpdate(DocumentEvent e) {
        // Συνήθως δεν χρησιμοποιείται για απλό κείμενο
    }
    private void updateText() {
        String currentText = changeTextField.getText();
        System.out.println("Το κείμενο άλλαξε σε: " + currentText);
        // Εδώ μπορείτε να αντιδράσετε στην αλλαγή του κειμένου
    }
});

Περισσότερες ρυθμίσεις και λειτουργίες του JTextField.

Το JTextField προσφέρει πολλές δυνατότητες για να προσαρμοστεί η συμπεριφορά και η εμφάνισή του:

  • setEditable(boolean b): Καθορίζει αν ο χρήστης μπορεί να επεξεργαστεί το κείμενο στο πεδίο (true) ή όχι (false).
  • setHorizontalAlignment(int alignment): Ορίζει την οριζόντια στοίχιση του κειμένου μέσα στο πεδίο (π.χ., SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT).
  • setFont(Font font): Αλλάζει τη γραμματοσειρά.
  • setForeground(Color color): Αλλάζει το χρώμα του κειμένου.
  • setBackground(Color color): Αλλάζει το χρώμα φόντου του πεδίου.
  • setCaretPosition(int position): Θέτει τη θέση του δείκτη εισαγωγής στο index.
  • select(int selectionStart, int selectionEnd): Επιλέγει ένα τμήμα του κειμένου.
  • getSelectedText(): Επιστρέφει το επιλεγμένο κείμενο.
  • setColumns(int columns): Το πλάτος του πεδίου σε αριθμό στηλών.
  • setToolTipText(String text): Ορίζει ένα επεξηγηματικό κείμενο που εμφανίζεται όταν ο χρήστης αφήνει τον δείκτη του ποντικιού πάνω από το πεδίο.

Πότε χρησιμοποιούμε ένα JTextField;

Το JTextField είναι ιδανικό για περιπτώσεις όπου θέλουμε ο χρήστης να εισάγει μια σύντομη γραμμή κειμένου. Μερικά συνηθισμένα παραδείγματα:

  • Εισαγωγή ονόματος χρήστη και κωδικού πρόσβασης.
  • Πεδίο αναζήτησης.
  • Εισαγωγή μιας απλής τιμής ή λέξης-κλειδιού.
  • Επεξεργασία ενός μικρού τμήματος κειμένου.

Για την εισαγωγή μεγαλύτερου κειμένου, πολλών γραμμών, συνήθως χρησιμοποιούμε το JTextArea.

Αφήστε ένα Σχόλιο

Η ηλ. διεύθυνση σας δεν δημοσιεύεται. Τα υποχρεωτικά πεδία σημειώνονται με *

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Decline
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Who we are

Our website address is: http://stavrosbyte.eu.

Comments

When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection. An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.

Media

If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.

Cookies

If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year. If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser. When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed. If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.

Embedded content from other websites

Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website. These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.

Who we share your data with

If you request a password reset, your IP address will be included in the reset email.

How long we retain your data

If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue. For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.

What rights you have over your data

If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.

Where your data is sent

Visitor comments may be checked through an automated spam detection service.
Save settings
Cookies settings