d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Swing Getcontentpane() Help
Add Reply New Topic New Poll
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
May 24 2012 10:58pm
I am trying to make a scrolling Jlist like http://www.java2s.com/Code/Java/Swing-JFC/AsimpleJScrollPaneforaJListcomponent.htm, but that example is a frame with one panel. I have a Jlist within "panel", which is within "tabSection", which is within my frame. How do i use the "getContentPane()"? The problem is on the last statement in this code.

Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class RecipeTab extends JPanel
{
private JList list;
private DefaultListModel listModel;
private JTextField employeeName;
private JScrollPane scrollPane;
 
public RecipeTab(JFrame frame, TabSection tabSection, JPanel panel)
{
 String categories[] = { "Household", "Office", "Extended Family",
       "Company (US)", "Company (World)", "Team", "Will",
       "Birthday Card List", "High School", "Country", "Continent",
       "Planet","Household", "Office", "Extended Family",
       "Company (US)", "Company (World)", "Team", "Will",
       "Birthday Card List", "High School", "Country", "Continent",
       "Planet","Household", "Office", "Extended Family",
       "Company (US)", "Company (World)", "Team", "Will",
       "Birthday Card List", "High School", "Country", "Continent",
       "Planet","Household", "Office", "Extended Family",
       "Company (US)", "Company (World)", "Team", "Will",
       "Birthday Card List", "High School", "Country", "Continent",
       "Planet" };
              list = new JList(categories);
              scrollPane = new JScrollPane(list);
              list.setPreferredSize(new Dimension(tabSection.getWidth(), tabSection.getHeight()));
              panel.add(list);
 
               //problem here doesnt compile, but I need this line to get scrolling
               getContentPane().add(scrollpane, BorderLayout.CENTER);
}
}


Thanks for the help. I am very rusty with all this :)
Edit: Sorry the spacing copied over horribly

This post was edited by xandumx on May 24 2012 10:59pm
Member
Posts: 31
Joined: May 18 2012
Gold: 1,450.00
May 25 2012 03:20am
getContentPane() is a method of the JFrame class. In the example, they are able to call it because the class they are in extends JFrame. Here, it seems all you need to do is
Code
frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
May 25 2012 09:46am
Quote (jfkelley @ May 25 2012 02:20am)
getContentPane() is a method of the JFrame class. In the example, they are able to call it because the class they are in extends JFrame. Here, it seems all you need to do is
Code
frame.getContentPane().add(scrollpane, BorderLayout.CENTER);


Tried that, it doesn't affect anything. Do i need to add all my panels to the ContentPane since it is embedded?
Member
Posts: 31
Joined: May 18 2012
Gold: 1,450.00
May 25 2012 06:23pm
Quote (xandumx @ May 25 2012 08:46am)
Tried that, it doesn't affect anything.  Do i need to add all my panels to the ContentPane since it is embedded?


Probably not - you just need to be sure that everything you want displayed is added to something that is eventually added to the frame. Anyway, if that didn't fix it, I'm not sure - are you calling setVisible(true) on the JFrame? Can you post all of your code?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll