Results 1 to 6 of 6
  1. #1
    Tohron is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Text Fields Acting Up

    Okay, I'm trying to give an applet a text field with a specific position and size - there are function for text fields that seem to have this purpose (setLocation and setSize), the problem is, the functions seem to do nothing - the text field is invisible until you click somewhere in the applet, then it covers the entire applet window. Here's my code so far:

    Java Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class AppTest extends Applet {
    	
    	StringBuffer buffer;
    	JTextField field;
    	 
        public void init() {
            buffer = new StringBuffer();
            addItem("initializing... ");
            setSize(700, 500);
    
            field = new JTextField();
    
            //setLayout(new java.awt.GridLayout(3, 1));
            field.setText("Blah!");
            field.setLocation(200,210);
            field.setSize(10, 10);
            field.setBackground(Color.red);
            add(field);
            
        }
     
        public void start() {
            addItem("starting... ");
        }
     
        public void stop() {
            addItem("stopping... ");
        }
     
        public void destroy() {
            addItem("preparing for unloading...");
        }
        
        private void addItem(String newWord) {
            System.out.println(newWord);
            buffer.append(newWord);
            repaint();
        }
        
        public void paint(Graphics g) {
            g.drawRect(0, 0, 
                    getWidth() - 1,
                    getHeight() - 1);
            g.drawString(buffer.toString(), 5, 15);
        }
    }
    The setLayout command (which is commented out here) will restrict the red-filled textbox to the first row (though it fills all the horizontal space regardless of the column count). What I want, however, is just a regular textbox with a set position and set borders, that is visible at the start.

    Also, in case it is relevant, I am writing this program in Eclipse.

    Thanks!
    Last edited by Tohron; 08-14-2012 at 01:37 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Text Fields Acting Up

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tohron is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Text Fields Acting Up

    Looking at those, I decided that GridBag Layout would work the best - problem is, the only change was that instead of covering the entire screen, the text field is now locked in the middle of the window(one line high), stretching from left to right, regardless of what grid position I try to add it in (and still only becomes visible after you click in it). Here's my current code:

    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class AppTest extends Applet {
    	
    	StringBuffer buffer;
    	JTextField field;
    	JTextArea area;
    	 
        public void init() {
            buffer = new StringBuffer();
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0.5;
            c.gridx = 2;
        	c.gridy = 0;
            setSize(700, 500);
    
            field = new JTextField("Blah!", 10);
    
            field.setBackground(Color.red);
            add(field, c);
        }
     
        public void start() {
            
        }
     
        public void stop() {
            
        }
     
        public void destroy() {
            
        }
    
        public void paint(Graphics g) {
            g.drawRect(0, 0, 
                    getWidth() - 1,
                    getHeight() - 1);
            g.drawString(buffer.toString(), 5, 15);
        }
    }
    Any help on what might be causing this?

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

  5. #5
    Tohron is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Text Fields Acting Up

    Quote Originally Posted by Fubarable View Post
    Where's your weighty constraint? Have you gone through the GBL tutorial?
    [Which is that] - EDIT: Misread, also adding c.weighty = 0.5; has no evident effect.

    I have gone through the tutorial at How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) and looked at the corresponding sample code (http://docs.oracle.com/javase/tutori...ayoutDemo.java). Many of the buttons in the sample code had no more constraints than in the code I gave (though there were unfortunately no text fields in the sample code for GridBags, so there may be another necessary constraint that I'm missing).
    Last edited by Tohron; 08-15-2012 at 05:54 AM.

  6. #6
    Tohron is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Text Fields Acting Up

    Okay, am I to interpret the lack of responses in over half a month to mean that nobody knows how to prevent textboxes from being locked to the middle region of the screen when placed in a GridBag Layout? Given the silence, I don't know what else to conclude. It's hard to believe there isn't a relatively simple way around this and yet... silence.

Similar Threads

  1. disapling text fields
    By javanew in forum AWT / Swing
    Replies: 2
    Last Post: 05-06-2010, 04:39 PM
  2. searching a row using text fields
    By bigj in forum New To Java
    Replies: 1
    Last Post: 02-03-2010, 12:28 PM
  3. Problem With Text Fields!
    By freshoreo in forum AWT / Swing
    Replies: 3
    Last Post: 08-04-2008, 10:52 PM
  4. Demonstration of text fields in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:20 PM
  5. Help with text fields in Java
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:29 AM

Posting Permissions

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