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.
Related
I am currently programming a little tool in java with swing elements.
I want an external window to pop up when clicking on a menu button. Now im questioning what the best/a good way to implement that?
My current solution is a JOptionPane with a few JLabels in it.
public void optionpane_about_ShowDialog() {
JComponent[] testArray = {
label_about_versionAutor, label_about_links};
optionpane_about.showMessageDialog(this, testArray, "Info", optionpane_about.INFORMATION_MESSAGE);
}
I'm fine with that solution, despite it maybe being a bit odd.
But i have one problem that i don't know a solution to, with that way:
The labels included allow me to have a border around them, but only around one label, not two (If it's possible, it would be nice to know how ;) )
I read about solutions on how to make an info pop up window, from OptionPanes to JFrames and so on.
What is the best possible solution?
Greetz. Julian
Any parameter to set A JFrame's border/frame thickness or existence and still keep the title bar intact? I want an almost borderless frame with a thin blue line like this one and not like the default border.
If JFrame isn't the way to go, what is a good way to achieve that? (preferably that is compatible with WindowBuilder but that's probably asking for too much).
A search barely yields any mention and related questions on SOF don't seem to have answers so I thought I'd try to get a good answer once and for all.
JFrame#setUndecorated
Disables or enables decorations for this frame.
This method can only be called while the frame is not displayable. To make this frame decorated, it must be opaque and have the default shape, otherwise the IllegalComponentStateException will be thrown. Refer to Window.setShape(java.awt.Shape), Window.setOpacity(float) and Window.setBackground(java.awt.Color) for details
Please, consult the available documentation
Please note, you will become responsible for providing the title bar yourself, should you want it
A search barely yields any mention and related questions on SOF don't seem to have answers
Google provides a number of promising hits
I ended up switching to NetBeans and learning some Photoshop basics which you'll need thanks to a comment by #MadProgrammer
writing your own look and feel delegate
and ended up exactly with what you mentioned #theProgrammer101
You can make a JButton, and when it is clicked, call System.exit(0) , which will terminate the program
You can create a similar button for minimize action as well as your own drop down menus that are totally custom made and you won't need to rely on the default JFrmae window in case that bothers you too (I found it horrid).
check out this link for a good NetBeans tutorial with an nice example of writing your own look and feel delegate and this link for a great tutorial on getting started with Photoshop which is critical to GUI creation.
Thought i'd round up some of my research for anyone else who's just getting into GUI's.
I'm trying to work out how best to approach creating a program in java where I have a very simple GUI where a user can drop in multiple images and multiple text boxes move these around by dragging, resize, maybe a couple of other very simple features and then output a jpg for instance.
I have been looking around and all the program's I can find seem to be much more complex.
I have been trying to do this using java swing just creating a GUI that when you press a JButton inserts a picture into a JLabel, but I'm thinking this kind of thing must be the wrong approach and there must be a better way.
Any ideas and suggestions would be much appreciated...
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. :)