Results 1 to 8 of 8
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default TextFields not showing up

    Java Code:
    package gui;
    
    import java.awt.*;
    
    import javax.swing.*;
    
    public class Registration extends JApplet {
    	TextField TF;
    	TextField TF1;
    	TextField TF2;
    	TextField TF3;
    	TextField TF4;
    	JButton b1;
    	JButton b2;
    	
    		public void init(){
    			try{
    			SwingUtilities.invokeAndWait(new Runnable(){
    				public void run(){
    					swingGUI();
    				}
    			});
    		}
    			catch(Exception e){
    				System.out.println(e);
    			}
    		}
    		public void swingGUI(){
    			TF = new TextField(20);
    			TF1 = new TextField(20);
    			TF2 = new TextField(20);
    			TF3 = new TextField(20);
    			TF4 = new TextField(20);
    			TF.setEditable(true);
    			TF1.setEditable(true);
    			TF2.setEditable(true);
    			TF3.setEditable(true);
    			TF4.setEditable(true);
    			b1= new JButton("OK");
    			b2 = new JButton("Cancel");
    			setSize(300, 300);
    			setLayout(new FlowLayout());
    			add(TF);
    			add(b1);
    			add(b2);
    			
    			
    		}
    }
    Not really sure on what I have here, but it seems to me like I added 5 TextFields, but when I run it, none of them show up. Anything that I didn't do...?


    Also, if you guys know how to do it, how would I go about making a thing where if I click OK Another TextField is added? And If I hit cancel, it closes it. Please no Yoda answers like "Have you Googled it" because that doesn't help me. It's a waste of your time to write it and my time to read it.

  2. #2
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: TextFields not showing up

    I got it, I didn't add the TF1,2,3, and 4.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: TextFields not showing up

    I would recommend using JTextField rather than TextField since this is a Swing JApplet.

  4. #4
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: TextFields not showing up

    Java Code:
    package gui;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class Registration extends JApplet {
    	JTextField TF;
    	JTextField TF1;
    	JTextField TF2;
    	JTextField TF3;
    	JTextField TF4;
    	JButton b1;
    	JButton b2;
    	JLabel j;
    	JLabel j1;
    	JLabel j2;
    	JLabel j3;
    	JLabel j4;
    	
    	
    		public void init(){
    			try{
    			SwingUtilities.invokeAndWait(new Runnable(){
    				public void run(){
    					swingGUI();
    				}
    			});
    		}
    			catch(Exception e){
    				System.out.println(e);
    			}
    		}
    		public void swingGUI(){
    			j = new JLabel("Name");
    			j1 = new JLabel("Age");
    			j2 = new JLabel("Height");
    			j3 = new JLabel("Address");
    			j4 = new JLabel("Zip");
    			TF = new JTextField(20);
    			TF1 = new JTextField(6);
    			TF2 = new JTextField(10);
    			TF3 = new JTextField(20);
    			TF4 = new JTextField(6);
    			TF.setEditable(true);
    			TF1.setEditable(true);
    			TF2.setEditable(true);
    			TF3.setEditable(true);
    			TF4.setEditable(true);
    			b1= new JButton("OK");
    			b2 = new JButton("Cancel");
    			setSize(300, 300);
    			setLayout(new FlowLayout());
    			add(j);
    			add(TF);
    			add(j1);
    			add(TF1);
    			add(j2);
    			add(TF2);
    			add(j3);
    			add(TF3);
    			add(j4);
    			add(TF4);
    			add(b1);
    			add(b2);
    			
    			
    		}
    }
    Alright, I have this code and I want the Jlabels to be on the left hand side and the Fields to the right of them and then the buttons at the bottom. Right now it just looks like this


  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: TextFields not showing up

    Consider nesting JPanels, each using their own layouts, consider using a GridBagLayout for the JPanel that holds the JTextFields and JLabels. For example please have a look at my code examples here and here.

  6. #6
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: TextFields not showing up

    Java Code:
    package gui;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class DisplayFrame {
    	JButton JB;
    	JTextField TextF;
    	JTextArea TextA;
    	JScrollBar ScrollB;
    	JCheckBox Check;
    	JLabel Label;
    	
    	public void Display(){
    		JFrame f = new JFrame();
    		JB = new JButton("Button");
    		TextF = new JTextField("TextField", 10);
    		TextA = new JTextArea("TextArea", 10, 10);
    		ScrollB = new JScrollBar();
    		Check = new JCheckBox("CheckBox");
    		Label = new JLabel("Label");
    		f.setLayout(new FlowLayout());
    		f.setSize(300, 250);
    		f.setVisible(true);
    		
    		f.add(JB);
    		f.add(TextF);
    		f.add(TextA);
    		f.add(ScrollB);
    		f.add(Check);
    		f.add(Label);
    	}
    }
    Just a quick question, will this run? It seems that my Eclipse thinks that it is still an Applet... If it still is, how do I change it to just a normal program. Or can Applets not be run from an HTML file?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: TextFields not showing up

    Give it a main method for it to run as a desktop application. You can find out more about this in the introductory Java tutorials.

  8. #8
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: TextFields not showing up

    Java Code:
    package gui;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class displayFrame1 extends JPanel{
    	public void layout(){
    		JPanel p = new JPanel(new FlowLayout());
    		p.add(new JButton("Button"));
    		p.add(new JTextField(5));
    		p.add(new JScrollBar());
    		p.add(new JTextArea("TextArea"));
    		p.add(new JCheckBox("CheckBox"));
    		p.add(new JLabel("Label"));
    		
    		add(p, BorderLayout.NORTH);
    	}
    	private static void swingGUI(){
    		JFrame frame = new JFrame("Layout");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new displayFrame1());
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	}
    	public static void main(String[] args){
    		SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				swingGUI();
    			}
    		});
    	}
    }
    Alright using some of your code that you showed, I made this, but all it gives me is this:


    I added the main method but I get a ton of errors.

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
    	Cannot make a static reference to the non-static field JB
    	Cannot make a static reference to the non-static field TextF
    	Cannot make a static reference to the non-static field TextA
    	Cannot make a static reference to the non-static field ScrollB
    	Cannot make a static reference to the non-static field Check
    	Cannot make a static reference to the non-static field Label
    	Cannot make a static reference to the non-static field JB
    	Cannot make a static reference to the non-static field TextF
    	Cannot make a static reference to the non-static field TextA
    	Cannot make a static reference to the non-static field ScrollB
    	Cannot make a static reference to the non-static field Check
    	Cannot make a static reference to the non-static field Label
    
    	at gui.DisplayFrame2.Display(DisplayFrame2.java:16)
    	at gui.DisplayFrame2.access$0(DisplayFrame2.java:14)
    	at gui.DisplayFrame2$1.run(DisplayFrame2.java:36)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
    	at java.awt.EventQueue.access$000(EventQueue.java:102)
    	at java.awt.EventQueue$3.run(EventQueue.java:662)
    	at java.awt.EventQueue$3.run(EventQueue.java:660)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
    Java Code:
    package gui;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class DisplayFrame2 {
        JButton JB;
        JTextField TextF;
        JTextArea TextA;
        JScrollBar ScrollB;
        JCheckBox Check;
        JLabel Label;
         
        private static void Display(){
            JFrame f = new JFrame();
            JB = new JButton("Button");
            TextF = new JTextField("TextField", 10);
            TextA = new JTextArea("TextArea", 10, 10);
            ScrollB = new JScrollBar();
            Check = new JCheckBox("CheckBox");
            Label = new JLabel("Label");
            f.setLayout(new FlowLayout());
            f.setSize(300, 250);
            f.setVisible(true);
             
            f.add(JB);
            f.add(TextF);
            f.add(TextA);
            f.add(ScrollB);
            f.add(Check);
            f.add(Label);
        }
        public static void main(String[] args){
        	SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				Display();
    			}
    		});
        }
    }
    Last edited by Army; 06-20-2012 at 07:41 PM.

Similar Threads

  1. textfields - cursor
    By newbie123 in forum AWT / Swing
    Replies: 6
    Last Post: 09-29-2011, 07:39 PM
  2. applet and textfields
    By poajavaweb in forum Java Applets
    Replies: 4
    Last Post: 01-28-2011, 09:56 PM
  3. adding textfields
    By themanepalli in forum Java Applets
    Replies: 1
    Last Post: 12-28-2010, 04:56 AM
  4. Buttons and TextFields
    By Xystus777 in forum New To Java
    Replies: 27
    Last Post: 03-17-2009, 02:26 AM
  5. information from a textfields
    By bbq in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-28-2007, 06:28 PM

Posting Permissions

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