tags:

views:

336

answers:

2

Hey Stack Overflow folks,

I have been trying to get code working where i can double click on an item in a JList and it creates a new JList on a different place on the Frame from scratch of all the object names of items that belong to that category (this is useless info i guess). But the problem is when i double click on the items in the list, it runs through the code to add a component to the JFrame but it just never shows up, is this because i am using a mouse event to build it after run time or something?

My Gui Class is:

public class MediaGUI extends JFrame

and the adding code happens here

    _mediaList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent event) {
            if (event.getClickCount() == 2) {
                CreateObjectPane(_mediaList.getSelectedValue().toString(););
            }
        }
    });

and here is the building code, this method belongs to MediaGUI, so this. is referring to a JFrame

 private void CreateObjectPane(String category)
 {
     /*=======================================================================
     * Create ther list on Objects that belong to each category
     * Also a scroll bar for the list
     *=======================================================================*/
    String objects[] = _mediaHandler.GetObjects(category);
    _mediaList = new JList(objects);
    _mediaList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    _mediaList.setLayoutOrientation(_mediaList.VERTICAL);
    _mediaList.setVisibleRowCount(-1);

    _mediaScrollPane = new JScrollPane(_mediaList);
    _mediaScrollPane.setPreferredSize(new Dimension(100,100));
    _mediaScrollPane.setAlignmentX(CENTER_ALIGNMENT);

    _mediaPanel = new JPanel();
    _mediaPanel.setLayout(new BoxLayout(_mediaPanel, BoxLayout.PAGE_AXIS));
    _mediaLabel = new JLabel("Media Objects");
    _mediaLabel.setLabelFor(_mediaList);

    _mediaPanel.add(_mediaLabel);
    _mediaPanel.add(Box.createRigidArea(new Dimension(0, 15)));
    _mediaPanel.add(_mediaScrollPane);
    _mediaPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    this.add(_mediaPanel, BorderLayout.CENTER);
}

Any help you could offer would be great, if any more explanation is needed i am happy to require it

+3  A: 

From the documentation:

Note: If a component has been added to a container that has been displayed, 
validate must be called on that container to display the new component. 
If multiple components are being added, you can improve efficiency by 
calling validate only once, after all the components have been added.

Some other tips:

  • Rename CreateObjectPane to createObjectPane
  • What LayoutManager are you using? Try to experiment with it, since most of the times the new component wont appear where you expect it to be.
kgiannakakis
Thank you very much, me thinks i need to read more.
Craig
revalidate is probably better (but is not in AWT). Call it as many times as you like, and it'll just do the validate once when an event gets through the event queue.
Tom Hawtin - tackline
A: 

You my need to call the revalidate() method on the container of the new created component.

Xavier Young