I have a JTree that I'm filling with skills for a Game Database programme that I'm writing.
There are several categories, and subcategories (actual skills), and then levels skill below that (sometimes). Currently I'm emulating this with one skill class, some options inside, and a few enums, plus a method to check that if the skill is a category (called isCategory). Two other things to note:
Different types of skill behave differently.Some are bought once, other several times, some have options to pick from etc
Different categories contain different skill. For instance The Weapon Skills category has different types of weapons, but the Armour Skills are in a different section.
I've seen a very good example of attaching a ComboBox via a cell renderer to each node in the tree. Here is the example I found.
I understand the above code, but I can't see how to would attach the combo box to a node, and not to the tree? I've read 'How to use Trees', and I've run, and looked at the code for a few of the demos for tree. I can make basic trees, but I find the tutorials a bit obtuse and lacking in enough detail to figure out for myself how to proceed. I've found another example of only rendering leaf nodes as checkboxes, which is much more complex.
Obviously I'd like to combine the two, being able to have different categories able to have different skills and different skills have different levels of proficiency. However the only way I can think of doing so it to have different JComboBoxModels for the different types but I don't know how to do this, and I can't find out how. I've tried to edit the checkbox example to use ComboBoxes, but for the life of me I can't figure it out.
Could someone give me a hint as to what approach to take on this, as I'm new to Java and strugglingto figure out what to do?
You should implement TreeCellRenderer as well as TreeCellEditor. Both should return JComboBox with different model. Which model to choose you should decide in the getTreeCellEditorComponent/getTreeCellRenderingComponent depending on value parameter (the tree node in fact).
Would this be a good place to start?
Conceptually, yes. Both JTable and JTree use the flyweight pattern to render and edit cells/nodes.
This example cites a basic TreeCellRenderer.
This example illustrates a simple TreeCellEditor using the default renderer.
This example shows an Outline view that incorporates features of both JTable and JTree.
Related
I'm currently doing a course in Object Oriented programming in Java, and we need to create a game for our last lab so my lab partner an me chose to make a 2 player chess game without AI and using the model-view-controller approach.
We are a little bit lost as how and where to start so we need a simple UML diagram so we know where to start.
We have come up with the following classes, but aren't sure if they are enough or if all the data fields and methods make sense:
ChessBoard (model) class and ChessLogic class (?)
View class to present data from the model class
Controller class that updates the model data as well as the View class based on user input
An abstract Piece class or interface which is inherited or implemented by each of the 6 pieces.
This diagram is a nice start but there's still a lot to be done. I will draw your attention on some points you need to improve, but without preventing you from learning by doing.
First some formalities:
use the arrows for the relationships only to indicate navigability. If the link is bidirectional, either use 2 arrows or none. For example, here we could understand that View knows ChessBoard, but it is not clear if ChessBoard knows about Views: how can the ChessBoard then inform Views that the state of the board has changed after a move was done ?
indicate the multiplicity: for exemple, I wonder if it is one ChessBoard for one View or could there be several Views for a same Chessboard ?
avoid ambiguity between associations and properties. For example, in View, you have a model property of type ChessBoard. But you also have an association with ChessBoard. So is this all the same ChessBoard ? Or do we have two ChessBoards, one associated and one embedded ? It would be clearer to remove the model from the properties, and indicate model as the name of the asscoiated object at the end of the association.
The association starting from Controller that suddenly splits into two is not very practical, especially if you'll tell us about association ends and multiplicity. Prefer to visually have two very distinct lines .
Now to the core:
Your model should not just be a ChessBoard. The model should be a Game that has a couple of elements. ChessBoard is only one of them: the current position of the Pieces. But what about the layers ? Where are they ? How do I know that it's two of them ? How does the Controller know wo's turn it is ?
By calling something ChessBoard that is more than a chess board, you might create a confusion. For example, can the board know for sure if isGameOver(), just with the information on the position of the pieces ? Isn't it possible that a player decides to abandon ? So try to name your classes according to what they really represent.
How can I find out which Pieces are where on the board ?
How do I know the color of a Piece ?
How can I find out if a board's cell is free or occupied ?
What happens if a Piece is removed from the board because it was taken ?
Your UML diagram must evolve to clarify all this. So I think to complete the ChessBoard and the Piece you're missing at least Game, Player, BoardCell (also called "Square", and perhap's some containers of Pieces still on the board for each player.
Once you've completed, you also need to think about the relation between the Model, the View and the Controler, to be sure that the Controller knows enough to give commands to the model, but also that the model can inform the View when something has changed.
P.S: I've added some links to a the Chess Programming Wiki since this website describes well some elementary concepts for programming the game, some usual questions as well as a lot of references. But be aware however that although very informative this resource is not very object oriented.
we're building a small chat app for an assignement in our university. I have a question regarding how I can implement something.
This is our ui. The big white part is a jTabbedPane where conversations the user is participating in will appear. The two small ones are where active groups and active users will appear.
I found out that I can populate a jcombobox from a linkedlist using .toArray. I don't know what ui element to use, in order to display the list elements one under the other, and being "selectable" (only one at a time). The concept is that the user will select a group and press "Join", to, well, join.
This is what I have in my as to how it will look in the end.
Any pointers and advice in general would be greatly appreciated.
It looks like you're wanting to use either a JTable or a JList -- one with a custom renderer, a renderer that displays both the group name and its "status"(?).
If a JTable, then your key job is to create a TableModel that will accept your data well, either by using the DefaultTableModel (the easiest way to do this), or by creating your own model derived from the AbstractTableModel (a little more difficult, but more flexible).
For a more detailed answer, consider providing pertinent code, preferably as a minimal example program or MCVE.
I want to have one model that can be customised with different parts eg. Different arm models or different head models. I want them to all be different models (one for the head, body, one for each arm and one for each leg) and then have them all pieced together and rendered to look like one complete model. How could I implement this into my project using lwjgl and OpenGL?
There are a few ways that I could think of that would work, some of them touching on the concept of Skeletal Animation.
First, you could use your modeling software (for example, blender) and add all of the components into the scene individually, then just read in the whole scene into your game.
Another way of going about this would be to try to give each component the role of a bone, then use a skeleton structure to link all the bones together. The only problem with this is that you're going to have to create your own variables for the skinning formula.
Lastly, you could load them all into a scene in your modeling software, link them with joints, then export that into your game. This would be my choice since it would give you all the information you need to perform the skinning formula, which will give you the ability to animate the components of your structure.
Is it possible to drag and drop a container composite in SWT (Eclipse RCP)?
If it is can anyone explain how? Thanks!
Since this question has been asked many times around the SWT community, I've decided to make a blog post on this, with included source code. Check it out and don't hesitate to ask any questions if you are in doubt.
I think generally it should be possible, it is quite complicated however. Let's assume an application tailored to do so, what would be done
You select the composite in View A, trying to drag it to View B. First it is not possible to directly select a composite, you would have to provide a hook, by possibly taking a label within this composite and fetching the labels parent.
You then serialize the elements contained in the composite somehow.. anyway, what is it you want to transport?
You drop the data into View B, which gets your serialized String coming in. What to do with this String now?
You would have to dynamically create a new composite, resembling the structure of the original composite (that is where the String comes into play, which must contain all this information) and fill it with the information you wanted
You would then have to reload this view in order to show the new structure.
I think you first have to answer the question to what scenario you want to cover with draging and dropping a container before a deeper analysis can be done.
I need to make an expandable list using java swing. I will attempt to demonstrate:
Unexpanded:
>[Expand me!]
>[And me!]
Expanded:
|[Expand me!]
>[Expand us too!]
>[Expand us too!]
>[Expand us too!]
>[And me!]
So, when you click on the "Expand me" portion of the list, another lists will drop down, possibly containing more expandable lists. If you were to click on it again, it's "sub-lists" would then retract. Pretty basic. And, as you can see, I am not looking for JComboBox, and I do not think JList can do this. If someone were to point me in the right direction, or give some programming examples, I would be grateful.
Thanks,
MirroredFate
How about using a JTree.
A control that displays a set of hierarchical data as an outline.
You can try using a JTable and put a button in the first column. When the button is clicked you add more data in the rows in between.
update
Something like this:
Or this
I think the first uses a JTree but that the idea.
BTW these two belong to JIDE Soft, check if it is feasible for you to buy a license:
http://www.jidesoft.com/products/grids.htm
Is not trivial to roll you own but is not impossible either.
check for TreeTable or one example or Outline, but with notice, that on official Java (SnOracle) pages any progress died ...,