Results 1 to 1 of 1
- 05-25-2012, 03:15 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 31
- Rep Power
- 0
display ImageIcon in JList when tab is changed.
I have a swing app that has several tabs. It has a Jlist in which I want to display a different image each time the tab is changed. When I run the app, the image is displayed that corresponds to the first tab. However when I click another tab, the image stays the same, rather than change.
Here is where I create the tabs and have the tab change listener.
Here is where I have the Jlist where I try to load the different images.Java Code:public MainFrame() { setTitle("My Game Collection"); Image ltreeImage = Toolkit.getDefaultToolkit().getImage( "images/ltree_icon.gif"); setIconImage(ltreeImage); Container container = this.getContentPane(); container.setLayout(new BorderLayout()); tabbedPane = new JTabbedPane(); snes = new GamePanelGUI(new JFrame()); tabbedPane.addTab("Snes", snes); nes = new GamePanelGUI(new JFrame()); tabbedPane.addTab("NES",nes); sega = new GamePanelGUI(new JFrame()); tabbedPane.addTab("SEGA", sega); sega32x = new GamePanelGUI(new JFrame()); tabbedPane.addTab("SEGA32X", sega32x); sms = new GamePanelGUI(new JFrame()); tabbedPane.addTab("SMS", sms); container.add(BorderLayout.CENTER , tabbedPane); setSize(500, 400); setLocation(100, 100); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tabbedPane.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent evt) { JTabbedPane tabbedPane = (JTabbedPane) evt.getSource(); sel = tabbedPane.getSelectedIndex(); title = tabbedPane.getTitleAt(sel); System.out.println("Tab Changed to." + title); img = new GamePanelGUI(new JFrame()); } }); }
Java Code:public GamePanelGUI(JFrame theParentFrame){ ... gameListBox = new JList(); gameListBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); musicScrollPane = new JScrollPane(gameListBox); //set the cell renderer gameListBox.setCellRenderer(new ImageListCellRenderer()); if(gameListBox.getModel().getSize() <1) { int x = MainFrame.sel; if(x == 0){ snesIcon = (new ImageIcon("C:\\Users\\mfierro\\Desktop\\crs471\\SourceCode\\Solution - ex71\\images\\snes.jpg" )); System.out.println(snesIcon); snesLabel = new JLabel(snesIcon,JLabel.CENTER) ; snesPanel = new JPanel(); snesPanel.add(snesLabel); gameListBox.setListData(getPanel().toArray()); } if(x == 1){ nesIcon = (new ImageIcon("C:\\Users\\mfierro\\Desktop\\crs471\\SourceCode\\Solution - ex71\\images\\nes.jpg" )); System.out.println(nesIcon); nesLabel = new JLabel(nesIcon,JLabel.CENTER) ; nesPanel = new JPanel(); nesPanel.add(nesLabel); gameListBox.repaint(); gameListBox.setListData(getPanel().toArray()); } } } public ArrayList<Object>getPanel(){ ArrayList<Object> obj = new ArrayList<Object>(); int y = MainFrame.sel; System.out.println("Value of sel in getPanel:" + y); if (MainFrame.sel == 0){ Object item = snesPanel; obj.add(item); } if (MainFrame.sel == 1){ Object item = nesPanel; obj.add(item); } System.out.println("The # of objects is:" + obj.toArray()); System.out.println("This is in logoPanel" + obj.toString()); return obj; } public String getImgName(){ System.out.println ("In getImg method. tab value is:" + MainFrame.sel); String imgName = ""; if(MainFrame.sel == 0){ // categoryComboBox.setSelectedIndex(0); imgName = "snes.jpg"; return imgName; } if(MainFrame.sel == 1){ imgName = "nes.jpg"; return imgName; } else{ return imgName; } }
Here is the ListCellRenderer
Java Code:class ImageListCellRenderer implements ListCellRenderer { public Component getListCellRendererComponent(JList jlist, Object value, int cellIndex, boolean isSelected, boolean cellHasFocus) { if (value instanceof JPanel) { Component component = (Component) value; component.setForeground (Color.white); component.setBackground (Color.white); return component; } else { return new JLabel("???"); } } }
Similar Threads
-
how to display a video from a JList?
By mathidioticz in forum AWT / SwingReplies: 3Last Post: 01-30-2012, 11:40 AM -
Collection to JList to Display
By D.Calladine in forum New To JavaReplies: 1Last Post: 12-03-2010, 12:53 PM -
[SOLVED] JButton does not display ImageIcon properly
By Singing Boyo in forum New To JavaReplies: 1Last Post: 04-17-2009, 03:47 AM -
[SOLVED] JList( Vector ) Display problem
By logicbug in forum AWT / SwingReplies: 4Last Post: 03-21-2009, 02:28 PM -
How do I display a JList with icons and text?
By aneesahamedaa in forum AWT / SwingReplies: 2Last Post: 09-04-2008, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks