Reputation: 1229
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
Upvotes: 0
Views: 1121
Reputation: 368
You my need to call the revalidate() method on the container of the new created component.
Upvotes: 0
Reputation: 104196
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:
Upvotes: 3