What does a JTextField look like in various LAF instances? [closed]
Asked Answered
Q

1

-3

What does a Swing text field look like when editable, not editable and disabled, across various Pluggable Look & Feel implementations?

Below is an answer for PLAFs seen on Windows & Apple OS X. I'd also very much appreciate seeing the look on other PLAFs (e.g. GTK on *nix).

Quadrennial answered 22/2, 2019 at 7:45 Comment(10)
what exactly is the point to this question?Asymmetry
Getting an answer? then why do you answer it yourself practically within a second after posting the question? That's what I'm wondering about.Asymmetry
I don't really get the point of the question too. The same question would have been answered like "did you try making an example and testing it yourself?" if it was asked by someone "new". Also, there are applications like JTattoo which already show the difference between plenty of LaF (not just 5 of them).Cimbri
Why don't you just try and see for yourself?`Reverberator
@Cimbri if you read the code of the answer carefully, it actually creates the pictures for each and every UIManager.getInstalledLookAndFeels(). Its just 5 examples postedCornuted
@Asymmetry See It’s OK to Ask and Answer Your Own Questions. It is actually a 'side bar' to comments on another answer.Quadrennial
@Cornuted - I get it, but it still isn't clear to me what's the point of the question. It's very similar to other questions like what does System.out.println("hello"); do? - which the answer is, did you try it yourself? Maybe I'm missing something.Cimbri
@Cimbri "did you try it yourself?" See edit to question, requesting answers for PLAFs I can't run here.Quadrennial
@Cimbri I searched some SO regarding LaF and found that so far, there are mostly links to external resources. One could assume that this question is supposed to create an internal resource regarding LaFs. And he did try it himself as seen in the answerCornuted
I don't want to seem fractious, but I'm voting to reopen, as this Q&A illustrates yet another reason why GUI programs should use layouts correctly and avoid this irritating pitfall; I may be a little sensitive because my favorite LAFs are the most susceptible.Hornbeck
Q
5

They say a picture paints a thousand words, so here's a 6K word answer.

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Note that in Nimbus & Motif PLAFs, the background of the non-editable text field appears same as the editable text field, while in three others, it looks different.

The disabled text field appears different to either the editable or non-editable fields in all PLAFs.

Code

Use this code to test on your system / JRE.

import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
import javax.imageio.ImageIO;
import java.io.*;

public class TextFieldPLAF {

    TextFieldPLAF() {
        initUI();
    }

    public final void initUI() {
        UIManager.LookAndFeelInfo[] lafInfos = UIManager.getInstalledLookAndFeels();
        try {
            for (UIManager.LookAndFeelInfo lAFI : lafInfos) {
                saveImageOfLookAndFeel(lAFI);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void saveImageOfLookAndFeel(UIManager.LookAndFeelInfo lafi) throws IOException {
        String classname = lafi.getClassName();
        try {
            UIManager.setLookAndFeel(classname);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        JComponent ui = new JPanel(new GridLayout(1, 0));
        ui.setBorder(new TitledBorder(classname));
        int cols = 13;
        JTextField tf;
        tf = new JTextField("Editable & Enabled", cols);
        ui.add(tf);
        tf = new JTextField("Not Editable", cols);
        tf.setEditable(false);
        ui.add(tf);
        tf = new JTextField("Not Enabled", cols);
        tf.setEnabled(false);
        ui.add(tf);
        JOptionPane.showMessageDialog(null, ui);
        BufferedImage bi = new BufferedImage(
                ui.getWidth(), ui.getHeight(), BufferedImage.TYPE_INT_RGB);
        ui.paint(bi.getGraphics());
        File dir = new File(System.getProperty("user.home"));
        File f = new File(dir, String.format("PLAF-%1s.png", classname));
        ImageIO.write(bi, "png", f);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            new TextFieldPLAF();
        };
        SwingUtilities.invokeLater(r);
    }
}
Quadrennial answered 22/2, 2019 at 7:45 Comment(2)
Thanks @trashgod. :) Huh.. like Nimbus, the Aqua non-editable text field looks exactly like the one that's editable.Quadrennial
Aqua also selects the entire text when focusing via the tab key.Hornbeck

© 2022 - 2024 — McMap. All rights reserved.