Associate a Jbutton with object values of an ArrayList. Swing - java

I am working on a mahjong project.
My code contains a dynamic List<List<Tiles>> board.
The abstract class Tiles gets extended by the different kind of Tiles the game has.
Now im interesting on extending this small project with GUI componenets.
My thinking is to create the grid with a GridBagLayout cause my Array is dynamic sized.
A simmilar question has been asked in the past but i had hard time understanding anything from the answers and its a bit old also. old post
What i need is: when im creating an array of Jbuttons i want uponcreation of each button to place inside of them the info of each cell of the array.
Here is for example what my board array looks like: (one cell example)
specialvalue= 1x
coordinates = {0,0}
colour = blue
how i can hold this information in a Jbutton ?
And if its not possible , whats the best way to go from here?
Thanks in advance,
I can post some code of the project upon needed.

As i come back to my question i see that there was never an exact answer so here is how i did it at last and worked.
I used an abstract class for the Tiles , then i created a hashmap at my GUI class and i made key-value pairs with keys:jbuttons and values: all the info of Tiles i needed.
That was the main key to the solution.

Related

How to make a board in Java using 2D arrays and JTable ?

What I have in mind is to make a JTable. The model will use the 2D array's columncount and rowcount to set the JTable's rows and columns.
What I cant figure out is how do I go around making fields for these cells in the table?
Example fields like - Status(boolean), Image etc. So by having these fields I want to edit the cells when the application is running and show the user the updates.
I have tried to make a 2D object and add fields in there. But it doesnt seem to work.
What would be a good way to build this?
I am using swing to build this.
This isn't a very specific question so that limits the answer I can give - from what you want to do, I'd suggest
Making sure you understand how the Model-View-Controller paradigm works in Swing
Looking into the Observer / Observable framework and how it fits in with MVC
Extending some sort of Swing Component to reflect changes in your model.
Once you've covered the above you should find that implementing what you want to do is reasonably trivial, and that you can ask more directed questions that will get better responses.

Chess view and javafx

I need some advices about how to build code correctly.
I creating a chess viewer using javafx.
1) As a new one I don't exactly know what is the best way to make correct hierarchy.
I think to create abstract class Figure and the problem is it seems I need overide own behavior.
I tried extends Node but it seems wrong.
What is correct way to extends to make custom element in javafx?
Maybe you know good link anout creating custom element (not necessary chess) where described this process.
2) I looked at both (browser chess/desktop programms) and one thing about I'm thinking is when you grab & hold chess piece icon and dragging around the board the image icon is moving after your cursor. So in javafx it can be done with MouseEvent that's I understand.
But the question is how to redrawing it? Calculating coordinats?
3) About board. I think it is good way to make some class Cell based on Rectangle ovveriding behavior and class Field which include array of Cell(s)?

Swapping JButtons in Java

I made an array list of strings and assigned an image to each string. Then, I randomized these images. I want to now make a method that swaps one button to the button adjacent to it, but I have no idea how to do this. Any suggestions about how to go about it? Thanks
First suggestion: Don't. Don't swap JButtons as you're making things much harder than they need to be. Instead if possible swap images or more specifically ImageIcons, and this can be easily done using the JButton method, setIcon(...).
It almost sounds as if you're trying to create a memory game, and if so there are plenty of examples of just this sort of thing to be found on this site, at least one created by me.
As always in these sorts of things, first concentrate on the program's model, that is, its logical underpinnings, and only after getting that working, apply it to the program's view or its GUI representation of the model's state.

Creating a Grid in Java

As a way of learning Java, I'm writing this little application for grade schoolers to practice basic math. The idea is that the kid does any number of math problems, as long as they are in the app, it just continues to throw problems at them until they click a 'Done' button. When they decide to be done, I want a new JFrame to come up that will show them all of the problems they attempted, along with their answer, and whether they got the problem right or wrong.
The advice that I am looking for is what is the best way for me present these results. I looked into the GridLayout and the GroupLayout, but I don't think that these are exactly right. I did something similar in VBA for Excel, and there I just ran a for loop with one iteration for every problem they attempted. Each iteration would add a row of labels to the frame with the elements of the problem displayed in the various labels. I tried this in Java, but I'm not even able to get the labels to even display.
So before I get all specific and start posting my code, I want to ask a bigger question, which is "what is the best method to create a view like this?" Often, I go off in one direction only to waste time before somebody suggests a totally different (and better) approach.
Thanks!
(edit: here's an image of how I did this in Excel. I'm trying to repeat basically the same thing in Java)
One simple way to make that design would be to use a mix of components. You could have a bunch of JLabels and JPanels stacked in a vertical FlowLayout. The grid you have described would be best designed in a JTable, something like the below:
If you like tables like Excel then, Java provides JTable class to create tables, if you want.
Tutorial : http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

How to Create a Java Quiz GUI?

I would like to create a quiz for my Java GUI.
I'd like it to display a question on one page, then have a next button which takes the user to another page telling them if that question was correct or not, and if it is correct they can move on to the next question (by clicking another next button) and if they got it wrong, have to go back and answer it again. However I have no clue how to do this!!
So far I have a simple GUI with a welcome page and tabs down the side, one of which includes a quiz. Would it be a better idea to create the quiz in Flash or something and then embed it into my Java application? Or just do the whole thing in Java? I'm really new to java so I'm not at all sure what to do, any help would be greatly appreciated!
Thanks
It would be best to use just Java. Consider using CardLayout with your GUI to allow swapping questions, or else you can create and modify key components on the fly such as the text displayed by JLabels and JRadioButtons.
Key though before considering any GUI structures is to first create solid OOPS based non GUI classes to handle your questions. For instance, you may want classes for:
Question class that holds a question String, a List of possible answer Strings, a correct answer String. This class can randomly order the incorrect and correct answers, can have a method for checking if the answer selected is correct.
A Test class that holds a collection of questions, that can present questions in random order, that can hold the score obtained.
A QuestionReaderWriter class that can read and write questions to a text file (you definitely do not want to hard-code the question text).
For this type of app it would be best to use the language you are more familiar with.
Since this is just a simple display this then do that there really is nothing very difficult about it.
Personal, I would lean towards Flash as the flash IDE is drag and drop and an experienced flash guru could make this in a few hours. Flash IDE is geared towards this.

Categories

Resources