Swing Help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 9
Reputation: Metsfan147 is an unknown quantity at this point 
Solved Threads: 0
Metsfan147's Avatar
Metsfan147 Metsfan147 is offline Offline
Newbie Poster

Swing Help

 
0
  #1
Apr 4th, 2007
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.

  1. class GraphGUI extends JPanel
  2. {
  3. /**The frame that everything will lie in.*/
  4. private JFrame mainFrame;
  5.  
  6. /**The panel that the nodes will lay in.*/
  7. private JPanel nodesPanel;
  8.  
  9. /**LayeredPane to implement moving of the
  10.   * nodes within the windows.*/
  11. private JLayeredPane nodeLayer;
  12.  
  13. /**The default size of the main panel.*/
  14. private Dimension DEFAULT_DIMENSION = new Dimension( 650, 500 );
  15.  
  16. /**The graph object currently associated with the application.*/
  17. private MatrixGraph graph;
  18.  
  19. /**The labels that will represent nodes of the graph.*/
  20. private JLabel[] nodeLabels;
  21.  
  22. /**The default background color of the nodes*/
  23. private Color nodeBgColor = Color.ORANGE;
  24.  
  25. /**
  26.   * Default constructor, creates an empty window/frame.
  27.   */
  28. public GraphGUI()
  29. {
  30. this.createMainComponents();
  31. graph = new MatrixGraph( 5 );
  32. this.createNodes();
  33. this.displayNodes();
  34. mainFrame.pack();
  35. mainFrame.setVisible( true );
  36. }
  37.  
  38. /**Method to create the main frame components. This includes the main frame
  39.   * itself, the main panel that resides inside the pane, and the layered pane
  40.   * for eventual drag and drop features. This will also create the buttons panel
  41.   * and the information panel that exists on the bottom.
  42.   */
  43. private void createMainComponents( )
  44. {
  45.  
  46. mainFrame = new JFrame( "Graph: name=?" );
  47. mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  48. mainFrame.setDefaultLookAndFeelDecorated( true );
  49. mainFrame.setLocationRelativeTo( null );
  50. mainFrame.setResizable( false );
  51. mainFrame.getContentPane().setLayout( new BorderLayout() );
  52.  
  53.  
  54. nodeLayer = new JLayeredPane();
  55. nodeLayer.setPreferredSize( DEFAULT_DIMENSION );
  56.  
  57. nodesPanel = new JPanel();
  58. nodesPanel.add( nodeLayer );
  59. mainFrame.add( nodesPanel );
  60.  
  61. }
  62.  
  63.  
  64. /**Creates all of the JLabels that will represent the nodes of the graph.
  65.   */
  66. private void createNodes( )
  67. {
  68. if( this.graph == null )
  69. {
  70. System.err.println( "error!" );
  71. }
  72.  
  73. this.nodeLabels = new JLabel[ graph.getSize() ];
  74.  
  75. for( int i = 0; i < nodeLabels.length; ++i )
  76. {
  77. JLabel l = new JLabel( new Integer( i ).toString() );
  78. l.setOpaque( true );
  79. nodeLabels[ i ] = l;
  80. }
  81. }
  82.  
  83.  
  84. /**Puts the nodes on the screen in two rows.
  85.   */
  86. private void displayNodes( )
  87. {
  88.  
  89. for( int i = 0; i < nodeLabels.length; ++i )
  90. {
  91. nodeLayer.add( nodeLabels[i], JLayeredPane.DEFAULT_LAYER );
  92. }
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99. public static void main( String[] args )
  100. {
  101. String LandFError = "Error setting the L&F, using default\n";
  102. try
  103. {
  104. UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
  105. GraphGUI g = new GraphGUI();
  106. }
  107.  
  108. catch ( ClassNotFoundException e )
  109. {
  110. System.err.println( LandFError );
  111. }
  112.  
  113. catch( InstantiationException e )
  114. {
  115. System.err.println( LandFError );
  116. }
  117.  
  118. catch( IllegalAccessException e )
  119. {
  120. System.err.println( LandFError );
  121. }
  122.  
  123. catch( UnsupportedLookAndFeelException e )
  124. {
  125. System.err.println( LandFError );
  126. }
  127. }
  128. }
More systems have been wiped out by admins than any cracker could do in a lifetime.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1482 | Replies: 0
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC