Boxlayout stretch my Jpanels
Hey (: This is my first time asking help on this forum so I hope I have done everything correctly
Okay my problem is what I have marked with red. My JPanel uses a Boxlayout with BoxLayout.Y_AXIS orientation. I guess that is not the right choise. I don't want my JPanel(online users), my JSeperator and my other JPanel(admin) to be apart of eachother. I want them to stack directly under eachother.
Pic of GUI: http://dl.dropbox.com/u/10049103/notcool.png
I have heard that I should be able to use some kind of "glue" but could not get that to work as intended. I also heard that I had to use GridBagLayout but that seems very complicated and I would really like if there is an easier way.
Here is the code for the marked JPanel (don't know if needed):
package Client;
import Server.User;
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
/**
*
* @author Sune
*/
public class OnlineUsersPanel extends JPanel {
private UserWindow owner; public void setOwner(UserWindow uw) { owner = uw; }
private HashMap<String, ShowUserPanel> userPanels = new HashMap<String, ShowUserPanel>();
public OnlineUsersPanel() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(addStart());
this.add(new JSeparator());
System.out.println("OnlineUsersPanel con called");
}
void addUser(User user) {
ShowUserPanel newPanel = new ShowUserPanel(owner, user.getUserName());
userPanels.put(user.getUserName(), newPanel);
this.add(newPanel);
owner.updateGUI();
}
void deleteUser(User user) {
ShowUserPanel toRemove;
for (Iterator i = userPanels.keySet().iterator(); i.hasNext();) {
String tempUserName = (String) i.next();
if (tempUserName.equals(user.getUserName())) {
toRemove = userPanels.remove(tempUserName);
this.remove(toRemove);
System.out.println("Fundet og fjernet");
break;
}
}
}
private JPanel addStart() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Online Users:"), BorderLayout.LINE_START);
p.add(new JLabel("mute:"), BorderLayout.LINE_END);
return p;
}
I hope someone can le开发者_StackOverflow中文版arn me how to use that glue properly or tell me the good way to do this (:
Edit: changed to an answer:
The panels are right on top of each other. The question is what kind of layout is being used by the container holding the two JPanels? Also are you doing to display many users? Do you need a JList or JTable there? Can you show a picture of what you want the gui to look like, and show it when there are a typical number of users. Also consider creating and posting an SSCCE. This would take a bit of work on your part, but would be well worth it.
Edit 1:
On further review of your image, I suggest that you use a JTable here with "Online Users" and "Mute" as your column headings and with the stuff you currently show below, the Admin and the checkbox as a row of the JTable. Put it in a JScrollPane and display it where you're currently displaying it.
after adding the two panels to the boxlayout do
add(Box.createVerticalGlue());
this will shrink those two panels to their preferred sizes, and put the space below them.
精彩评论