some swing questions:
whenever i add a JPanel to a JTabbedPane, the JPanel takes up the whole JTabbedPane area. i wanted my layout to look like this: http://img292.imageshack.us/my.php?image=tab1co0.png and when i added another JPanel it looked like this: http://img512.imageshack.us/my.php?image=tab2ki9.png right now this is how it looks when i add a JPanel to the tab: http://img178.imageshack.us/my.php?image=tab3fu6.png as you can see the JPanel takes up the WHOLE tab :(
[461 byte] By [
Alex1989a] at [2007-11-26 19:02:52]

# 2
Do you use tabbedPane.add(panel, index);to add your JPanel to the JTabbedPane?
# 3
im using tabs.setComponentAt(index, JPanel);
# 4
With this you set the component of the JTabbedPane at index. If there was a JPanel before the new panel will replace the old one.
You could try this:
1. create JPanel (mainPanel) for tabbedPane and set its layout to BoxLayout
2. create first panel and add it to your mainPanel
3. create second panel and add it to your mainPanel
4. etc.
5. tabs.setComponentAt(index, mainPanel);
Hope this helps!
# 5
[url http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html]How to Use Layout Managers[/url]. I would guess you are using a GridLayout. Try using a BoxLayout.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
# 6
Thanks for your help so far guys, appreciate it. Now im wondering if i should be using a JPanel or something else, because it seems as though a JPanel takes up all the space that it is allowed (unlike a JButton in a BoxLayout). when i try to add a second JPanel to my JTabbedPane it overwrites the first one and does not go under it. Any other component that i should try using instead of a JPanel?
# 7
You are not very clear what you want. If you should use a JPanel depends on your requirement. If you want to see two or more JPanels in your TabbePane just read my first answer. If it is something else, say clearly what it is.
# 8
> > because it seems as though a JPanel takes up all the space that it is allowed No. It depends on the Layout Managers you are using.Thats why I gave you the link to the tutorial and asked for a SSCCE. We can't give specific advice to a general question.
# 9
ok this is the essential code for my class that is used for the contacts in my phonebook:
public class ContactPanel extends JPanel{
private String contactName, contactNumber, contactEMail;
private JLabel contactNameLabel, contactNumberLabel, contactEMailLabel;
public ContactPanel(String contactName, String contactNumber, String contactEMail){
this.contactName = contactName;
this.contactNumber = contactNumber;
this.contactEMail = contactEMail;
contactNameLabel = new JLabel(contactName);
contactNumberLabel = new JLabel(contactNumber);
contactEMailLabel = new JLabel(contactEMail);
add(contactNameLabel);
add(contactNumberLabel);
add(contactEMailLabel);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
}
}
i then use it like this (users enter their name, number and e-mail address using a JDialog and press the "Add" button to add their "profile" to the phonebook):
if(event.getSource() == addButton){
tabs.setComponentAt(1, new ContactPanel(nameField.getText(), numberField.getText(), eMailField.getText()));
}
the problem is that the ContactPanel takes up all the space in the JTabbedPane that it is added to, and when i add another ContactPanel (by adding another contact to the phonebook), the 2nd one overwrites the 1st one.
1st ContactPanel added to JTabbedPane: http://img247.imageshack.us/my.php?image=tab4wn6.png
2nd ContactPanel added to JTabbedPane: http://img87.imageshack.us/my.php?image=tab5ws1.png
this is what i want to happen when i add a 2nd ContactPanel to the JTabbedPane: http://img87.imageshack.us/my.php?image=tab6zy8.png
so how do i make the ContactPanels take up the bare minimum amount of space needed, so that they will be placed underneath each other, instead of them taking up the whole JTabbedPanes area? is there a way to explicitly set the width and height of a JPanel? ive tried setPreferredSize(), setSize() and setBounds() and none of them have an effect the the ContactPanels.
# 10
You can only add one ContactPanel to your tabbedPane. If you want more Panels on your tabbedPane you will need one Panel that you add to the TabbedPane to which you add your ContactPanels (as I suggested above). I recommend to use BoxLayout for the Panel to which the ContactPanels are added. And I also recommend to read the tutorial about layout-Management (see the link camickr provided above).
Message was edited by:
the12hunters
# 11
> ok this is the essential code for my class that is used for the contacts in my phonebook:Still not a SSCCE. Still no help.
# 12
k thnx the12hunters u solved part of my problem :)Message was edited by: Alex1989
# 13
Thats nice! Which part did I solve and which part is not solved still?
# 14
> Thats nice! Which part did I solve and which part is
> not solved still?
u solved the part where my ContactPanels took up all the space in the JTabbedPane, now they go underneath each other, thnx dude.
my next problem is when i try to add a ContactPanel to a certain JTabbedPane using add(Component comp, int index)
the ContactPanels always get added to the Z tab because im using a for loop to create my tabs from A-Z.
# 15
Alex, we can't help you if you are not more specific. How should we know what is going wrong if we can't see your code? There is nothing wrong with add(Component comp, int index) and a for-loop. So try to create an SSCCE and we will see.
# 16
k sorry guys for being unclear. this is the loop used to make my tabs from A-Z:
for(index = 65; index <= 90; index++){
tabHeadingLabel = new JLabel(" " + String.valueOf((char)(index)));
tabHeadingLabel.setFont(new Font("Helvetica", Font.BOLD, 50));
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.add(tabHeadingLabel);
AZTabs.addTab(String.valueOf((char)(index)), mainPanel);
}
and this is the code used to add a ContactPanel to the "A" tabs JPanel at index 1:
mainPanel.add(new ContactPanel(nameField.getText(), numberField.getText(), eMailField.getText()), 1);
mainPanel.validate();
even though i add it to index 1, my ContactPanels always get added to the "Z" tabs JPanel (the last tab created in the loop).
# 17
Well, this is no SSCCE so I can only guess what is going on.
mainPanel.add(new ContactPanel(nameField.getText(), numberField.getText(), eMailField.getText()), 1);
adds a ContactPanel to your mainPanel at index 1. I think you are confusing the mainPanel and the tabbedPane. This is the index for mainPanel not for the tabbedPane.
I guess mainPanel is global and it is equal to the last JPanel you added in your for-loop, because this was the last assignment you did with mainPanel. So it is quite understandable that your ContactPanel is added to Z and not to A.
What you should do is to store your mainPanels in a List or Set (see package java.util). Which one to choose depends on what you want to do with your mainPanels later. But I would use a HashMap and add every mainPanel with the key A for the A-panel, B for B-Panel etc.
When you have to add another ContactPanel you get the appropriate mainPanel out of your HashMap and add the new ContactPanel to this mainPanel.
# 18
ok u guys are probably get really annoyed with me but i read a tutorial on HashMaps and im still REALLY confused. do u mean i do something like this:
HashMap map = new HashMap();
map.put("A-Panel", mainPanel);
map.put("B-Panel", mainPanel);
map.put("C-Panel", mainPanel);
map.put("D-Panel", mainPanel);
map.put("E-Panel", mainPanel);
map.put("F-Panel", mainPanel);
map.put("G-Panel", mainPanel);
map.put("H-Panel", mainPanel);
map.put("I-Panel", mainPanel);
map.put("J-Panel", mainPanel);
map.put("K-Panel", mainPanel);
map.put("L-Panel", mainPanel);
map.put("M-Panel", mainPanel);
map.put("N-Panel", mainPanel);
map.put("O-Panel", mainPanel);
map.put("P-Panel", mainPanel);
map.put("Q-Panel", mainPanel);
map.put("R-Panel", mainPanel);
map.put("S-Panel", mainPanel);
map.put("T-Panel", mainPanel);
map.put("U-Panel", mainPanel);
map.put("V-Panel", mainPanel);
map.put("W-Panel", mainPanel);
map.put("X-Panel", mainPanel);
map.put("Y-Panel", mainPanel);
map.put("Z-Panel", mainPanel);
# 19
You would do something like this:
this.map = new HashMap();
for(index = 65; index <= 90; index++){
tabHeadingLabel = new JLabel(" " + String.valueOf((char)(index)));
tabHeadingLabel.setFont(new Font("Helvetica", Font.BOLD, 50));
JPanel mainPanel = new JPanel();
map.put(" " + String.valueOf((char)(index)), mainPanel);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.add(tabHeadingLabel);
AZTabs.addTab(String.valueOf((char)(index)), mainPanel);
}
and
String key = <theKeyWhereYouWantToAddContactPanel>;
JPanel mainPanel = (JPanel )map.get(key);
mainPanel.add(new ContactPanel(nameField.getText(), numberField.getText(), eMailField.getText()), 1);
mainPanel.validate();
There might be some typos but this should point you in the right direction.