java - declaring a variable textfield -
i trying declare jtextfield variable name
for fixed jtextfield declare after public class as
private jtextfield hh1;
but variable text field trying create them within
int count = 1 hh + count++ = new jtextfield(10);
this within private class
private void creategui() { setdefaultcloseoperation(exit_on_close); container window = getcontentpane(); window.setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gridbagconstraints gbc1 = new gridbagconstraints(); gbc.fill = gridbagconstraints.both; gbc.insets = new insets(5, 50, 5, 0); gbc1.insets = new insets(5, -100, 5, 10); int count = 1; for(int y = 0; y < 10; y++) { gbc.gridy = y; gbc1.gridy = y; for(int x = 0; x < 1; x++) { gbc.gridx = x; gbc1.gridx = x; vol1hh + count++ = new jtextfield(10); hh1 = new jlabel("hh1"); window.add(hh1 + count++, gbc1); window.add(vol1hh + count++, gbc); }
how can create variable jtextfields called hh1 hh10?
answer below
private jtextfield vol1hh[] = new jtextfield [10]; private void creategui() { setdefaultcloseoperation(exit_on_close); container window = getcontentpane(); window.setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gridbagconstraints gbc1 = new gridbagconstraints(); gbc.fill = gridbagconstraints.both; gbc.insets = new insets(5, 50, 5, 0); gbc1.insets = new insets(5, -100, 5, 10); //int count = 1; for(int y = 0; y < 10; y++) { gbc.gridy = y; gbc1.gridy = y; for(int x = 0; x < 1; x++) { gbc.gridx = x; gbc1.gridx = x; vol1hh[y] = new jtextfield(10); window.add(vol1hh[y], gbc); }
in code not following rules declare variable. variable name cannot contain +
, , neither name of variable dynamic.
if know how many jtextfield
need in case best approach use array of jtextfield
.
private jtextfield hh[] = new jtextfield[10]; int count = 0 hh[count++] = new jtextfield(10);
and if don't know how jtextfield
need use list
idea.
private arraylist<jtextfield> hh = new arraylist<>(); int count = 0; hh.add(new jtextfield(10));
Comments
Post a Comment