943,715 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 890
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 2nd, 2009
0

JComponent Placement

Expand Post »
Hi. When I add a JComponent to my JFrame, it isn't the size of the remaining space in the frame. I want it to work in a way so that when i add a JComponent, it (and it's contents) stretches to fill the remaining space in the screen... Is there anyway to do that?
Similar Threads
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Jul 2nd, 2009
0

Re: JComponent Placement

Click to Expand / Collapse  Quote originally posted by llemes4011 ...
Hi. When I add a JComponent to my JFrame, it isn't the size of the remaining space in the frame. I want it to work in a way so that when i add a JComponent, it (and it's contents) stretches to fill the remaining space in the screen... Is there anyway to do that?
This will depend on your LayoutManager. What LayoutManager are you using?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jul 2nd, 2009
0

Re: JComponent Placement

I'm not... But I found a way that works. I have a thread that has static copies of the JFrame and the JComponent, and is always watching to see if the height or width is changing, then resizes the JComponent. I don't know it that is the way it's supposed to be done (probably not), but it is working. I only have 2 Components in the frame, so i think this way should be fine....
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Jul 3rd, 2009
0

Re: JComponent Placement

VernonDozier>What LayoutManager are you using?
llemes4011>I'm not... But I found a way that works.
Can I see the way you have used?
Do you know BorderLayout, GridBagLayout & GridBagConstraints?
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 3rd, 2009
0

Re: JComponent Placement

Click to Expand / Collapse  Quote originally posted by adatapost ...
VernonDozier>What LayoutManager are you using?
llemes4011>I'm not... But I found a way that works.
Can I see the way you have used?
Do you know BorderLayout, GridBagLayout & GridBagConstraints?
Yeah here's the code:
Java Syntax (Toggle Plain Text)
  1. package JEmail.util;
  2.  
  3. import java.util.*;
  4. import java.awt.*;
  5. import javax.swing.*;
  6.  
  7. public class FrameResizeHandler extends Thread
  8. {
  9. //consists of 2 JSplitPanes (one nested)
  10. private static ContentViewer cv;
  11. //components to resize
  12. private static ArrayList<JComponent> components;
  13. // JFrame to resize to
  14. private static JFrame watching;
  15.  
  16. private static int width; // JFrame width to check against
  17. private static int height; // JFrame height to check against
  18.  
  19. /**
  20.   * I used an ArrayList and JComponents because I want to
  21.   * get it so that it can deal with multiple resizings at once
  22.   */
  23. public FrameResizeHandler(JFrame jf, JComponent jc){
  24. watching = jf;
  25. components = new ArrayList<JComponent>();
  26. components.add(jc);
  27. width = watching.getWidth();
  28. height = watching.getHeight();
  29. }
  30.  
  31. public void run(){
  32. while(true){
  33. if(width!=watching.getWidth() || height!=watching.getHeight()){
  34. if(components.get(0) instanceof ContentViewer){
  35. // I know what the first element is so that's why it looks like this
  36. cv = (ContentViewer)components.get(0);
  37. // adjust the JComponents' size
  38. cv.setNewSize(watching.getWidth()-watching.getInsets().left-watching.getInsets().right,
  39. watching.getHeight()-watching.getInsets().top-watching.getInsets().bottom-57);
  40. }
  41. width = watching.getWidth(); // re-adjust the dimensions to check against
  42. height = watching.getHeight();
  43. }
  44. }
  45. }
  46. }

Note that this is still VERY sloppy. It doesn't completely work (It doesn't fix the size when the MAXIMIZE button is pressed), and is slow. I need to put in checks to determine when the thread actually goes through its loop so it doesn't eat up processor space (WHICH IT DOES! >=[ )

But Yes. I do know about BorderLayout, and I hate GridBagLayout. It's a total pain in the butt. But it does have its perks. I used it in another section of my program and it worked well there. I couldn't get it to work here.
Last edited by llemes4011; Jul 3rd, 2009 at 5:09 am. Reason: Added Comments
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Jul 3rd, 2009
0

Re: JComponent Placement

Use BorderLayout and add a component with CENTER position. Do not forget to issue validate() method of container after component has been added to that container.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 5th, 2009
0

Re: JComponent Placement

I tried that and it didn't resize it. ( I tried it as a JPanel too...)
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Jul 5th, 2009
0

Re: JComponent Placement

Please post complete code with bb code tags.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 5th, 2009
0

Re: JComponent Placement

Okay, Here's what I have:

Java Syntax (Toggle Plain Text)
  1. package JEmail.util;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. public class ContentViewer extends JPanel
  7. {
  8. public ContentViewer(){
  9. createAndShowGUI();
  10. }
  11.  
  12. public void createAndShowGUI(){
  13.  
  14. WindowComponents.vertSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  15. WindowComponents.horiSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  16. WindowComponents.files = new JTree();
  17. WindowComponents.tabs = new JTabbedPane();
  18. WindowComponents.display = new JPanel();
  19. WindowComponents.tabs.add(new InboxTab(), "Inbox");
  20. WindowComponents.tabs.add(new ContactsTab(), "Contacts");
  21. WindowComponents.tabs.add(new NotesTab(), "Notes");
  22.  
  23. WindowComponents.vertSplitPane.setTopComponent(WindowComponents.tabs);
  24. WindowComponents.vertSplitPane.setBottomComponent(WindowComponents.display);
  25. WindowComponents.vertSplitPane.setDividerLocation(.5);
  26. WindowComponents.horiSplitPane.setLeftComponent(WindowComponents.files);
  27. WindowComponents.horiSplitPane.setRightComponent(WindowComponents.vertSplitPane);
  28.  
  29. this.add(WindowComponents.horiSplitPane);
  30.  
  31. }
  32. }

Java Syntax (Toggle Plain Text)
  1. package JEmail;
  2. import JEmail.util.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. /**
  6.  * Write a description of class JMail_Runner here.
  7.  *
  8.  * @author (your name)
  9.  * @version (a version number or a date)
  10.  */
  11. public class JMail_Runner{
  12.  
  13. private static final int PWIDTH = 600;
  14. private static final int PHEIGHT = 500;
  15.  
  16. JFrame jMailWindow;
  17. JEmailMenuBar menuBar;
  18. ContentViewer content;
  19. FrameResizeHandler frh;
  20.  
  21. Insets windowInsets;
  22.  
  23. public JMail_Runner(){
  24. createAndShowGUI();
  25. }
  26.  
  27. public void createAndShowGUI(){
  28. try{ // Set the L&F to match the System - will have option to change later.
  29. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  30. }catch(Exception e){
  31. e.printStackTrace();
  32. }
  33.  
  34. jMailWindow = new JFrame("JEmail");
  35. jMailWindow.setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
  36. jMailWindow.setLocation(150,100);
  37. jMailWindow.setSize(PWIDTH, PHEIGHT);
  38.  
  39. Container pane = jMailWindow.getContentPane();
  40. pane.setLayout(new BorderLayout());
  41.  
  42. menuBar = new JEmailMenuBar();
  43. content = new ContentViewer();
  44.  
  45. // The MenuBar stretches fine... just not the content object
  46. pane.add(menuBar, BorderLayout.NORTH);
  47. pane.validate();
  48. pane.add(content, BorderLayout.CENTER);
  49. pane.validate();
  50.  
  51. jMailWindow.setVisible(true);
  52. }
  53.  
  54. public static void main(String[] args) {
  55. //Schedule a job for the event-dispatching thread:
  56. //creating and showing this application's GUI.
  57. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  58. public void run() {
  59. new JMail_Runner();
  60. }
  61. });
  62. }
  63. }
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Jul 5th, 2009
0

Re: JComponent Placement

As per your post I did't find any ambiguity in your code. I have added JMenuBar and ContentView and some components into ContentView - There is no problem.

I am not sure about JEmailMenuBar class. If it is JMenuBar then use setJMenuBar method of JFrame.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I need a class in Java API...
Next Thread in Java Forum Timeline: getting nullpoint exception again





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC