Question

i want the user to click on a image and the panel with cardlayout changes for every image clicked. so i have one panel with a textarea and one with just a blue backgound, when i start the program the panel with the textarea show without textarea, when i click the image it show the blue panel, why cant i see the textarea?

i have removed the location of the image in the code

frame = new JFrame("Sandwich deLuxe");
    frame.setBounds(100, 100, 741, 522);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBounds(10, 132, 705, 89);
    frame.getContentPane().add(panel);
    panel.setLayout(null);



    JToolBar toolBar = new JToolBar();
    toolBar.setBounds(10, 0, 705, 22);
    frame.getContentPane().add(toolBar);




    panelCont.setBounds(10, 221, 544, 251);
    frame.getContentPane().add(panelCont);
    panelCont.setLayout(cl);

    JPanel panelTest1 = new JPanel();
    JTextArea txtrGhg = new JTextArea();
    txtrGhg.setForeground(Color.BLACK);
    txtrGhg.setBackground(UIManager.getColor("Button.background"));
    txtrGhg.setEditable(false);
    txtrGhg.setText("Velkommen til Sandwich deLuxe\r\n\r\nK\u00F8b din mad  her!\r\n\r\n1. V\u00E6lg en af kategoriene fra oven.\r\n2. V\u00E6lg dinne  retter/sandwichs.\r\n3. Bekr\u00E6ft k\u00F8bet i indk\u00F8bskurven.\r\n4. Din bestilling  er modtaget og vi g\u00E5r straks \r\n   i gang med at tilberede din mad. ");
    txtrGhg.setFont(new Font("Monospaced", Font.PLAIN, 18));
    panelTest1.add(txtrGhg);
    panelCont.add(panelTest1, "1");

    JPanel panelTest2 = new JPanel();
    panelTest2.setBackground(Color.BLUE);
    panelCont.add(panelTest2,"2");
    cl.show(panelCont, "1");

    JLabel lblNewLabel = new JLabel("New label");
    lblNewLabel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            cl.show(panelCont, "2");
        }
    });
    lblNewLabel.setIcon(new ImageIcon(""));
    lblNewLabel.setBounds(28, 11, 97, 67);
    panel.add(lblNewLabel);



    JScrollPane scrollBar = new JScrollPane(txtrGhg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollBar.setViewportBorder(null);
    scrollBar.setPreferredSize(new Dimension(300,200));
    scrollBar.setBorder(BorderFactory.createEmptyBorder());
    panelCont.add(scrollBar, "name_171726698118247");
    panelCont.setOpaque(true);
Was it helpful?

Solution

At the end of your code you create a JScrollPane thwarting the setup you have created before. You pass txtrGhg in JScrollPane’s constructor. So the scroll pane reparents your text area as your JTextArea cannot be contained in two different containers at the same time. After that, panelTest1 does not contain your JTextAreatxtrGhg” anymore but the CardLayout will still show panelTest1 as you have requested. But it’s empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top