Results 1 to 3 of 3

Thread: Java - JLabel/JTextField Combo

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

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

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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?

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Java - JLabel/JTextField Combo

    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width