Java - JLabel/JTextField Combo
The following example uses a JLabel and a JTextField class and intergrates them together to be used as a simple drop in. Any comments would be great and if anyone wants to add additional functionality that would be even better. Thanks. :thumb:
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class X extends JFrame{
public static void main(String[] args){
new X();
}
public X(){
super("JLabel/JTextField");
setLayout(new GridLayout(4,1));
getContentPane().add(new LabelTextCombo("First Name", new Font("Arial",Font.PLAIN, 13), new Font("Arial",Font.PLAIN, 13), 15));
getContentPane().add(new LabelTextCombo("Last Name", new Font("Garamond",Font.PLAIN, 13), new Font("Garamond",Font.PLAIN, 13), 15));
getContentPane().add(new LabelTextCombo("Address",new Font("Verdana",Font.ITALIC, 11), new Font("Verdana",Font.ITALIC, 11), 15));
getContentPane().add(new LabelTextCombo("Phone Number",new Font("Tahoma",Font.PLAIN, 13), new Font("Tahoma",Font.PLAIN, 13), 15));
setSize(350,125);
setVisible(true);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
public void processWindowEvent(WindowEvent we){
super.processWindowEvent(we);
if(we.getID() == WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
}
class LabelTextCombo extends JPanel{
private JLabel jl;
private JTextField jtf;
public LabelTextCombo(String jltext, Font jlfont, Font jtffont, int jtfsize){
setLayout(new FlowLayout(FlowLayout.LEFT));
jl = new JLabel(jltext);
if(jlfont != null){
jl.setFont(jlfont);
}
add(jl);
jtf = new JTextField(jtfsize);
if(jtffont != null){
jtf.setFont(jtffont);
}
add(jtf);
}
public LabelTextCombo(String jltext, int jtfsize){
this(jltext, null, null, jtfsize);
}
public LabelTextCombo(String jltext){
this(jltext, 20);
}
}
Re: Java - JLabel/JTextField Combo
I'm not sure I understand what's happening...Is it the text is set to whatever style the label is?
Re: Java - JLabel/JTextField Combo
Quote:
Posted by System_Error
I'm not sure I understand what's happening...Is it the text is set to whatever style the label is?
If you use either of the two following constructors it is. public LabelTextCombo(String jltext), public LabelTextCombo(String jltext, int jtfsize) or if you specify the same font for both the JLabel and JTextField when calling the following constructor. public LabelTextCombo(String jltext, Font jlfont, Font jtffont, int jtfsize)