Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default This will have an obvious answer!

    Hi folks, I was just wondering how, in the following example, I'd make button1 appear on the JFrame. I've put "THIS DOESN'T WORK!" after my attempt which is commented out at this time...



    Java Code:
    package marksct;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.ScrollPaneConstants;
    
    public class MarksCT {
      	static JFrame frame;
      	static JTable table;
      	protected static JButton button1;
    
    	private static void createAndShowGUI() {
    		/*
    		 * Here's the data that I want to put in the JTable.
    		 */
            String[] columnNames = {"First Name",
                    "Last Name",
                    "Sport",
                    "# of Years",
                    "Vegetarian"};
            Object[][] data = {
            	    {"Kathy", "Smith",
            	     "Snowboarding", new Integer(5), new Boolean(false)},
            	    {"John", "Doe",
            	     "Rowing", new Integer(3), new Boolean(true)},
            	    {"Sue", "Black",
            	     "Knitting", new Integer(2), new Boolean(false)},
            	    {"Jane", "White",
            	     "Speed reading", new Integer(20), new Boolean(true)},
            	    {"Joe", "Brown",
            	     "Pool", new Integer(10), new Boolean(false)}
            	};
    
    
            
        	//Create a JFrame and make sure program ends when visuals are closed.
            frame = new JFrame("MarksCT Application");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create a JTable with the data in it.
        	table = new JTable(data, columnNames);
        	//Create a JScrollPane make it contain the JTable
        	JScrollPane scroller =
        		new JScrollPane (table, 
        			ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
        			ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        	//Add the JScrollPane to the JFrame ContentPane
        	frame.getContentPane().add (scroller);
        	button1=new JButton();
        	//frame.getContentPane().add (button1); // THIS DOESN'T WORK!
        	
        	frame.pack(); // Make the window use the minimal amount of space possible.
            frame.setVisible(true);
        }
    	
    	public static void main(String[] args) {
    	    //Asynchronously create and show this applications GUI.
    		/* This application does the following;
    		 * (a) Creates a CustomTableModel
    		 * (b) Uses the above to display the contents of a database.
    		 */
    	    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    	        public void run() {
    	            createAndShowGUI(); 
    	        }
    	    });
    	}
    
    }
    Last edited by Eranga; 12-23-2010 at 02:24 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Please use code tags next time. Unformated codes are really hard to read. If you don't know how to do that, check my forum signature.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by MarkusHendersonicus View Post
    Hi folks, I was just wondering how, in the following example, I'd make button1 appear on the JFrame. I've put "THIS DOESN'T WORK!" after my attempt which is commented out at this time...
    Please define "this doesn't work" for us, because if we don't know how it's not working, we can't help you.

    Also, you'll want to read the Oracle Swing tutorials layout manager section where you'll see that a JFrame's contentPane uses BorderLayout by default, and what BorderLayout's behavior is.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You've put some comments. Identify that the comment and the rest of the code are relevant too.

  5. #5
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Sorry folks! Basically, when the following line is allowed to execute

    frame.getContentPane().add (button1); // THIS DOESN'T WORK!

    I would expect a JButton to appear in addition to the already displayed JTable. However, what actually happens is that the JTable vanishes and a very thin JButton appears instead! I have no idea why at this point.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Could anyway answer me this?
    By Jojomofoman in forum New To Java
    Replies: 14
    Last Post: 12-16-2010, 11:14 PM
  2. Need answer asap!!!
    By uranis_khai in forum New To Java
    Replies: 3
    Last Post: 07-07-2009, 09:48 AM
  3. Why is the answer not coming out
    By anonymous18 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 03:10 AM
  4. Replies: 3
    Last Post: 11-02-2008, 01:29 AM
  5. Plz answer this question ...
    By raghu2114 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 06:36 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
  •