Hespen
Hespen

Reputation: 1452

IntelliJ IDEA - JPanel in JScrollpane no content shown


I have a JScrollPane which needs to be filled with data coming from a DB. The panels are made with IntelliJ IDEA.

public class NodeViewer extends JFrame {
private NodeManager _manager;
private JScrollPane _container;

public NodeViewer(NodeManager manager) {
    setVisible(false);
    pack();
    setContentPane(_container);
    setSize(new Dimension(1000, 700));
    setLayout(new ScrollPaneLayout());
    _manager = manager;
}

public void LoadData() {
    _container.removeAll();
    LoadNodes(_manager.GetAllNodes());
}

private void LoadNodes(List<Object> nodes) {
    for (int i = 0; i < nodes.size(); i++) {

        JPanel newPanel = new NodePanel((INode) nodes.get(i), i);
        getContentPane().add(newPanel);
    }
}

This is the scrollpane, which will create a NodePanel for every iterations, and add it to the scrollpane.

public class NodePanel extends JPanel {
private JPanel _nodeContainer;
private JLabel _personName;
private JLabel _albumName;
private JLabel _bandName;
private JLabel _memberSince;
private JLabel _personBirth;
private JLabel _releasedOn;
private JButton _deletePerson;
private JButton _deleteBand;
private JButton _deleteAlbum;

public NodePanel(INode node, int i) {
    setVisible(true);
    setBounds(150, (i * 250) + (i * 25), 700, 250);
    setSize(new Dimension(700, 250));
    setLayout(new GridLayout());
    setBackground(Color.BLUE);
    SetContent(node);
}

The node panel extends the JPanel.

The panels itself are created: http://gyazo.com/6e55dc85c177f8599f16d3bdbeb6c36e as is seen on the screenshot. The content however is not to be seen. All JLabels from the NodePanel are set by setText().

I have tried to repaint, revalidate almost everything, but I can't seem to find what is wrong. I also tried different layout types, but that didn't seem to matter either.

Upvotes: 0

Views: 424

Answers (1)

mKorbel
mKorbel

Reputation: 109813

a few points


  • public class NodeViewer extends JFrame {, don't to extend JFrame, create this Obejct as local variable (as is private NodeManager _manager;)

code ordering is very important, e.g.

  • JFrame.setVisible(false);, why is visibility set to false, and for JFrame.setVisible(true); this code line should be the last code line in the public NodeViewer(NodeManager manager) {

  • JFrame.pack(); should be before code line JFrame.setVisible(true);, this code line is about to getPrefererSize from all JFrame childs in 1st level (JScrollPane in this case)

  • setSize(new Dimension(1000, 700));, rather to set getPrefererSize for JScrollPane, then JFrame.pack(); accepts this value as its PrefererSize

  • setLayout(new ScrollPaneLayout()); take this specifics LayoutManager as designated exclusivelly for JScrollPane (in APIs, BasicsScrollPaneUI or UIManager), is not designated as LayoutManager for JFrame


main problems

  • setContentPane(_container); != getContentPane().add(newPanel);, maybe to match with your original topics

  • _container.removeAll();, JSrollPane isn't container, option is explained in last point, otherwise to use JSrollPane.setVieportView(something);

  • overide getPreferredSize for public class NodePanel extends JPanel { instead of useless code lines setVisible(true); and setBounds(150, (i * 250) + (i * 25), 700, 250); and setSize(new Dimension(700, 250));


direct solution,

  • isn't easier to use JComponent.setText("") for all JTextComponents in public class NodePanel extends JPanel { rather than to remove and then to add the same JPanel instead of reseting for value, something hidden behind SetContent(node); should be refreshed with some default, initial value too

Upvotes: 2

Related Questions