Using arrays to make textfields, problem.

Here's my problem when i run this code, it makes a string in the middle of the screen. This string contains the text of the textfield, so i think it supposed to be a textfield. Do you know why this program is doing this.

I have JDK 1.5.0_06 and JRE 1.6.

Can you help me out please, thanks in advance.

/**

* @(#)HusIP.java

*

* HusIP application

*

* @author

* @version 1.00 2007/5/17

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass HusIPextends JFrame{

private JTextField[] maskrij;// array for mask fields

privatefinalint AANTAL_MASK = 4;//final for number of Mask fields

private JTextField[] iprij;// array for ip fields

privatefinalint AANTAL_IP = 5;// final for IP fields

publicstaticvoid main(String[] args){

new HusIP();

}

public HusIP(){

addWindowListener (new WindowHandler());// for the window

setSize(new Dimension (500, 500));//setting the size

setTitle("Hus Corp. IP-Calculator.");

iprij =new JTextField [AANTAL_IP];

int xpos = 0;

for (int i = 0; i <iprij.length; i++){// creating textfields in the frame

iprij[i] =new JTextField ("192", 3);//making its value 192 for id reasons

iprij[i].setBounds (xpos, 20, 30, 20);// setting bounds, making xpos so it moves 40 spaces

add(iprij[i]);

xpos+=40;//increasing xpos

}

maskrij =new JTextField [AANTAL_MASK];

int xpos1 = 0;

for (int j = 0; j >< maskrij.length; j++){// making textfields.

maskrij[j] =new JTextField ("255", 3);//making its value 255 for id reasons

maskrij[j].setBounds (xpos1, 50, 30, 20);

add(maskrij[j]);

xpos1+=40;//increasing xpos1

}

setVisible(true);//so you can see frame

}

privateclass WindowHandlerextends WindowAdapter{//closing window

publicvoid windowClosing (WindowEvent e){

dispose();

System.exit(0);

}

}

}

[4239 byte] By [MrHusa] at [2007-11-27 4:43:26]
# 1
Use getContentPane().add(...); instead of add(...)
Dalzhima at 2007-7-12 9:55:13 > top of Java-index,Java Essentials,New To Java...
# 2
Ok I've found the problem it apparently creates a textfield the size of the frame, does anyone have a solution. getContentPane().add() doesn't solve the problem.
MrHusa at 2007-7-12 9:55:13 > top of Java-index,Java Essentials,New To Java...
# 3

The other problem you had is that when you use absolute coordinates, you have to disable any layout manager you were using. To do this, you only need to add a line like

getContentPane().setLayout(null);

here's the resulting code:

/**

* @(#)HusIP.java

*

* HusIP application

*

* @author

* @version 1.00 2007/5/17

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class HusIP extends JFrame {

private JTextField[] maskrij; // array for mask fields

private final int AANTAL_MASK = 4; //final for number of Mask fields

private JTextField[] iprij; // array for ip fields

private final int AANTAL_IP = 5; // final for IP fields

public static void main(String[] args) {

new HusIP();

}

public HusIP() {

addWindowListener (new WindowHandler()); // for the window

setSize(new Dimension (500, 500)); //setting the size

setTitle("Hus Corp. IP-Calculator.");

getContentPane().setLayout(null);

iprij = new JTextField [AANTAL_IP];

int xpos = 0;

for (int i = 0; i <iprij.length; i++){ // creating textfields in the frame

iprij[i] = new JTextField ("192", 3); //making its value 192 for id reasons

iprij[i].setBounds (xpos, 20, 30, 20); // setting bounds, making xpos so it moves 40 spaces

getContentPane().add(iprij[i]);

xpos+=40; //increasing xpos

}

maskrij = new JTextField [AANTAL_MASK];

int xpos1 = 0;

for (int j = 0; j >< maskrij.length; j++){ // making textfields.

maskrij[j] = new JTextField ("255", 3); //making its value 255 for id reasons

maskrij[j].setBounds (xpos1, 50, 30, 20);

getContentPane().add(maskrij[j]);

xpos1+=40; //increasing xpos1

}

setVisible(true); //so you can see frame

}

private class WindowHandler extends WindowAdapter { //closing window

public void windowClosing (WindowEvent e){

dispose();

System.exit(0);

}

}

}

Dalzhima at 2007-7-12 9:55:13 > top of Java-index,Java Essentials,New To Java...