Hi there. I'm trying to write some code to display a graph with nodes/edges on a LayeredPane that will allow the user to move the nodes around. However, I am having some problems getting my nodes to show up. Whenever I launch the application, I just get a blank window. Does anyone have any ideas? Attached is the relevant file.
class GraphGUI extends JPanel
{
/**The frame that everything will lie in.*/
private JFrame mainFrame;
/**The panel that the nodes will lay in.*/
private JPanel nodesPanel;
/**LayeredPane to implement moving of the
* nodes within the windows.*/
private JLayeredPane nodeLayer;
/**The default size of the main panel.*/
private Dimension DEFAULT_DIMENSION = new Dimension( 650, 500 );
/**The graph object currently associated with the application.*/
private MatrixGraph graph;
/**The labels that will represent nodes of the graph.*/
private JLabel[] nodeLabels;
/**The default background color of the nodes*/
private Color nodeBgColor = Color.ORANGE;
/**
* Default constructor, creates an empty window/frame.
*/
public GraphGUI()
{
this.createMainComponents();
graph = new MatrixGraph( 5 );
this.createNodes();
this.displayNodes();
mainFrame.pack();
mainFrame.setVisible( true );
}
/**Method to create the main frame components. This includes the main frame
* itself, the main panel that resides inside the pane, and the layered pane
* for eventual drag and drop features. This will also create the buttons panel
* and the information panel that exists on the bottom.
*/
private void createMainComponents( )
{
mainFrame = new JFrame( "Graph: name=?" );
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mainFrame.setDefaultLookAndFeelDecorated( true );
mainFrame.setLocationRelativeTo( null );
mainFrame.setResizable( false );
mainFrame.getContentPane().setLayout( new BorderLayout() );
nodeLayer = new JLayeredPane();
nodeLayer.setPreferredSize( DEFAULT_DIMENSION );
nodesPanel = new JPanel();
nodesPanel.add( nodeLayer );
mainFrame.add( nodesPanel );
}
/**Creates all of the JLabels that will represent the nodes of the graph.
*/
private void createNodes( )
{
if( this.graph == null )
{
System.err.println( "error!" );
}
this.nodeLabels = new JLabel[ graph.getSize() ];
for( int i = 0; i < nodeLabels.length; ++i )
{
JLabel l = new JLabel( new Integer( i ).toString() );
l.setOpaque( true );
nodeLabels[ i ] = l;
}
}
/**Puts the nodes on the screen in two rows.
*/
private void displayNodes( )
{
for( int i = 0; i < nodeLabels.length; ++i )
{
nodeLayer.add( nodeLabels[i], JLayeredPane.DEFAULT_LAYER );
}
}
public static void main( String[] args )
{
String LandFError = "Error setting the L&F, using default\n";
try
{
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
GraphGUI g = new GraphGUI();
}
catch ( ClassNotFoundException e )
{
System.err.println( LandFError );
}
catch( InstantiationException e )
{
System.err.println( LandFError );
}
catch( IllegalAccessException e )
{
System.err.println( LandFError );
}
catch( UnsupportedLookAndFeelException e )
{
System.err.println( LandFError );
}
}
}