Java – create multiple jlabels and jtextfields in a less redundant way?
Anyone familiar with the Java GUI – swing package... And knows how to create multiple swing objects in a loop, such as "jlabels and jtextfields", so you don't have to set them redundantly anybody???
For example – I have to create 60 textfields and 11 jlabels, etc I don't think it's necessary (if possible) to create each one separately like this
JLabel jlblName = new JLabel("first one"); JLabel jlblName = new JLabel("first two"); JLabel jlblName = new JLabel("first three"); //etc...
If possible, I want to find a way to set the boundary and add labels to the panel in a shorter way
I've tried different ways to do this - using arrays and creating get methods, but I'm still out of luck
If you can, please help me
Here's an example I've written so far... But even if it does work, it doesn't seem to be practical for serval reasons I'm not sure why it doesn't work
public class prospectVer2 { private static int l = 59; //,t = 20,c=11; public static String getInfo(int b) { int a=b; String [] lah = new String [prospectVer2.l]; lah [0]= "a"; lah [1]= "b"; lah [2]= "c"; lah [3]= "d"; lah [4]= "e"; lah [5]= "f"; lah [6]= "g"; lah [7]= "e"; lah [8]= "f"; lah [9]= "g"; lah [10]= "h"; lah [11]= "i"; lah [12]= "j"; lah [13]= "k"; lah [14]= "l"; lah [15]= "m"; lah [16]= "n"; lah [17]= "o"; lah [18]= "p"; lah [19]= "q"; lah [21]= "r"; lah [22]= "s"; lah [23]= "t"; lah [24]= "u"; lah [25]= "v"; lah [26]= "w"; lah [27]= "x"; lah [28]= "y"; lah [29]= "z"; lah [30]= "aa"; lah [31]= "bb"; lah [32]= "cc "; lah [33]= " dd"; lah [34]= " ee"; lah [35]= " ff "; lah [36]= " gg "; lah [37]= " hh "; lah [38]= " ii "; lah [39]= "jj"; lah [40]= "kk"; lah [41]= "ll"; lah [42]= "mm"; lah [43]= "nn"; lah [44]= "oo"; lah [45]= "pp"; lah [46]= "qq"; lah [47]= "rr"; lah [48]= "ss"; lah [49]= "tt"; lah [50]= "uu"; lah [51]= "vv"; lah [52]= "ww"; lah [53]= "xx"; lah [54]= "yy"; lah [55]= "zz"; lah [55]= "aaa"; lah [56]= "bbb"; lah [57]= "ccc"; lah [58]= "ddd"; lah [59]= "eee"; String infos= lah[a]; return infos; } public static void main(String[] args) { // Declare variables - arrays // Create Frame and Panel - set size - JFrame frame = new JFrame("Prospect Assignment"); frame.setSize(700,900); JPanel mypanel = new JPanel(); JLabel[] labels = new JLabel[prospectVer2.l]; // Create labels (60) - for loop - for (int i=0; i<labels.length; i++) { labels[i] = new JLabel(prospectVer2.getInfo(i)); mypanel.add(labels[i]); labels[i].setBounds(i*10+245,i*10+210,120,20); } // extra functions for gui frame.add(mypanel); mypanel.setLayout(null); frame.setVisible(true); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); }
By the way, I have to copy this site using the Javas swing package https://cunycitytech.askadmissions.net/emtinterestpage.aspx?ip=prospect thank you!
Solution
If you try to start the code, the problem is obvious:
Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 59 at prospectVer2.getInfo(prospectVer2.java:70) at prospectVer2.main(prospectVer2.java:87)
In your code, you have private static int L = 59; / /, c = 11; And L are used to specify the length of the array In the Java array, it starts from the 0 index, so it means that in your case, the last accessible array index should be 58, but you try lah [59] = "EEE" That's why your code doesn't work
You can use one of the following solutions:
>Add L: private static int L = 60; > Or delete the access right to the 59th index (LAH [59] = "EEE";)