Results 1 to 14 of 14
  1. #1
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default adding JList and JButton to a TabbedPane

    Hi,

    Can someone tell me how I can add a JButtons or JLists on a Tabbedpane. I'm using the code below from the java tutorial but when use the add(list) then it gets added on a different tab.


    String[] data = {"one","two","three"};
    JComponent panel1 = makeTextPanel("Panel #1");

    tabbedPane.addTab("Tab 1", icon, panel1,
    "does nothing");
    tabbedPane.add(list);
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);


    Thanks !!!!.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You would add a JPanel to the JTabbedPane (not a JComponent), and you add the other components (JButton, JLIst) to this same JPanel.

  3. #3
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks for that again. Question about loading/populating my JList with the keys from my hashMap. If I use the keyset how I can add this into the JList if the JList only accepts Arrays.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have the JList use a DefaultListModel and iterate through the Set (it's iterable), adding to the model as you go.

  5. #5
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    thanks again!!!. Very very usefull tips you have given me. I'll try the DefaultListModel .Thanks...

  6. #6
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Actually I wonder if I can ask another question. Now I'm adding multiple JList's and JButton's to each JPanel but even though I'm specifying the Alignments (north,west,east and so on) they are all going to the centre. Also I need to add JLabel's on top of these Jlist's but not sure how I can put them where I want them. Thanks for your valuable help!!.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Quote Originally Posted by hariza View Post
    Actually I wonder if I can ask another question. Now I'm adding multiple JList's and JButton's to each JPanel but even though I'm specifying the Alignments (north,west,east and so on) they are all going to the centre.
    Use an appropriate layout manager. Only BorderLayout has NORTH, WEST etc.
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Also I need to add JLabel's on top of these Jlist's but not sure how I can put them where I want them.
    Are your JLists in JScrollPanes (if not, they probably should be). If they are,
    JScrollPane (Java Platform SE 6))

    db

  8. #8
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks Darryl for your message. I wonder if I need to use a layout manager where I can specify all by hand without relying on Netbeans which one would be the ideal one. I have to fit only 5 Jlist and the same number of JLabel on a JPanel.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    You can get by without a layout manager, if you want to make your code difficult to maintain.

    db

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Darryl.Burke View Post
    You can get by without a layout manager, if you want to make your code difficult to maintain.
    db
    Heartily seconded!

  11. #11
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Actually I managed to create the GUI using BorderLayout but I have another question. It might sounds silly but I'm using a multiple JList's and using listModel to load the items into the JList but what I did all this basically adding the data Structure (HashMap) in the same class as the GUI class so Hashmap is visible/accesable but now I wonder because I have a bunch of HashMaps define on my driver class what do a need to do so I can load all my JList's. So to recap I have my GUI class and I have a DRIVER class where all my data structures are defined. What do I need to do to make then visible to the GUI class. Thanks!!!. Sorry for my ignorance but my OO skill are very poor.Thanks again.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Search the web for MVC, it looks like your application has just attained the complexity that requires a systematic break-up.

    luck, db

  13. #13
    hariza is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks again Darryl. I'll have a look. Now I have another question. I wonder why I have created my TabbedPane succefully and in one of the TabbbedPane I have added JPanels. I wonder why the size of the JPanel remains so small even I have specified to make it bigger size. Where do I have to specify the size so I can make the containers inside the TabbedPane bigger. Thanks!!!

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Adding to JList with Netbeans
    By kewlgeye in forum NetBeans
    Replies: 7
    Last Post: 03-28-2012, 08:39 PM
  2. Adding \ removing items from Jlist
    By Desperado in forum AWT / Swing
    Replies: 10
    Last Post: 12-17-2009, 01:48 PM
  3. Adding JButton to a JTable
    By ting.at.net@hotmail.com in forum AWT / Swing
    Replies: 6
    Last Post: 05-26-2009, 04:37 AM
  4. adding Image to JButton
    By mayhewj7 in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 04:39 AM
  5. JList and JButton event handler not working
    By H3rtaherta in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2008, 01:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •