Resizing output window

Reply

Join Date: Jan 2007
Posts: 7
Reputation: bigbluesky is an unknown quantity at this point 
Solved Threads: 0
bigbluesky bigbluesky is offline Offline
Newbie Poster

Resizing output window

 
0
  #1
Aug 25th, 2007
I have tried to rezize my output window by changing the values in the width and legth - nothing happens. What am I doing wrong?
  1.  
  2. private final static int FRAME_GRID_ROWS = 3;
  3. private final static int FRAME_GRID_COLS = 1;
  4.  
  5. private final static int FIELD_PANEL_ROWS = 10;
  6. private final static int FIELD_PANEL_COLS = 5;
  7.  
  8. private final static int MAIN_PANEL_ROWS = 1;
  9. private final static int MAIN_PANEL_COLS = 4;
  10.  
  11.  
  12. private final static String EMPTY_ARRAY_MESSAGE = "Hit ADD to add a new Book";
  13.  
  14. private final static int FRAME_WIDTH = 350;
  15. private final static int FRAME_LENGTH = 300;
  16. private final static int FRAME_XLOC = 250;
  17. private final static int FRAME_YLOC = 100;

I also tried making changes in this part of the code to no avail. No matter what I change the numbers to, my window opens up the same size. Is there some code somewhere that controls this that I am not seeing?
  1. //Set some of the frame properties
  2. setSize( 350, 570 );
  3. setLocation( FRAME_XLOC , FRAME_YLOC );
  4. pack();
  5. setResizable(true);
  6. setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  7. setVisible( true );

Here is the rest of the code for this file, there are 2 more files with this program, but I don't think they have any code that would affect the size of the output window.
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*; //For java.awt.
  4. import java.util.Arrays;
  5.  
  6. public class Inventory6 extends JFrame
  7. {
  8.  
  9. //Private Variables
  10.  
  11. //The private data
  12. private Book [] book; //The inventory -- an array of Books
  13. private int index; //An index for keep track of the Book to display
  14.  
  15. //Panels
  16. private JPanel logoPanel; //Panel for placing the logo label
  17. private JPanel fieldPanel; //Panel for placing the JTextFields and their JLabels
  18. private JPanel mainPanel; //Panel for placing the main JButtons
  19.  
  20. //Labels
  21. private JLabel logoLabel; //Label for displaying the logo
  22. private JLabel nameLabel; //Label for the product name
  23. private JLabel idLabel; //Label for the product id
  24. private JLabel pagesLabel; //Label for the Book pages
  25. private JLabel unitsLabel; //Label for the units in stock
  26. private JLabel priceLabel; //Label for the price
  27. private JLabel valueLabel; //Label for the stock value
  28. private JLabel feeLabel; //Label for the restocking fee
  29. private JLabel totalLabel; //Label for the total value of the inventory
  30.  
  31. //Text Fields
  32. private JTextField nameText; //Field for displaying/editing the product name
  33. private JTextField idText; //Field for displaying/editing the product id
  34. private JTextField pagesText; //Field for displaying/editing the Book pages
  35. private JTextField unitsText; //Field for displaying/editing the units in stock
  36. private JTextField priceText; //Field for displaying/editing the Book price
  37. private JTextField valueText; //Field for displaying/editing the Book stock value
  38. private JTextField feeText; //Field for displaying/editing the Book restocking fee
  39. private JTextField totalText; //Field for displaying/editing the total inventory value
  40.  
  41. //Navigation Buttons
  42. private JButton firstButton; //Button for moving to the first element in the array
  43. private JButton nextButton; //Button for moving to the next element in the array
  44. private JButton previousButton; //Button for moving to the next element in the array
  45. private JButton lastButton; //Button for moving to the first element in the array
  46.  
  47. //Constants
  48. private final static int FRAME_GRID_ROWS = 3;
  49. private final static int FRAME_GRID_COLS = 1;
  50.  
  51. private final static int FIELD_PANEL_ROWS = 10;
  52. private final static int FIELD_PANEL_COLS = 5;
  53.  
  54. private final static int MAIN_PANEL_ROWS = 1;
  55. private final static int MAIN_PANEL_COLS = 4;
  56.  
  57.  
  58. private final static String EMPTY_ARRAY_MESSAGE = "Hit ADD to add a new Book";
  59.  
  60. private final static int FRAME_WIDTH = 350;
  61. private final static int FRAME_LENGTH = 300;
  62. private final static int FRAME_XLOC = 250;
  63. private final static int FRAME_YLOC = 100;
  64.  
  65. //Constructors
  66.  
  67.  
  68. //Initialization constructor
  69. // Starts the GUI with a Book array passed by the user, but ensures unique identification numbers
  70. public Inventory6( Book bookIn[] )
  71. {
  72. //Pass the frame title to JFrame and set the IconImage as well
  73. //"Kim's Book Inventory
  74. super( "Kim's Book Inventory" );
  75.  
  76. //Set the input array equal to the private array variable
  77. book = bookIn;
  78.  
  79. //Sort the array
  80. Arrays.sort( book );
  81.  
  82. //Always start the display at the first element of the array
  83. index = 0;
  84.  
  85. //Build the GUI
  86. buildGUI();
  87.  
  88. //Give it some values
  89. updateAllTextFields();
  90. }
  91.  
  92. //Set/Update/Other Methods
  93.  
  94.  
  95. //A method for updating each of the GUI fields
  96. private void updateAllTextFields()
  97. {
  98. if ( book.length > 0 ) //The update the TextField display
  99. {
  100. //Update the product name text field
  101. nameText.setText( book[index].getName() );
  102.  
  103. //Update the product id text field
  104. idText.setText( String.format( "%d", book[index].getIdentificationNumber() ) );
  105.  
  106. //Update the pages text field
  107. pagesText.setText( String.format( "%d", book[index].getPages() ) );
  108.  
  109. //Update the units in stock text field
  110. unitsText.setText( String.format( "%d", book[index].getUnitsInStock() ) );
  111.  
  112. //Update the price text field
  113. priceText.setText( String.format( "$%.2f" , book[index].getUnitPriceInDollars() ));
  114.  
  115. //Update the stock value text field
  116. valueText.setText( String.format( "$%.2f" , book[index].stockValueInDollars() ));
  117.  
  118. //Update the restocking fee text field
  119. feeText.setText( String.format( "$%.2f" , book[index].restockingFee() ));
  120.  
  121. //Update the total value text field
  122. totalText.setText( String.format( "$%.2f" , Product.totalInventoryValue( book ) ));
  123.  
  124. }//End if
  125. else //Put a special message in the fields
  126. {
  127. //Update the product name text field
  128. nameText.setText( EMPTY_ARRAY_MESSAGE );
  129.  
  130. //Update the product id text field
  131. idText.setText( EMPTY_ARRAY_MESSAGE );
  132.  
  133. //Update the pages text field
  134. pagesText.setText( EMPTY_ARRAY_MESSAGE );
  135.  
  136. //Update the units in stock text field
  137. unitsText.setText( EMPTY_ARRAY_MESSAGE );
  138.  
  139. //Update the price text field
  140. priceText.setText( EMPTY_ARRAY_MESSAGE );
  141.  
  142. //Update the stock value text field
  143. valueText.setText( EMPTY_ARRAY_MESSAGE );
  144.  
  145. //Update the restocking fee text field
  146. feeText.setText( EMPTY_ARRAY_MESSAGE );
  147.  
  148. //Update the total value text field
  149. totalText.setText( EMPTY_ARRAY_MESSAGE );
  150.  
  151. }//End else
  152.  
  153. }//End updateAllTextFields
  154.  
  155.  
  156. //Update the display for the calculated fields
  157. private void updateCalculatedFields()
  158. {
  159. //Update the stock value text field
  160. valueText.setText( String.format( "$%.2f" , book[index].stockValueInDollars() ));
  161.  
  162. //Update the restocking fee text field
  163. feeText.setText( String.format( "$%.2f" , book[index].restockingFee() ));
  164.  
  165. //Update the total value text field
  166. totalText.setText( String.format( "$%.2f" , Product.totalInventoryValue( book ) ));
  167.  
  168. }//End updateCalculatedFields
  169.  
  170.  
  171. //Set the appropriate fields editable or uneditable
  172. private void setModifiableTextFieldsEnabled( Boolean state )
  173. {
  174. //The Book name, ID, pages, units in stock, and price can all be set editable or uneditable
  175. nameText.setEditable( true );
  176. pagesText.setEditable( true );
  177. unitsText.setEditable( true);
  178. priceText.setEditable( true );
  179.  
  180. }//End setModifiableTextFieldsEnabled
  181.  
  182.  
  183.  
  184. //Button and Text Handlers and Methods
  185.  
  186. //Button Handler Class and Handling Methods
  187.  
  188. //The handler for handling the events for the buttons
  189. private class ButtonHandler implements ActionListener
  190. {
  191. public void actionPerformed(ActionEvent event)
  192. {
  193. if( event.getSource() == firstButton ) //|<<| pressed
  194. {
  195. handleFirstButton();
  196.  
  197. }//End if
  198.  
  199. else if( event.getSource() == previousButton ) //|< | pressed
  200. {
  201. handlePreviousButton();
  202.  
  203. }//End else if
  204. else if( event.getSource() == nextButton ) //| >| pressed
  205. {
  206. handleNextButton();
  207.  
  208. }//End else if
  209. else if( event.getSource() == lastButton ) //|>>| pressed
  210. {
  211. handleLastButton();
  212.  
  213. }//End else if
  214.  
  215. }//End method actionPerformed
  216.  
  217. }//End class ButtonHandler
  218.  
  219.  
  220.  
  221. //Display the first element of the Book array
  222. private void handleFirstButton()
  223. {
  224. //Set the index to the first element in the array
  225. index = 0;
  226.  
  227. //Update and disable modification
  228. updateAllTextFields();
  229. setModifiableTextFieldsEnabled( false );
  230.  
  231. }//End method handleFirstButton
  232.  
  233.  
  234. //Display the previous element of the Book array or wrap to the last
  235. private void handlePreviousButton()
  236. {
  237. //Decrement the index
  238. index--;
  239.  
  240. //If index is less than 0, wrap around to the last element of the array
  241. if ( index < 0 )
  242. {
  243. index = book.length - 1;
  244.  
  245. }//End if
  246.  
  247. //Update and disable modification
  248. updateAllTextFields();
  249. setModifiableTextFieldsEnabled( false );
  250.  
  251. }//End method handlePreviousButton
  252.  
  253. //Display the next element of the Book array or wrap to the first
  254. private void handleNextButton()
  255. {
  256. //Increment the index
  257. index++;
  258.  
  259. //If index exceeds the last valid array, wrap around to the first element of the array
  260. if ( index > book.length - 1)
  261. {
  262. index = 0;
  263.  
  264. }//End if
  265.  
  266. //Update and disable modification
  267. updateAllTextFields();
  268. setModifiableTextFieldsEnabled( false );
  269.  
  270. }//End method handleNextButton
  271.  
  272.  
  273. //Display the last element of the Book array
  274. private void handleLastButton()
  275. {
  276. //Set the index to the last book in the array
  277. index = book.length - 1;
  278.  
  279. //Update and disable modification
  280. updateAllTextFields();
  281. setModifiableTextFieldsEnabled( false );
  282.  
  283. }//End method handleLastButton
  284.  
  285.  
  286. //GUI Building Methods
  287.  
  288. //Build the GUI
  289. private void buildGUI()
  290. {
  291. //Make a grid for the panels
  292. setLayout( new GridLayout( FRAME_GRID_ROWS, FRAME_GRID_COLS ) );
  293.  
  294. //Add the logo
  295. buildLogo();
  296.  
  297. //Add the text fields, their labels, and the SEARCH line
  298. buildFields();
  299.  
  300. //Add the navigation and other buttons
  301. buildMainButtons();
  302.  
  303. //Set some of the frame properties
  304. setSize( 350, 570 );
  305. setLocation( FRAME_XLOC , FRAME_YLOC );
  306. pack();
  307. setResizable(true);
  308. setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  309. setVisible( true );
  310.  
  311. }//End buildGUI()
  312.  
  313.  
  314. //Add the logo to the JFrame
  315. private void buildLogo()
  316. {
  317. //Create the panel on which to place the logo
  318. logoPanel = new JPanel();
  319. logoPanel.setLayout( new GridLayout( 1, 1 ) );
  320.  
  321. //Create the logo
  322. logoLabel = new JLabel( new ImageIcon( "book10.gif" ) );
  323.  
  324. //Add the logo to the panel
  325. logoPanel.add( logoLabel );
  326.  
  327. //Add the panel to the frame
  328. add( logoPanel );
  329.  
  330. }//End method buildLogo
  331.  
  332. //Add the text fields, their labels, and the SEARCH line
  333. private void buildFields()
  334. {
  335. //Create the panel on which to place the labels and text fields
  336. fieldPanel = new JPanel();
  337. fieldPanel.setLayout( new GridLayout( FIELD_PANEL_ROWS, FIELD_PANEL_COLS ) );
  338.  
  339. //Declare a handler for the buttons
  340. ButtonHandler buttonHandler = new ButtonHandler();
  341.  
  342. //Create the name label
  343. nameLabel = new JLabel( "Name: " );
  344. nameLabel.setLabelFor( nameText );
  345. fieldPanel.add( nameLabel );
  346.  
  347. //Create the name text field
  348. nameText = new JTextField( "NONE" );
  349. nameText.setEditable( true );
  350. fieldPanel.add( nameText );
  351.  
  352. //Create the id label
  353. idLabel = new JLabel( "ID: " );
  354. idLabel.setLabelFor( idText );
  355. fieldPanel.add( idLabel );
  356.  
  357. //Create the id text field
  358. idText = new JTextField( "0" );
  359. idText.setEditable( true );
  360. fieldPanel.add( idText );
  361.  
  362. //Create the pages label
  363. pagesLabel = new JLabel( "Pages: " );
  364. pagesLabel.setLabelFor( pagesText );
  365. fieldPanel.add( pagesLabel );
  366.  
  367. //Create the pages text field
  368. pagesText = new JTextField( "" );
  369. pagesText.setEditable( true );
  370. fieldPanel.add( pagesText );
  371.  
  372. //Create the units in stock label
  373. unitsLabel = new JLabel( "Units in Stock: " );
  374. unitsLabel.setLabelFor( unitsText );
  375. fieldPanel.add( unitsLabel );
  376.  
  377. //Create the units in stock text field
  378. unitsText = new JTextField( "" );
  379. unitsText.setEditable( true );
  380. fieldPanel.add( unitsText );
  381.  
  382. //Create the unit price label
  383. priceLabel = new JLabel( "Unit Price: " );
  384. priceLabel.setLabelFor( priceText );
  385. fieldPanel.add( priceLabel );
  386.  
  387. //Create the unit price text field
  388. priceText = new JTextField( "" );
  389. priceText.setEditable( true );
  390. fieldPanel.add( priceText );
  391.  
  392. //Create the stock value label
  393. valueLabel = new JLabel( "Unit Stock Value: " );
  394. valueLabel.setLabelFor( valueText );
  395. fieldPanel.add( valueLabel );
  396.  
  397. //Create the stock value text field
  398. valueText = new JTextField( "" );
  399. valueText.setEditable( true );
  400. fieldPanel.add( valueText );
  401.  
  402. //Create the restocking fee label
  403. feeLabel = new JLabel( "Restocking Fee: " );
  404. feeLabel.setLabelFor( feeText );
  405. fieldPanel.add( feeLabel );
  406.  
  407. //Create the restocking fee text field
  408. feeText = new JTextField( "" );
  409. feeText.setEditable( true );
  410. fieldPanel.add( feeText );
  411.  
  412. //Add two labels that create a space between the other fields and the total inventory value field
  413. fieldPanel.add( new JLabel( " " ) );
  414. fieldPanel.add( new JLabel( " " ) );
  415.  
  416. //Create the total inventory value label
  417. totalLabel = new JLabel( "Total Inventory Value: " );
  418. totalLabel.setLabelFor( totalText );
  419. fieldPanel.add( totalLabel );
  420.  
  421. //Create the total inventory value text field
  422. totalText = new JTextField( "" );
  423. totalText.setEditable( false );
  424. fieldPanel.add( totalText );
  425.  
  426. //Add two labels that create a space between the total inventory value field and the search line
  427. fieldPanel.add( new JLabel( " " ) );
  428. fieldPanel.add( new JLabel( " " ) );
  429.  
  430. //Add the panel to the frame
  431. add( fieldPanel );
  432.  
  433. }//End buildFields
  434.  
  435. //Add the main buttons to the frame
  436. private void buildMainButtons()
  437. {
  438.  
  439. //Create the JPanel for the main buttons
  440. mainPanel = new JPanel();
  441. mainPanel.setLayout( new GridLayout( MAIN_PANEL_ROWS, MAIN_PANEL_COLS ) );
  442.  
  443. //Create the FIRST (<<) button
  444. firstButton = new JButton( "<<" );
  445. firstButton.addActionListener( new ButtonHandler() );
  446. mainPanel.add( firstButton );
  447.  
  448. //Create the PREVIOUS (<) button
  449. previousButton = new JButton( "< " );
  450. previousButton.addActionListener( new ButtonHandler() );
  451. mainPanel.add( previousButton );
  452.  
  453. //Create the NEXT (>) button
  454. nextButton = new JButton( ">" );
  455. nextButton.addActionListener( new ButtonHandler() );
  456. mainPanel.add( nextButton );
  457.  
  458. //Create the LAST (>>) button
  459. lastButton = new JButton( ">>" );
  460. lastButton.addActionListener( new ButtonHandler() );
  461. mainPanel.add( lastButton );
  462.  
  463. //Add the main button panel to the frame
  464. add( mainPanel );
  465.  
  466. }//End method buildMainButtons
  467.  
  468.  
  469.  
  470. //Main Method
  471.  
  472. //FutureValuePanel
  473.  
  474. //Provide a main method for genepages the GUI
  475. public static void main( String args[] )
  476. {
  477.  
  478. //Create some sample Books
  479. Book myBook[] = new Book[4];
  480.  
  481. myBook[0] = new Book("Inkspell", "Funke, Cornellia", 635, 1, 600, 20.00);
  482. myBook[1] = new Book("Thousand Orcs, The", "Salvatore, RA",345, 2, 700, 25.95);
  483. myBook[2] = new Book("Harry Potter and the Half Blood Prince", "Rowling, JK", 652, 3, 800, 29.99);
  484. myBook[3] = new Book("Magyk", "Sage, Angie", 564, 4, 900, 18.89);
  485. Inventory6 gui = new Inventory6( myBook );
  486. }
  487.  
  488. } //End class Inventory6


Any help would be greatl appreiciated.

Thank you!

-bigbluesky
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Resizing output window

 
0
  #2
Aug 27th, 2007
You just need to move the call to setSize() after pack(). Read the method description on pack() and you will see where the sizing issue is coming from.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC