JPanel not displaying components

i am working on a simple html renderer- at the moment all i am trying to do is to add JTextPane components to two different JPanels (search and content). I can add the text panes to the search bar, however none of the components are displayed in the content panel. I get no compile time errors, im really at a loss as to what the problem could be.

this is the code im using-

publicclass Demoextends JFrame

{

private JPanel search, content;

private JSplitPane splitPane;

//private ArrayList<JTextPane> textCollection;

//private ArrayList<JTextPane> preCollection;

//specifies the universal font size the content JPanel

privatefinalint fontSize = 18;

//specifies the universal font size for the search JPanel

privatefinalint searchFontSize = 9;

/** Creates a new instance of Main */

public Demo()

{

this.setTitle("DEMO!");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

//sets default size

this.setSize(500, 500);

//init panel controls- these will hold all rendered controls

search =new JPanel();

//use BoxLayout to ensure controls are positioned correctly

search.setLayout(new BoxLayout(search, BoxLayout.Y_AXIS));

search.setBackground(Color.white);

content =new JPanel();

content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

content.setBackground(Color.white);

//init JScrollPanes for both containers- allows for scrolling content of unlimited width/length

JScrollPane contentScroller =new JScrollPane(content);

JScrollPane searchScroller =new JScrollPane(search);

//create a split pane container to contain both scroll panes

splitPane =new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, searchScroller, contentScroller);

//search bar recieves 25% of screen size

splitPane.setDividerLocation(this.getWidth() / 4);

this.getContentPane().add(splitPane);

//show controls

this.setVisible(true);

}

/**

* Creates a new text component- text contained will be line wrapped as the user resizes the JFrame

*/

publicvoid newText(String text)

{

//init properites of new JTextPane

JTextPane tp = newTextPane(text);

tp.setBackground(Color.blue);

//line wrap add to content panel

tp.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));

tp = getLines(tp, this.getWidth() - splitPane.getDividerLocation());

content.add(tp);

//resize text for search panel, line wrap and add to search panel

tp.setFont(new Font("Times New Roman", Font.PLAIN, searchFontSize));

tp = getLines(tp, splitPane.getDividerLocation());

search.add(tp);

}

/**

* Creates a new preformated text component- this will not be line wrapped

*/

publicvoid newPre(String preformattedContent)

{

JTextPane tp = newTextPane(preformattedContent);

tp.setBackground(Color.red);

//add to content panel

tp.setFont(new Font("Courier New", Font.PLAIN, fontSize));

content.add(tp);

//resize text for search panel and add to search panel

tp.setFont(new Font("Courier New", Font.PLAIN, searchFontSize));

search.add(tp);

}

/**

* Small method that init's a JTextPane component with common properites used by the newText/Pre methods

*/

private JTextPane newTextPane(String s)

{

JTextPane tp =new JTextPane();

//set text

tp.setText(s);

//may not be editied

tp.setEditable(false);

//align correctly

tp.setAlignmentY(Component.TOP_ALIGNMENT);

tp.setAlignmentX(Component.LEFT_ALIGNMENT);

//set maximum size to preferred size, this makes sure the JTextComponent will not "stretch" when the JFrame is resized drastically

tp.setMaximumSize(tp.getPreferredSize());

return tp;

}

/**

* Sets the appropriate preferred height & width of a given JTextPanel, essentially word wrapping any text contained within

*/

private JTextPane getLines(JTextPane tp,int paneSize)

{

//get preferred size of the text panel - this is the smallest size the text panel may be and still visibly display its text content

Dimension d = tp.getPreferredSize();

double prefWidth = d.getWidth();

double prefHeight = d.getHeight();

//factor will determine if line wrapping is required

double factor = prefWidth / (double) paneSize;

//if factor is greater than 1, the preferred width is greater than the size available in the pane

if (factor >= 1){

//set width so that it matches the size available on the screen

d.setSize((double) this.getWidth() - paneSize, factor * prefHeight);

tp.setMaximumSize(d);

tp.setPreferredSize(d);

}

//else- do nothing! resizing is not required

return tp;

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String[] args)

{

Demo d =new Demo();

d.newText("this is some content ooh look content i hope this is long enough to test the wrapp esfsdfsdf sdf sdfsdfsd fsdf sdfsdfsdfd sfsdf sdfsdxf sdf sdf sdfsd fsd fsdf ing");

d.newPre("apre formattedlump of= text! ! !");

}

publicvoid paint(Graphics g)

{

super.paint(g);

System.out.println("update called");

//preferred.setSize(this.getWidth() - (this.getWidth() / 4), this.getHeight());

//max.setSize(this.getWidth(), this.getHeight());

}

}

[8878 byte] By [trooper22a] at [2007-11-26 18:40:57]
# 1
When you add or remove a component from a parent panel you need to revalidate() the parent panel so the Layout Manager can be invoked and lay out the components appropriately.Never override the paint() method of a Swing component, espcially of a JFrame
camickra at 2007-7-9 6:14:59 > top of Java-index,Desktop,Core GUI APIs...
# 2

Do you need to keep adding JTextPane's to the content and search panes or do you just need to append text to the existing panes?

If the later, you may want to create the JTextPanes in the constructor and use a set method to append/edit/remove text. As follows:

import javax.swing.*;

import java.awt.*;

public class Demo extends JFrame

{

private JPanel search, content;

private JSplitPane splitPane;

private JTextPane contentTextPane;

private JTextPane searchTextPane;

//private ArrayList<JTextPane> textCollection;

//private ArrayList<JTextPane> preCollection;

//specifies the universal font size the content JPanel

private final int fontSize = 18;

//specifies the universal font size for the search JPanel

private final int searchFontSize = 9;

/** Creates a new instance of Main */

public Demo()

{

this.setTitle("DEMO!");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

//sets default size

this.setSize(500, 500);

//init panel controls- these will hold all rendered controls

search = new JPanel();

//use BoxLayout to ensure controls are positioned correctly

search.setLayout(new BoxLayout(search, BoxLayout.Y_AXIS));

search.setBackground(Color.white);

content = new JPanel();

content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

content.setBackground(Color.white);

//init JScrollPanes for both containers- allows for scrolling content of unlimited width/length

JScrollPane contentScroller = new JScrollPane(content);

JScrollPane searchScroller = new JScrollPane(search);

//create a split pane container to contain both scroll panes

splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, searchScroller, contentScroller);

//search bar recieves 25% of screen size

splitPane.setDividerLocation(this.getWidth() / 4);

/*add the textpanes to the JPanels*/

contentTextPane = new JTextPane();

content.add(contentTextPane);

searchTextPane = new JTextPane();

search.add(searchTextPane);

this.getContentPane().add(splitPane);

//show controls

this.setVisible(true);

}

/**

* Creates a new text component- text contained will be line wrapped as the user resizes the JFrame

*/

public void newText(String text)

{

//init properites of new JTextPane

JTextPane tp = newTextPane(text);

tp.setBackground(Color.blue);

//line wrap add to content panel

tp.setFont(new Font("Times New Roman", Font.PLAIN, fontSize));

tp = getLines(tp, this.getWidth() - splitPane.getDividerLocation());

content.add(tp);

//resize text for search panel, line wrap and add to search panel

tp.setFont(new Font("Times New Roman", Font.PLAIN, searchFontSize));

tp = getLines(tp, splitPane.getDividerLocation());

search.add(tp);

}

public void addToContent(String text){

contentTextPane.setText(text);

}

/**

* Creates a new preformated text component- this will not be line wrapped

*/

public void newPre(String preformattedContent)

{

JTextPane tp = newTextPane(preformattedContent);

tp.setBackground(Color.red);

//add to content panel

tp.setFont(new Font("Courier New", Font.PLAIN, fontSize));

content.add(tp);

//resize text for search panel and add to search panel

tp.setFont(new Font("Courier New", Font.PLAIN, searchFontSize));

search.add(tp);

}

/**

* Small method that init's a JTextPane component with common properites used by the newText/Pre methods

*/

private JTextPane newTextPane(String s)

{

JTextPane tp = new JTextPane();

//set text

tp.setText(s);

//may not be editied

tp.setEditable(false);

//align correctly

tp.setAlignmentY(Component.TOP_ALIGNMENT);

tp.setAlignmentX(Component.LEFT_ALIGNMENT);

//set maximum size to preferred size, this makes sure the JTextComponent will not "stretch" when the JFrame is resized drastically

tp.setMaximumSize(tp.getPreferredSize());

return tp;

}

/**

* Sets the appropriate preferred height & width of a given JTextPanel, essentially word wrapping any text contained within

*/

private JTextPane getLines(JTextPane tp, int paneSize)

{

//get preferred size of the text panel - this is the smallest size the text panel may be and still visibly display its text content

Dimension d = tp.getPreferredSize();

double prefWidth = d.getWidth();

double prefHeight = d.getHeight();

//factor will determine if line wrapping is required

double factor = prefWidth / (double) paneSize;

//if factor is greater than 1, the preferred width is greater than the size available in the pane

if (factor >= 1){

//set width so that it matches the size available on the screen

d.setSize((double) this.getWidth() - paneSize, factor * prefHeight);

tp.setMaximumSize(d);

tp.setPreferredSize(d);

}

//else- do nothing! resizing is not required

return tp;

}

/**

* @param args the command line arguments

*/

public static void main(String[] args)

{

Demo d = new Demo();

d.newText("this is some content ooh look content i hope this is long enough to test the wrapp esfsdfsdf sdf sdfsdfsd fsdf sdfsdfsdfd sfsdf sdfsdxf sdf sdf sdfsd fsd fsdf ing");

d.newPre("apre formattedlump of= text! ! !");

d.addToContent("testing the content panel");

}

public void paint(Graphics g)

{

super.paint(g);

System.out.println("update called");

//preferred.setSize(this.getWidth() - (this.getWidth() / 4), this.getHeight());

//max.setSize(this.getWidth(), this.getHeight());

}

}

If this is not the case let me know and Ill take another look. ;-)

JOzzieTa at 2007-7-9 6:14:59 > top of Java-index,Desktop,Core GUI APIs...
# 3
By the way I also agree with camickr about overriding the paint() method of the JFrame. I should have really taken that out for ya.
JOzzieTa at 2007-7-9 6:14:59 > top of Java-index,Desktop,Core GUI APIs...