0

I have a single class extended from JFrame. I have multiple JLayeredPanes inside it. I want to switch from one JLayeredPane to another. For that what I does is;

  1. remove the current JLayeredPane like remove(pane1);
  2. repaint();
  3. add components to new JLayeredPane and set bounds
  4. add(pane2)

This removes my current JLayeredPane and shows next JLayeredPane. But the components are not added in correct position. For example, I have a jscrollpane containing a textarea. Only the scroll pane is appearing and text area is missing. Also, the size of comboboxes has changed. Why is this happening?

Here is a zip file containing entire java files http://s000.tinyupload.com/index.php?file_id=33381866612121130517

Alfred
  • 21,058
  • 61
  • 167
  • 249
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Apr 12 '16 at 23:18
  • @MadProgrammer I have uploaded the files – Alfred Apr 12 '16 at 23:25
  • `setLayout(null); ` is your first bad idea – MadProgrammer Apr 12 '16 at 23:28
  • You know, from what little I can understand, you don't need to use `JLayeredPane`, not quite sure why you wanted to use it, but instead you could use a `CardLayout` to switch between views – MadProgrammer Apr 12 '16 at 23:29
  • @MadProgrammer is there any quick fix? still with using jlayered pane? i need your help – Alfred Apr 12 '16 at 23:32
  • Why do you think you need to use `JLayeredPane`? And yes, use a `CardLayout` instead of manually switching the views – MadProgrammer Apr 12 '16 at 23:33
  • You've also lumped (from what I can) ALL you logic into a single class, each view should be responsible for it's own operations, leaving the only responsibility for the main view to provide navigation – MadProgrammer Apr 12 '16 at 23:35
  • @MadProgrammer can you help me to change it to card layout? I am a newbie and I need it urgently. I dont know how long it will take for me to turn it to card layout. I am not from a java background. I dont know anything about card layout. Kindly help me to fix this :( – Alfred Apr 12 '16 at 23:36
  • Oh my god... Seems like my whole effort is going to end up in nowhere.. I am running out of time.. :( – Alfred Apr 12 '16 at 23:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108982/discussion-between-blasteralfred--and-madprogrammer). – Alfred Apr 12 '16 at 23:38
  • [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Also, I've basically got your code to display the switcher, flight details and user details without to much of an issue. Generally just by adding `repaint` after the components were added to the container, but your `remover` method needs a lot more work, otherwise things tend to overlap each other – MadProgrammer Apr 12 '16 at 23:38
  • *".. I need it urgently"* You need better time management skills. – Andrew Thompson Apr 12 '16 at 23:43
  • @AndrewThompson this is my first experience.. Can u help me? – Alfred Apr 13 '16 at 00:02
  • *"this is my first experience.."* Your first experience of *what?* At 9,821 rep. it is surely not your first experience at SO. Also note that though your time may be limited, take the time to spell all ***3*** letters of words like 'you'. This is not twitter or a text message. – Andrew Thompson Apr 13 '16 at 00:34
  • As an aside, I would not spend time on this until you post (as an edit to the question, within the question) a [mcve] as suggested by @MadProgrammer in the first comment. If you could not be bothered/have not got the time to prepare an MCVE, stop asking me to help. – Andrew Thompson Apr 13 '16 at 00:36

1 Answers1

1

I combined this JLayeredPane example with this CardLayout example to demonstrate multiple, selectable layered panes.

image

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/a/36587584/230513
 * @see https://stackoverflow.com/a/6432291/230513
 * @see https://stackoverflow.com/questions/6432170
 */
public class CardPanel extends JLayeredPane {

    private static final Dimension d = new Dimension(320, 240);
    private static final Random random = new Random();
    private static final JPanel cards = new JPanel(new CardLayout());
    private static final JComboBox combo = new JComboBox();
    private final String name;

    public CardPanel(String name) {
        this.name = name;
        this.setBackground(new Color(random.nextInt()));
        this.add(new LayerPanel(1 * d.height / 8), 100);
        this.add(new LayerPanel(2 * d.height / 8), 101);
        this.add(new LayerPanel(3 * d.height / 8), 102);
    }

    @Override
    public Dimension getPreferredSize() {
        return d;
    }

    @Override
    public String toString() {
        return name;
    }

    private static class LayerPanel extends JPanel {

        private static final Random r = new Random();
        private int n;
        private Color color = new Color(r.nextInt());

        public LayerPanel(int n) {
            this.n = n;
            this.setOpaque(false);
            this.setBounds(n, n, d.width / 2, d.height / 2);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(color);
            g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);
            g2d.setColor(Color.black);
            g2d.drawString(String.valueOf(n), 5, getHeight() - 5);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 1; i < 9; i++) {
            CardPanel p = new CardPanel("Panel " + String.valueOf(i));
            combo.addItem(p);
            cards.add(p, p.toString());
        }
        JPanel control = new JPanel();
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox jcb = (JComboBox) e.getSource();
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.show(cards, jcb.getSelectedItem().toString());
            }
        });
        control.add(combo);
        f.add(cards, BorderLayout.CENTER);
        f.add(control, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045