Results 1 to 6 of 6
Thread: Text Fields Acting Up
- 08-14-2012, 12:34 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
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:
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.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); } }
Also, in case it is relevant, I am writing this program in Eclipse.
Thanks!Last edited by Tohron; 08-14-2012 at 12:37 AM.
- 08-14-2012, 04:27 AM #2
Re: Text Fields Acting Up
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-14-2012, 05:46 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
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:
Any help on what might be causing this?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); } }
-
Re: Text Fields Acting Up
Where's your weighty constraint? Have you gone through the GBL tutorial?
- 08-15-2012, 12:18 AM #5
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: Text Fields Acting Up
[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 04:54 AM.
- 09-05-2012, 06:14 AM #6
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
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
-
disapling text fields
By javanew in forum AWT / SwingReplies: 2Last Post: 05-06-2010, 03:39 PM -
searching a row using text fields
By bigj in forum New To JavaReplies: 1Last Post: 02-03-2010, 11:28 AM -
Problem With Text Fields!
By freshoreo in forum AWT / SwingReplies: 3Last Post: 08-04-2008, 09:52 PM -
Demonstration of text fields in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:20 PM -
Help with text fields in Java
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks