-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).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • what exactly is the point to this question? – Stultuske Feb 22 '19 at 07:47
  • 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. – Stultuske Feb 22 '19 at 07:53
  • 1
    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). – BackSlash Feb 22 '19 at 07:55
  • Why don't you just try and see for yourself?` – Maurice Perry Feb 22 '19 at 07:56
  • 1
    @BackSlash if you read the code of the answer carefully, it actually creates the pictures for each and every `UIManager.getInstalledLookAndFeels()`. Its just 5 examples posted – XtremeBaumer Feb 22 '19 at 08:05
  • @Stultuske See [It’s OK to Ask and Answer Your Own Questions](https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/). It is actually a 'side bar' to comments on another answer. – Andrew Thompson Feb 22 '19 at 08:06
  • @XtremeBaumer - 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. – BackSlash Feb 22 '19 at 08:08
  • @BackSlash *"did you try it yourself?"* See edit to question, requesting answers for PLAFs I can't run here. – Andrew Thompson Feb 22 '19 at 08:13
  • 1
    @BackSlash 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 answer – XtremeBaumer Feb 22 '19 at 08:14
  • 1
    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](https://stackoverflow.com/a/5630271/230513) and avoid this [irritating pitfall](https://stackoverflow.com/a/37801762/230513); I may be a little sensitive because my favorite LAFs are the most susceptible. – trashgod Feb 22 '19 at 11:36

1 Answers1

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);
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433