Results 1 to 5 of 5
  1. #1
    DC1
    DC1 is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default Simple JFrame Problem

    Hi,

    I'm having issues adding a JScrollPane to a JFrame via another method. The pane isn't visible unless you first minise the frame/look at something else - validate() doesn't work. The only way I can solve it is to close the initial frame, then recreate it right before I add the pane - which obviously looks awful to the user. Can someone pls help me?

    Java Code:
      
    
    private void initialise(){
    		
    		frame = new JFrame("Relational Algebra and SQL Visualisation Tool");
    		//frame.setTitle();
    		frame.getContentPane().setLayout(null);
    		frame.setSize(screenSize);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //add stuff
    
    		frame.setVisible(true);
    }
    
    private class EventListener implements ActionListener{
    		public void actionPerformed(ActionEvent event) {
    			// Actions taken when button is clicked
    			JButton sent = (JButton) event.getSource();
    			String button = sent.getText();
    			if (button.equals("Generate RA Tree")) {
    				// Parse query string and generate tree
    				
    				
    				Tree tree = new Tree();
    				JScrollPane j = new JScrollPane();
    				j.setLayout(null);
    				j = tree.genTree(text.getText());
    				frame.dispose();
    				initialise();
    				frame.getContentPane().add(j);
    //etc.
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You may not want to add things directly to a JFrame. I often find it better to add them to a JPanel that is added to the JFrame's contentPane. Then after changing the components on the JPanel, call revalidate() on it (sometimes also requiring a repaint()), and you should see the changes.

    Also, I strongly suggest you avoid using the "null" layout and learn to use the Swing/AWT layout managers. Once you've used them and have become familiar with them, they will make your GUI coding much easier.
    Last edited by Fubarable; 06-05-2008 at 04:32 AM.

  3. #3
    DC1
    DC1 is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default

    Hey, thanks. I used revalidate() and such and it works fine.

    Only thing is, before I add the scrollpane, I add textfields to it. Now I can see the scrollpane, but none of the text fields inside it! I assume it's another revalidate()-type problem but I have no idea where to try anything! Anyone care to help?

    (Apologies, I'm vv new to Swing!)

    Java Code:
     
    
    private void initialise(){
    		
    		frame = new JFrame("Relational Algebra and SQL Visualisation Tool");
    
    //etc.
    
    frame.setVisible(true);
    }
    
    private class EventListener implements ActionListener{
    		public void actionPerformed(ActionEvent event) {
    			// Actions taken when button is clicked
    			JButton sent = (JButton) event.getSource();
    			String button = sent.getText();
    			if (button.equals("Generate RA Tree")) {
    				// Parse query string and generate tree
    				
    				
    				Tree tree = new Tree();
    				JScrollPane j = new JScrollPane();
    				j.setLayout(null);
    				j = tree.genTree(text.getText());
    				//frame.dispose();
    				//initialise();
    				frame.getContentPane().add(j);
    				j.invalidate();
    				j.validate();
    				j.repaint();
    // can now see the scrollpane, but non of the text fields on it
    			}
    }
    
    public JScrollPane genTree(String query){
    		// Parses query and creates RA tree
    		JScrollPane scroll = new JScrollPane();
    scroll.setLayout(null);
    		scroll = drawTree();
    		
    		return scroll;
    	}
    
    public JScrollPane drawTree(){
    		int pos = 20;
    		JScrollPane scroll = new JScrollPane();
    		scroll.setBounds(584, 71, 600, 600);
    		scroll.setLayout(null);
    		for (int i = ((left.length)-1); i>(-1); i--){
    			if (!(left[i].equals(""))){
    				JTextField x = new JTextField(left[i]);
    				x.setEditable(false);
    				if ((right[0] == null) && (centre[0] == null)){
    					x.setBounds(180, pos, 140, 30);
    				}
    				System.out.println(x.getText());
    				x.setHorizontalAlignment(JTextField.CENTER);
    				scroll.add(x);
    				
    				
    				pos += 50;				
    			}
    		}	
    		return scroll;		
    	}
    (that's all the code that should be needed to understand my problem - obv. I'm adding a text field containing text to the scrollpane, which is then eventually routed back to the method where it is added to the frame).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please don't cross-post questions in multiple forums (here, the forum.java.sun.com, and lord-knows where). It can frustrate many who volunteer time here to help someone only to find that the question had already been answered elsewhere. No one likes wasting time. It's also a breech of the forum policies which you agreed to on joining this forum.
    Last edited by Fubarable; 06-06-2008 at 08:07 AM.

  5. #5
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    And maybe, he could be one of the expert's ignore list soon....

    I hope not....
    freedom exists in the world of ideas

Similar Threads

  1. My Simple Refresh Problem
    By pmcastillo in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 08:59 AM
  2. JFrame problem
    By vassil_zorev in forum AWT / Swing
    Replies: 1
    Last Post: 01-25-2008, 03:53 AM
  3. Picture in a JFrame problem
    By saytri in forum New To Java
    Replies: 3
    Last Post: 01-12-2008, 10:44 AM
  4. JFrame problem
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 06:12 PM
  5. New to JSF, simple problem with Eclispe and Tomcat
    By nibeck in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-16-2007, 06:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •