Results 1 to 6 of 6
  1. #1
    horndinkle's Avatar
    horndinkle is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default Multiple JPanels within a JFrame

    I'm trying to have multiple JPanels within a JFrame, and it's not going too well. I start with a JFrame, then I add a JPanel to it which contains a JList. Then I select a value from the JList, and what I want to do is have another JPanel come up that displays the value selected in the JList. Here's my code so far (the relevant bits anyway):
    Java Code:
    	private void JListValueChanged(javax.swing.event.ListSelectionEvent evt) {
    		JList jlist = (JList) evt.getSource();
    		String s = (String) jlist.getSelectedValue();
    		this.add(new JavaPanel(s));
    	}
    Java Code:
    	public JavaPanel(String s) {
    		super();
    		JLabel label = new JLabel();
    		label.setFont(new Font("Roman", Font.BOLD, 24));
    		label.setText(s);
    		this.setVisible(true);
    	}
    (The latter extends JPanel.)

    I know it will be something that I'll say "duh" about, but I can't figure it out. Can anyone help me, please?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Okay first:
    Java Code:
    public JavaPanel(String s) {
    		super();
    		JLabel label = new JLabel();
    		label.setFont(new Font("Roman", Font.BOLD, 24));
    		label.setText(s);
    		this.setVisible(true);
    	}
    here you are creating a Panel and a JLabel, but you dont add the label to the panel or? this.add(label) is missing ?

    Then, in your listener code, you have to call revalidate() or something similar to that after you are added the new panel. (and consider that the valueChanged method will be invoked twice. maybe you have to copy that code in an if-statement like if (!e.getValueIsAdjusting()) { ... )

  3. #3
    horndinkle's Avatar
    horndinkle is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    You were right - I hadn't added the label to the panel or validated the frame. Here's my code now:

    Java Code:
    	private void JListValueChanged(javax.swing.event.ListSelectionEvent evt) {
    		JList jlist = (JList) evt.getSource();
    		String s = (String) jlist.getSelectedValue();
    		if (! evt.getValueIsAdjusting()) {
    			this.add(new JavaPanel(s));
    			this.validate();
    		}
    	}
    Java Code:
    	public JavaPanel(String s) {
    		super();
    		JLabel label = new JLabel();
    		label.setFont(new Font("Roman", Font.BOLD, 24));
    		label.setText(s);
    		this.add(label);
    		setVisible(true);
    	}
    I still can't get this to work, though. Do you (or someone) have any more suggestions?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db

  5. #5
    horndinkle's Avatar
    horndinkle is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    Oh, okay. Here's the code:

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class TheFrame extends JFrame {
    	private JList jList1;
    	private JScrollPane jScrollPane1;
    
    	public TheFrame() {
    		initComponents();
    	}
    
    	private void initComponents() {
    
    		jScrollPane1 = new JScrollPane();
    		jList1 = new JList();
    
    		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
    		jList1.setModel(new AbstractListModel() {
    			String[] strings = { "One", "Two", "Three", "Four", "Five", "Six" };
    
    			public int getSize() {
    				return strings.length;
    			}
    
    			public Object getElementAt(int i) {
    				return strings[i];
    			}
    		});
    		jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		jList1.addListSelectionListener(new ListSelectionListener() {
    			public void valueChanged(ListSelectionEvent evt) {
    				JListValueChanged(evt);
    			}
    		});
    		jScrollPane1.setViewportView(jList1);
    
    		GroupLayout layout = new GroupLayout(
    				getContentPane());
    		getContentPane().setLayout(layout);
    		layout.setHorizontalGroup(layout.createParallelGroup(
    				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
    				layout.createSequentialGroup()
    						.addGap(99, 99, 99)
    						.addComponent(jScrollPane1,
    								javax.swing.GroupLayout.PREFERRED_SIZE, 397,
    								javax.swing.GroupLayout.PREFERRED_SIZE)
    						.addContainerGap(96, Short.MAX_VALUE)));
    		layout.setVerticalGroup(layout.createParallelGroup(
    				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
    				layout.createSequentialGroup()
    						.addGap(27, 27, 27)
    						.addComponent(jScrollPane1,
    								javax.swing.GroupLayout.PREFERRED_SIZE, 74,
    								javax.swing.GroupLayout.PREFERRED_SIZE)
    						.addContainerGap(323, Short.MAX_VALUE)));
    
    		pack();
    	}
    
    	private void JListValueChanged(ListSelectionEvent evt) {
    		JList jlist = (JList) evt.getSource();
    		String s = (String) jlist.getSelectedValue();
    		if (! evt.getValueIsAdjusting()) {
    			this.add(new JavaPanel(s));
    			this.validate();
    		}
    	}
    
    	public static void main(String args[]) {
    		java.awt.EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				new TheFrame().setVisible(true);
    			}
    		});
    	}
    
    }
    Java Code:
    import java.awt.Font;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class JavaPanel extends JPanel {
    
    	public JavaPanel(String s) {
    		super();
    		JLabel label = new JLabel();
    		label.setFont(new Font("Roman", Font.BOLD, 24));
    		label.setText(s);
    		this.add(label);
    		setVisible(true);
    	}
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is a layout problem. You've given your JFrame's contentPane a fairly complex layout, the GroupLayout, and then add your new JPanel to the JFrame's contentPane without regard to this layout manager, and so you never see the added component. Why not create a JPanel to hold the new JPanels, and have it use a much simpler layout manager such as a BoxLayout, and then add your JPanels to this JPanel and revalidate it each time this occurs (you'll need to repaint it sometimes too, especially if you remove components).

    e.g.,

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    @SuppressWarnings("serial")
    public class TheFrame2 extends JPanel {
       private static final String[] ITEMS = {"One", "Two", "Three", "Four", "Five", "Six", 
          "Seven", "Eight", "Nine", "Ten"};
       private DefaultListModel model = new DefaultListModel();
       private JList myJList = new JList(model);
       private JPanel resultPanel = new JPanel();
       
       public TheFrame2() {
          for (String item : ITEMS) {
             model.addElement(item);
          }
          
          myJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          myJList.addListSelectionListener(new ListSelectionListener() {
             public void valueChanged(ListSelectionEvent e) {
                myListValueChanged(e);
             }
          });
          
          JScrollPane scrollpane = new JScrollPane(myJList);
          scrollpane.getViewport().setPreferredSize(new Dimension(200, 110));
          JPanel listPanel = new JPanel();
          listPanel.add(scrollpane);
          
          resultPanel.setLayout(new BoxLayout(resultPanel, BoxLayout.PAGE_AXIS));
          JScrollPane resultScrollPane = new JScrollPane(resultPanel, 
                   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          resultScrollPane.getViewport().setPreferredSize(new Dimension(200, 300));
          JPanel resultHolderPanel = new JPanel();
          resultHolderPanel.add(resultScrollPane);
          
          int eb = 25;
          setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
          setLayout(new BorderLayout(eb, eb));
          add(listPanel, BorderLayout.CENTER);
          add(resultHolderPanel, BorderLayout.SOUTH);
       }
    
       private void myListValueChanged(ListSelectionEvent e) {
          if (!e.getValueIsAdjusting()) {
             String selection = (String) myJList.getSelectedValue();
             resultPanel.add(new JavaPanel(selection));
             resultPanel.revalidate();
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("TheFrame2");
          frame.getContentPane().add(new TheFrame2());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

Similar Threads

  1. repainting a jframe containing two jpanels
    By musasabi in forum New To Java
    Replies: 0
    Last Post: 05-12-2010, 04:36 AM
  2. Switching JPanels inside JFrame attempt
    By frenk_castle in forum AWT / Swing
    Replies: 7
    Last Post: 03-31-2010, 08:39 AM
  3. Switch JPanels in a single JFrame
    By atom86 in forum AWT / Swing
    Replies: 8
    Last Post: 09-23-2009, 09:30 AM
  4. Multiple JPanels Not Working
    By jgezau in forum AWT / Swing
    Replies: 5
    Last Post: 04-17-2009, 04:52 AM
  5. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •