| | |
Swing Help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
java Syntax (Toggle Plain Text)
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 ); } } }
More systems have been wiped out by admins than any cracker could do in a lifetime.
![]() |
Similar Threads
- a Java GUI based program using Swing classes, Vat (Java)
- using javax.swing.Icon ... (Java)
- Java Swing Help!! (Java)
- Open Intenet Explorer in Swing application (Java)
- Swing Java Database (Java)
- Java awt and swing event handling (Java)
Other Threads in the Java Forum
- Previous Thread: JOptionPane with JSpinners Help
- Next Thread: Error in NetBeans
Views: 1482 | Replies: 0
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component consumer database desktop detection draw eclipse encode error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number object online oracle os print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner score screen server set size sms socket sort sql string swing template test threads time transfer tree update windows working xstream





