Swapping JButtons in Java - 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.

Related

Ideas on how to set a button to call a specific position from an ArrayList in a separate activity

Long post ahead...
Ok, I'm not sure how to explain it so I made a couple of screenshots. Also, I want to start off by saying I'm not looking for somebody to write code for me to copy, or links to specific articles. I just need help figuring out if what I want to make is possible and any direction on what to look at. I just haven't done something like that and so far I can't seem to word my searches well enough it seems to be able to find a solution.
TL;DR: I need to get scrn2 to either swipe left and right to prev/next card from the RecyclerView, or add buttons that do that
So. Here's the screenshot. I have already made scrn1 and scrn2, and am looking for a way to get to my mockup (that heart button there is just wishful thinking and I'm not at all concentrating on it, and the buttons on top of the cards are just a left-over from an old test I forgot to remove).
The idea behind this is just sort of a catalogue/text book to help with self-preparation for an exam on a specific topic.
What I have is a RecyclerView that I've populated with CardViews in an ArrayList. Since I'll be having a few cathegories of cards, and each will have around 40-50 in there, this seems like an efficient and quick way to load a long list like that, and it runs smoothly so far so I'm happy with it.
In order to get a "full details" screen (scrn 2) I implemented Parcelable to my single item template/class, and now when I click on a card I get that second activity that shows the full sized image, title and full description text.
I also added a search that filters the cards in real time, since there will be many of them and I want it to be easy to get to a specific item.
So far so good. Everything works, surprisingly.
Where I'm stuck now is that it will be extremely annoying for somebody to keep going in and out of the single items in order to get to the next one, so I need to be able to either make them scroll or have buttons at the bottom that go to previous and next. The thing is, the only time I've used swiping is in a tabbed activity and that requires fragments. When making those I had to have an xml for each tab, and a class to correspond to it, which is definitely not comfortable when I have 40 in 10 categories.
I assume I need something like an onClick event to add for buttons in the activity that holds the information that gets acquired from the RecyclerView to bring a specific position from my ArrayList... Like, if somebody clicks on the third card, the button brings -1 or +1. Or something like that?
Makes some sense in my head, but I can't find a way to search for that, and can't figure out if it's even possible to make. Soo any pointers would be hella useful. I'm really excited about being able to do that much from scratch, but still lack too much knowledge to be able to imagine what would work here. I'm still testing things out and am looking around for information, but will be happy if somebody has input on the situation.

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

Java Netbeans: How to layer panels on top of each other?

Please bear with me as I've just started using NetBeans for the first time! Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time. It'll start with one panel, and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I've tried looking into utilizing LayeredPanes since that's what I've come after hours of researching this only.. I don't understand how to do it! I think using different panels would be much easier than using different frames, so that's why I'm just going to stick with layering panels.
If anyone could explain LayeredPanes, I'd be very grateful! I'm not sure my coding will help here, but if anyone needs it I'll put it up.
This tutorial should point you in the right direction, however, if you want to have items positioned over each other, you might also want to take a look at the CardLayout:
The CardLayout class manages two or more components (usually JPanel
instances) that share the same display space.
Reading your question I dont think that what you need is a LayeredPane,
Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time.
Tell exactly what you want to achieve, I mean on what you are working on.
...and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I'd like to suggest a JDialog , show dialogs depending on the user inputs. To make sure if this is what you need, you want to provide us with more information. :)

Slick2D Movement Issue

So, I converted a game to Slick2D. The movement is broke, and I am at a loss. Before, we used KeyPressed and keyReleased methods, but now with Slick2D movement isn't working right.
Yea, nothing has gone right with converting to Slick2D. First the launcher, which I had a help topic on before, and now this. Though, the other topic was an issue with WebStart hating code.
You can only move right, using A. And you can't stop moving. Am I using the right methods? How can I fix it? Any help is greatly appreciated!
Here is a PasteBin link to the code, if it helps! http://pastebin.com/GRH86Yuw
I'm a fan of Slick, and I'd be happy to help.
The fundamental difference is that Slick is a polling model, not an event-driven model when it comes to input. Basically, in your logic update method you loop through the keys bound to your events, and check to see if any of the keys are currently pressed, and then trigger those events. For a number of reasons that I can go into if you like, polling tends to work better for games, especially with a large number of keys. It's just a different way of doing things, an not that complicated. The biggest upside is that you get centralized input processing a single method, instead of having it spread across multiple KeyListener instance objects.
If you want to look at Pedestrians - a simple pedestrian sim implemented in Slick - you can see an example of how to handle input in Slick.
Specifically, I handle input in this file (lines 192-295), inside the processInput method. Basically, you pass in a reference to the GameContainer object (the Slick object that contains your game), and from that you can get an instance to the Input instance that will allow you to check which keys are pressed, what mouse buttons are clicked, etc.

Java | I need to make an applet but I don't know how to begin

I need to make kind of a game in Java but I am a total beginner. It has to be applicable to the web and I need to use images on it and action listeners. So, do you recommend any site to know how to begin?
The description of the game (it is not really a game but it implements things that usually are in a game) is this:
Show a matrix of images of 3x3 elements, then, hide them and put instead empty squares. The images shown in the matrix, must remain in the lower part of the screen just below the empty squares and they must be randomly positioned. The user, must click one image and put it on the correct empty square. The result, must be, how many images were correctly positioned, the time it took to end the game, the time between mouse clicked and released for each image.
For additional information, I want you to know that this application is for a friend of mine who studies medicine. He wants this program to test patients who accident and receive hits on their heads. You may think that the description I gave you may not be a good software for that purpose, and in fact, it may be not, but, once I know the management of all that is required (Images, MouseListeners, how to introduce it to a web etc), I will be able to make a better product. So, please tell me, how can I begin?. What do I need to know?
I would start here. Except for some startup boilerplate and the restrictions of the sandbax (which, based on your description, you will b unlikely to encounter), there is no fundamental difference in an applet from normal code.

Categories

Resources