I'm trying to create a grid of buttons, each with a relatively simple GUI (or at least so I though):
The button is halved by a a diagonal line and a space for two numbers received as input.
I've been trying to implement it using BasicButtonUI but facing difficulties. I'm wondering if that's the correct tool to the job, or maybe as a newbie to Swing I'm missing a smarter way to do it.
I'm trying to write a program that has several navigation buttons, for example the classical "Back" and "Forward" buttons. I would need a way to let the user to navigate through the different pages (JPanels, to be true). How can I do that? Or better, how would a real programmer (I'm a beginner jet) do that? I'm using for the moment CardLayout and I thought to store the "path" that the user does and use previous() and next() methods that CardLayout provides. What about this solution? Is it a bit "dummy"? Does a kind of "navigation pattern" exist?
You may simply store created JPanels in a List or a Stack as long as they keep they internal states unchanged by any other JPanels. However this might be a little resources hungry, thats why its better to store just internal state of an object rather than whole object.
Take a look at Memento Pattern:
http://en.wikipedia.org/wiki/Memento_pattern
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:
We have many JLabels with its Naming conventions
(*jll_txtNormalCnn*).
The 'nn' literally means a coordinate of (x,y). TO be
precise, it is a digit of (0-9).
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 it 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");
}
I'm currently using net beans with Java to work on a senior project. Essentially the program is going to create word search puzzles.
While I'm still a few weeks from needing a solution, I'm having difficulty thinking of a possible control that could display a word search puzzle (not to be mistaken for a crossword puzzle).
My initial thought would be to have a grid of subclassed JPanels stored in another subclass of JPanel [as a 2D array, or something].
You could then attach MouseListeners to the grid cell JPanels if you need to detect if they've been clicked on, or whatever
Might give you an idea
brainjar - Wordsearch (source at bottom of page)
What do you mean by control? Maybe something like a JTable + Custom ListSelectionModel
I want to create an array of JButtons with the GUI Builder (not actually writing the code, but drawing it). I can only figure out how to change the name of the element, not add it to an array.
Thanks.
http://wiki.netbeans.org/FaqGuiControlArray
I think you'll find doing much "programmatically" will go beyond most GUI builders, other than for specific programmed-for exceptions.