import javax.swing.*;
public class BorderExample implements Runnable {
public void run() {
JPanel p = new JPanel();
JTextField field0 = new JTextField("null border", 16);
field0.setBorder(null);
p.add(field0);
JTextField field1 = new JTextField("empty border", 16);
field1.setBorder(BorderFactory.createEmptyBorder());
p.add(field1);
JFrame f = new JFrame("BorderExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new BorderExample());
}
}
The two textfields look the same to me. Am I going blind?
Probably not, but you're not giving the look and feel any chance to take the hint:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class BorderExample implements Runnable {
public void run() {
final JPanel p = new JPanel();
JTextField field0 = new JTextField("null border", 16);
field0.setBorder(null);
p.add(field0);
JTextField field1 = new JTextField("empty border", 16);
field1.setBorder(BorderFactory.createEmptyBorder());
p.add(field1);
JTextField field2 = new JTextField("default border", 16);
p.add(field2);
final JFrame f = new JFrame("BorderExample");
p.add(new JButton(new AbstractAction() {
{ putValue(NAME, "Look and Feel"); }
int laf;
public void actionPerformed (ActionEvent event) {
try {
UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
++laf;
if (laf >= lafs.length) laf = 0;
UIManager.setLookAndFeel(lafs[laf].getClassName());
SwingUtilities.updateComponentTreeUI(f);
f.pack();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new BorderExample());
}
}
As soon as you change to a different look and feel, any nulls get replaced with the default value for the new look and feel. So if your code is ever going to get used in something that might get skinned, you shouldn't use null.