Possible to Remove Border Around JTextField?

is it Possible to Remove Border Around JTextField?
[57 byte] By [Alex1989a] at [2007-11-27 2:11:48]
# 1
field.setBorder(null);
DrLaszloJamfa at 2007-7-12 2:05:24 > top of Java-index,Java Essentials,Java Programming...
# 2
hmm that didnt work but thnx to u i found a border that i liked haha :). shot oke
Alex1989a at 2007-7-12 2:05:24 > top of Java-index,Java Essentials,Java Programming...
# 3
IIRC setBorder(null) tells the look and feel to use its default border, so you have to use an explicitly transparent or zero-width one.
pm_kirkhama at 2007-7-12 2:05:24 > top of Java-index,Java Essentials,Java Programming...
# 4
As in BorderFactory.createEmptyBorder().
DrClapa at 2007-7-12 2:05:24 > top of Java-index,Java Essentials,Java Programming...
# 5
Swing related questions should be posted in the Swing forum.> IIRC setBorder(null) tells the look and feel to use its default border, Works fine for me using JDK1.4.2 on XP.
camickra at 2007-7-12 2:05:24 > top of Java-index,Java Essentials,Java Programming...
# 6

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?

DrLaszloJamfa at 2007-7-12 2:05:24 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

pm_kirkhama at 2007-7-12 2:05:24 > top of Java-index,Java Essentials,Java Programming...
# 8
Gotcha, thanks!
DrLaszloJamfa at 2007-7-12 2:05:24 > top of Java-index,Java Essentials,Java Programming...