Components don't show in the content panel

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

Join Date: Aug 2008
Posts: 4
Reputation: jackieblock is an unknown quantity at this point 
Solved Threads: 0
jackieblock jackieblock is offline Offline
Newbie Poster

Components don't show in the content panel

 
0
  #1
Nov 8th, 2008
I am just learning Java. I am trying to do a GUI to calculate road trip data. My program compiles, and a window opens, but only the top title line shows. Nothing shows in the center or south areas. What do I need to change? Here is my file.
  1. * This class calculates and prints
  2. * Total Gallons Used, Total Fuel Cost,
  3. * and Travel Time from data entered by user.
  4. */
  5.  
  6. /** text files imported to aid in formatting decimals,
  7.  * and creating GUI interface */
  8. [
  9. import java.text.DecimalFormat;
  10. import java.text.NumberFormat;
  11. import java.awt.BorderLayout;
  12. import java.awt.Container;
  13. import java.awt.GridLayout;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16.  
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextArea;
  22. import javax.swing.JTextField;
  23.  
  24. public class RoadTripCalculationFrame extends JFrame implements ActionListener {
  25.  
  26. //Variables declared to be visible to all methods
  27. JTextArea output;
  28. JTextField distanceTf;
  29. JTextField mpgTf;
  30. JTextField speedTf;
  31. JTextField costTf;
  32.  
  33. NumberFormat formatter = new DecimalFormat("#0.00");
  34.  
  35. public RoadTripCalculationFrame() {
  36. }
  37.  
  38. //This constructor used when RoadTripCalculationFrame
  39. //object is created from main() method
  40.  
  41. public RoadTripCalculationFrame(String title) {
  42. super(title);
  43.  
  44. Container myContainer = getContentPane();
  45.  
  46. BorderLayout layout = new BorderLayout();
  47. myContainer.setLayout(layout);
  48.  
  49. JLabel titleL = new JLabel ("Road Trip Calculator");
  50. JLabel distanceL = new JLabel("Distance:");
  51. distanceTf = new JTextField(10);
  52. JLabel mpgL = new JLabel("MPG:");
  53. mpgTf = new JTextField(10);
  54. JLabel speedL = new JLabel("Speed:");
  55. speedTf = new JTextField(10);
  56. JLabel costpergallonL = new JLabel("Cost per Gallon:");
  57. costTf = new JTextField(10);
  58.  
  59. JButton calculateB = new JButton("Calculate");
  60. calculateB.addActionListener(this);
  61. JButton clearB = new JButton("Clear");
  62. clearB.addActionListener(this);
  63.  
  64. JPanel north = new JPanel();
  65. north.add(titleL);
  66. JPanel center = new JPanel();
  67. center.setLayout(new GridLayout(2,5));
  68. center.add(distanceL);
  69. center.add(distanceTf);
  70. center.add(mpgL);
  71. center.add(mpgTf);
  72. center.add(speedL);
  73. center.add(speedTf);
  74. center.add(costpergallonL);
  75. center.add(costTf);
  76. center.add(calculateB);
  77. center.add(clearB);
  78.  
  79. output = new JTextArea(100, 80);
  80.  
  81. myContainer.add(north, BorderLayout.NORTH);
  82. myContainer.add(center, BorderLayout.CENTER);
  83. myContainer.add(output, BorderLayout.SOUTH);
  84.  
  85. }
  86.  
  87. //This method is called when the buttons are pressed
  88.  
  89. public void actionPerformed(ActionEvent e) {
  90. JButton myButton = (JButton)e.getSource();
  91.  
  92. if (myButton.getText().equals("Calculate")){
  93. double distance = Double.parseDouble(distanceTf.getText());
  94. double mpg = Double.parseDouble(mpgTf.getText());
  95. double speed = Double.parseDouble(speedTf.getText());
  96. double cost = Double.parseDouble(costTf.getText());
  97.  
  98. double gallonsUsed = distance / mpg;
  99. double fuelCost = gallonsUsed * cost;
  100. double travel = distance / speed;
  101. double hours = travel;
  102. double decimalValue = distance % speed;
  103. double seconds = (decimalValue / speed) * 3600;
  104.  
  105. output.setText("Total Gallons Used:" + gallonsUsed +", Total Fuel Cost: $" + fuelCost +
  106. ", Travel Time:" + hours + "hrs, " + seconds + "seconds");
  107.  
  108. }
  109. else {
  110. output.setText("");
  111. distanceTf.setText("");
  112. mpgTf.setText("");
  113. speedTf.setText("");
  114. costTf.setText("");
  115. }
  116. }
  117.  
  118. //Entry point (main method) for the program
  119.  
  120. public static void main(String []args){
  121.  
  122. RoadTripCalculationFrame frame = new RoadTripCalculationFrame("Road Trip Calculation");
  123.  
  124. frame.setSize(500, 300);
  125. frame.setVisible(true);
  126. }
  127.  
  128. }

I would appreciate any help. I just can't figure out why nothing shows in the center of the frame.
Last edited by jackieblock; Nov 8th, 2008 at 3:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,629
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Components don't show in the content panel

 
0
  #2
Nov 8th, 2008
1. You need to use code tags. Read the rules stickied at the top of the forum.

2. Read this.
http://java.sun.com/docs/books/tutor...out/index.html
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 4
Reputation: jackieblock is an unknown quantity at this point 
Solved Threads: 0
jackieblock jackieblock is offline Offline
Newbie Poster

Re: Components don't show in the content panel

 
0
  #3
Nov 8th, 2008
I have done that. Can anyone help? Thank you!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Components don't show in the content panel

 
0
  #4
Nov 8th, 2008
haven't read the code in detail, and haven't worked on Swing app's for a while, but last time I made some, I sometimes had that problem when not using
  1. this.pack();
  2. this.show();
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,629
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Components don't show in the content panel

 
0
  #5
Nov 8th, 2008
well, one problem is that you never added your myContainer to the frame. So nothing will show up. Also, I'm not sure about the use of
Container myContainer = getContentPane();
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Components don't show in the content panel

 
0
  #6
Nov 8th, 2008
Overwrite method getPreferredSize() in JTextArea:

  1. outputArea = new JTextArea() {
  2. public Dimension getPreferredSize() {
  3. return new Dimension(100, 80);
  4. }
  5. };
quuba
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Components don't show in the content panel

 
0
  #7
Nov 9th, 2008
jackieblock - simple replace
  1. output = new JTextArea(100, 80);
with
  1. output = new JTextArea() {
  2. public Dimension getPreferredSize() {
  3. return new Dimension(100, 80);
  4. }
  5. };
quuba
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC