开发者

Java and Its Variables (with many 's'-es) on Swing

I'm about to share abit interesting case here about the java programming and its variables.

First thing I wanted to say is that, we are in these situations:

  1. We have many JLabels with its Naming conventions (*jll_txtNormalCnn*).
  2. The 'nn' literally means a coordinate of (x,y). TO be precise, it is a digit of (0-9).
  3. The screenshot of the many Variables used here.

In the screenshot taken; We may see there are 5 x 3 table. And each column consist of each JLabel placed above it. So it is mimicing as a board with a text on it.

My very simple question is not about the Interface, instead; it is about the programming style. What if.... The variables are sooOOO many. Let say there are 100 Variables using that kind of naming conventions. And once we want to setText() to each of the variables, we want to simplify the coding- instead of typing i开发者_运维知识库t one by one.... we wanted to use for-looping to reach each of the Variable.... But, I realized it is impossible.

The code below will not work at all;

for (int x=00; x<101; x++){
(jll_txtNormalC+x).setText("Something");
}

Is there any way around to solve this matter? I'm not sure whether this is a topic of Dynamic Variable or something, because I never heard about it in Java, except 'Generics', yes I've heard.


I don't think it's a generics problem. It's not about dynamic variables, either. It sounds to me like you're having problems because you've embedded information about the location of the label in a grid inside the variable name. That's a very bad idea, in my view.

Maybe a better idea is to encapsulate that information inside another object and let it maintain the grid of labels. That's far better than your variation on Hungarian notation.


Simply put: don't use separately named variables for this. Use a collection of some kind, whether it's an array (possibly a JLabel[][]), a map or whatever is appropriate.


1) if is your requirement(s) strict quadratic and there are JLabels or JTextFields (with its Swing nested/inherits methods and its derivates including pictures),

2) if you required periodical changes for Component's contents

3) if you want to avoiding memory leaks or GPU performace or freeze

4) if you want to simple and easy to get/set data or changes

then put that to the JTable, by defalut contains JLabel in the cell, by defalut contains JTextField on CellEdit (Mouse or Keyboard input)

1) then you can pretty to forgot about naming, possitionig and another ZOO, all three areas from MVC and JTable would be still consistent

2) you can access to data just from visible/filtered/sorted/removed/rendered TableView

3) you can access to all data from TableModel

4) plus all JTable's features that were added/cames from Java6

5) save lots of time for LayoutManager, possitioning on the screen, Listeners, access to the concrete Component


Normally, when working with grids or matrices you use 2d arrays.

Store your JLabels in a 2d array. You can iterate over them or access labels at (x, y) grid-coordinates using [x][y] notation which is easy to read.

JLabel[][] labelArray = new JLabel[numRows][numCols];
for(int i = 0 ; i < labelArray.length; i++) {
    for(int j = 0; j < labelArray[i].length; j++) {
        labelArray[i][j].setText("Something");        
    }
}


store the jlabels in an array (5x3 or 10x10).


You need to use some sort of UI container component (something like a grid), or maybe just an array.


OTOH idea: create an ArrayList or an array and populate it with Jlabels. Iterate through the collection and call setText for each one.


the prober answer is the several times mentioned 2d array.

but, if your labels have to remain single variables, you can also solve this with reflection:

    for (int x=00; x<101; x++){
    Field f = getClass().getDeclaredField("jll_txtNormalC" + x);
    JLabel l = (JLabel)f.get(this);
    l.setText("Something");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜