Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Index of gui elements (array style)

Started by kordova Oct 26, 2002 at 1:39 PM 12 replies 400+ views
Original Post
kordova
kordova
I can not seem to find an example or even a mention of the ability to create elements such as textfields in an array fashion...such as txtField[1] = ... txtField[2] = ... can this be done in java as I haven''t been able to tinker in such a fashion as to get it to function (or compile for that matter)?
Viro
Viro
Don''t see why not. The following should work without complaints


  

JLabel[] lblMany = new JLabel[10];

for(int i = 0; i < lblMany.length; i++) {
lblMany = new JLabel("This is button " + i);
}

"Linux is not about free software, it is about community," -- Steve Balmer, Microsoft Chief Executive.
kordova
kordova
  
public class jGrey2 extends javax.swing.JApplet
{
JTextField txtText[];
public jGrey2()
{
//...some things
}

public void init() {
//JLabel[] lblMany = new JLabel[10];
txtText[] = new JTextField[2];



I've been toying with this, and why does this not work?


Also, in adding listeners is...

txtText[0].addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyTyped(java.awt.event.KeyEvent evt)
{
txtAText[0]KeyTyped(evt);
}
});
...proper?

[edited by - kordova on October 28, 2002 9:37:54 PM]
CaptainJester
CaptainJester
quote:
Original post by kordova
txtText[] = new JTextField[2];

I''ve been toying with this, and why does this not work?



The new you have here sets up the array. You then have to new each element to set up the individual classes.

for(int i=0;i<2;i++) {
txtText=new JTextField("Some text"+i);
}

quote:
[i]Original post by kordova
txtText[0].addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyTyped(java.awt.event.KeyEvent evt)
{
txtAText[0]KeyTyped(evt);
}
});


You can do it this way. I personally prefer to implement the event listeners.



Make it work, then
make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
kordova
kordova
JTextField txtText[] = new JTextField[2];
public jGrey2()
{
}
/*JLabel[] lblMany = new JLabel[10];
for(int i = 0; i < lblMany.length; i++)
{
lblMany = new JLabel("This is button " + i);
}*/
public void init() {
for(int i = 0; i < txtText.length; i++)
{
txtText = new JTextField(String.valueOf(p.txtVal));
}
...


with this i get the error:


jGrey2.java:57: incompatible types
found : javax.swing.JTextField
required: javax.swing.JTextField[]
txtText = new JTextField(String.valueOf(p[i].txtVal));


[edited by - kordova on October 28, 2002 10:27:22 PM]
Viro
Viro

  
JTextField txtText[] = new JTextField[2];
public jGrey2()
{
}
/*JLabel[] lblMany = new JLabel[10];
for(int i = 0; i < lblMany.length; i++)
{
lblMany[i] = new JLabel("This is button " + i);
}*/

public void init() {
for(int i = 0; i < txtText.length; i++)
{
txtText[i] = new JTextField(String.valueOf(p.txtVal));
}


Remember the array index. After all, it is an array.
"Linux is not about free software, it is about community," -- Steve Balmer, Microsoft Chief Executive.
CaptainJester
CaptainJester
Hint: use [ source ][ /source ] to display code. That way your array index won''t disappear. Just remove the spaces before and after source for the tag to work.



Make it work, then
make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
cyrax256
cyrax256
When you declare and assign an array of classes you do this:


  
MyNeatClass[] mnc = new MyNeatClass[50];


But when you do it separately, at the time of assign you don't use the brackets, that's why it gives you a compile-time error:


  
MyNeatClass[] mnc;

//mnc[] = new MyNeatClass[50]; <----You DON'T do this

mnc = new MyNeatClass[50]; //This way works



El Ciro

[edited by - cyrax256 on October 29, 2002 11:51:31 AM]
kordova
kordova
Ah, ok...that clears up things (I think...) greatly. I have gotten this to compile with no errors...but now it says that the applet was not initialized? What might be the cause of this...no matter where I put things, either it won't compile because they are incorrectly places (which I now grasp courtesy of your examples) or, as I stated above, it says applet not initialized...


  
public class jGrey2 extends javax.swing.JApplet
{
JTextField[] txtText;
public jGrey2()
{
//...

}
public void init()
{
txtText = new JTextField[2];
for(int i = 0; i < txtText.length; i++)
{
txtText[i].setText(String.valueOf(p[i].txtVal));
}
Container contentPane = getContentPane();
//i have several other non-array style texts and labels

contentPane.setLayout(new java.awt.GridLayout(16, 3, 2, 2));
for(int i = 0; i < txtText.length; i++)
{
txtText[i].setText(String.valueOf(p[i].txtVal));
}
contentPane.add(txtText[0]);
contentPane.add(txtText[1]);

//...

}
}


Edit: Oh, and thanks all for your input...I try to not usually be so dependent on anything but my books and sources available...but this little intricacy has had me stumped to no avail...

[edited by - kordova on October 29, 2002 7:05:15 PM]
Viro
Viro
quote:

txtText.setText(String.valueOf(p[i].txtVal));


What''s p[]? It doesn''t seem defined.
"Linux is not about free software, it is about community," -- Steve Balmer, Microsoft Chief Executive.
kordova
kordova
it is, no worries, i just removed the code I know works (hmm...maybe I should not...) If I get back to my room before an answer is about I''ll post the whole code (though there are 16x3 labels and textfields so I had hoped to save a headache and actually get a response)
CaptainJester
CaptainJester
You are still not using new properly. Here is your code:

  
public class jGrey2 extends javax.swing.JApplet {
JTextField[] txtText;
public jGrey2() {
//...

}
public void init() {
txtText = new JTextField[2];
for(int i = 0; i < txtText.length; i++) {
txtText[i].setText(String.valueOf(p[i].txtVal));
}
Container contentPane = getContentPane();
//i have several other non-array style texts and labels

contentPane.setLayout(new java.awt.GridLayout(16, 3, 2, 2));
for(int i = 0; i < txtText.length; i++) {
txtText[i].setText(String.valueOf(p[i].txtVal));
}
contentPane.add(txtText[0]);
contentPane.add(txtText[1]);
//...

}
}

Here''s the fix:

  
public class jGrey2 extends javax.swing.JApplet {
JTextField[] txtText;
public jGrey2() {
//...

}

public void init() {
Container contentPane = getContentPane();
txtText = new JTextField[2];
//i have several other non-array style texts and labels

contentPane.setLayout(new java.awt.GridLayout(16, 3, 2, 2));
for(int i = 0; i < txtText.length; i++) {
// >>> need to ''new'' each JTextField <<<

txtText[i]=new JTextField(String.valueOf(p[i].txtVal);
// >>> to save work, do your add in the loop <<<

contentPane.add(txtText[i]);
}
//...

}
}





Make it work, then
make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
kordova
kordova
D''oh. It was sort of so blatantly obvious I suppose I never really looked at it... Anyway, thanks a lot for helping with that, your patient aid is much appreciated.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.