Results 1 to 7 of 7
- 12-23-2010, 01:33 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
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
- 12-23-2010, 02:26 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
-
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.
- 12-23-2010, 02:33 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You've put some comments. Identify that the comment and the rest of the code are relevant too.
- 12-23-2010, 04:19 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
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.
- 12-24-2010, 12:48 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's because of the layout you've used. Do you know about those?
- 12-24-2010, 12:49 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Could anyway answer me this?
By Jojomofoman in forum New To JavaReplies: 14Last Post: 12-16-2010, 11:14 PM -
Need answer asap!!!
By uranis_khai in forum New To JavaReplies: 3Last Post: 07-07-2009, 09:48 AM -
Why is the answer not coming out
By anonymous18 in forum New To JavaReplies: 4Last Post: 11-12-2008, 03:10 AM -
i want my answer to a whole number or i think an int, please help?
By soc86 in forum New To JavaReplies: 3Last Post: 11-02-2008, 01:29 AM -
Plz answer this question ...
By raghu2114 in forum Advanced JavaReplies: 2Last Post: 09-19-2008, 06:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks