1

everytime i click on a JList item, i need to clear + refresh my current panel & load another panel, returned via method 'populateWithButtons()'. temp is an int variable that stores what was clicked at the JList. How do i rectify the following?

        list_1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {

                //refresh + populate JPanel
                Food food = new Food();             
                JPanel panel2 = new JPanel();
                JPanel pane11 = new JPanel();


                panel2.add(panel1);
                panel1.validate();
                panel1.repaint();
                panel1.setBounds(153, 74, 281, 269);

                panel1.add(food.populateWithButtons(temp));             
                contentPane.add(panel2);
        }
brainsfrying
  • 241
  • 1
  • 6
  • 21

2 Answers2

3
  1. don't to use NullLayout

  2. add ListSelectionListener to JList instead of MouseListener, otherwise you would need to convert point from mouse to Item in JList

  3. use CardLayout instead of add, remove JPanels on runtime, then selection from ListSelectionListener (ListSelectionModel to SINGLE...) to switch prepared card (JPanel with some contents)

EDIT

enter image description here

.

enter image description here

.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class CardlayoutTest {

    private Color[] colors = new Color[]{Color.BLACK, Color.RED, Color.GREEN, Color.BLUE};
    private JFrame frame = new JFrame();
    private JList list = new JList();
    private JPanel panel = new JPanel();
    private CardLayout card = new CardLayout();

    public CardlayoutTest() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setLayout(card);
        Vector<String> items = new Vector<String>();
        for (int x = 0; x < colors.length; x++) {
            JPanel pnl = new JPanel(new BorderLayout());
            pnl.setBackground(colors[x]);
            panel.add(pnl, colors[x].toString());
            items.add(colors[x].toString());
        }
        list = new JList(items);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    String card = list.getSelectedValue().toString();
                    CardLayout cL = (CardLayout) (panel.getLayout());
                    cL.show(panel, card);
                }
            }
        });
        frame.add(new JScrollPane(list), BorderLayout.WEST);
        frame.add(panel);
        frame.setPreferredSize(new Dimension(400, 150));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CardlayoutTest();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • have heeded ur advice & used ListSelectionListener. How may I implement your 3rd advice? – brainsfrying Oct 22 '13 at 07:26
  • 1
    Item in JList represents card (JPanel) or vice versa you have 10 separate views, create JPanel for each of views, put this JPanel as Card to CardLayout, it has String description for CardLayout, then put the same String as a new Item to JList, ListSelectionListener returns String, pass this String value as parameter for CardLayout, linked tutorial shows JComboBox, this is the same as for JList, – mKorbel Oct 22 '13 at 07:33
  • seems complicated.all i wanted was the JPanel to refresh itself.thank you anyway i appreciate your advice, which may not be applicable at this point in time. – brainsfrying Oct 22 '13 at 07:57
  • I think that is job for a few minutes, hmmm youll kill my endless lazyness, wait – mKorbel Oct 22 '13 at 08:03
  • simpler as is possible – mKorbel Oct 22 '13 at 08:24
  • +1 `How may I implement your 3rd advice?` Did you read the tutorial? If so then ask a specific question about what you don't understand. The tutorial has a working example that does almost exactly what you want. Don't expect us to spoon feed you the answer. – camickr Oct 22 '13 at 15:23
0

move validate() and repaint() after adding to contentPane as in that point it will be redrawed.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • have tried. am sorry but it doesnt work out. i understand im working on the GUI directly. Will u please provide a short example? – brainsfrying Oct 22 '13 at 07:20