I'm developing a Java application that displays a counter, that sums one to itself every time a pixel in a certain position is equal to a color after a certain action. The number from the counter is obtained from a text file, and gets updated on close.
I'm quite new into Swing so I'm a bit confused on how should I manage the small version of my app, which is basically the same but smaller duh and always on top.
Since according to this thread making new JFrames is not ideal, especially since this application just shows an Integer in the end.
My thought was to make them as different classes that extend from JPanel. Is this okay? I'm a bit confused on how should I make the switch back and forth if so.
big version idea:
small version idea:
I don't see why making a small JFrame would be bad. Also, you cannot display anything on a JPanel without either a JFrame or a JWindow.
My final verdict is that it would be just fine to use a small JFrame, you aren't taking performance hits or anything like that.
Related
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.
Im making a small program that shows teams/ players/ then their twitter feeds, tweets, etc. My problem seems to be unrelated to that though, it just my jPanel isn't updating. I got pretty far in the project, and i keep throwing random code segments in my method to make it so once i click something, the "left" jpanel either is replaced with the jbuttons, or the jbuttons are put into it, ive tried both. Heres the code segment ive been trying to get to work, which it does, just not as intended.
public void mousePressed(MouseEvent e) {
System.out.println("You clicked on " + ap.getTeams() [addPlayers.OPTIC].getTeamName());
for(int i = 0; i<4; i++){
//JPanel temp = ap.makePanel(ap.getTeams()[ap.OPTIC].getPlayers().get(i).getTwitterScreenName());
//temp.setBounds(0,(i*125), 450,125);
//left.add(temp);
JButton b = new JButton("Test");
b.setBounds(30,30,30,30);
left.add(b);
left.revalidate();
add(left);
add(b);
right.add(b);
left.setVisible(false);
left.setVisible(true);
System.out.println(i);
}
}
}
the commented out code is my main code, but i am just using jbuttons to test. As you can see, i just keep adding random bits, hoping something will work. Ive also tried invoke later, but sadly, that didn't work either.Not sure why the jpanels not updating, but any feedback would be great, thanks.
Avoid null layout and setBounds since this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
The best way to swap JPanels or any component is to use a CardLayout.
If still stuck, then post a minimal example program. It's the best way for us to actually see and experience your problem.
Don't add a component to more than one container like you're doing with your b JButton variable. You're in fact adding it to three containers.
You seem to be trying to add four JButtons to containers, giving them all the same bounds, one exactly on top of the other?? Sorry, but the more I look at your code, the more screwy it becomes. Again, stop this nonsense, learn about the layout managers at the Swing tutorials, and use them.
"and i keep throwing random code segments in my method to make it so ..." -- throwing things at the wall to see what sticks is not a good heuristic for creating a program. Start with the knowledge base -- here the Swing tutorials and Java API, then plan your program structure, and then create your code.
Edit
You ask:
..... All im asking is whats the problem with the whole thing not updateing?
If you don't use a CardLayout and change components manually, then you would need to call revalidate() and repaint() on the container after the change, and the container should update with its new components. If this does not fix the problem, then likely you have an issue in code not shown us, and you will again then want to create and post a minimal example program. Again it's the best way for us to actually see and experience your problem.
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
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. :)
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.