Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Hiding\Showing parts of the frame

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Hiding\Showing parts of the frame

    I have JFrame with some components, and I'm trying to supply methods for
    hiding\showing each component.
    I set the visible of the component to false - witch make him indeed invisible,
    and them I try to write pack for the frame, so it resize and fill the the free space with the component that left
    but it's now happen and the place where the component was became empty.

    Is there any simple solution for solving this issue?

    thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Hiding\Showing parts of the frame

    Why don't you just figure out the size you want your JFrame to be and then set it to that? The pack() method isn't going to take invisible components into account.

    Or you could set the visibility of the components after calling the pack() method?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    but I want that the other components will re size and fill the space,
    I don't want that the frame will be smaller.
    if I will calculate it, I will need to take into account all possibilities and combination of the components that left in the frame...it's seem like 'dirty' work

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Hiding\Showing parts of the frame

    I'm not sure what you're trying to accomplish, or what your problem is. Maybe you should provide an SSCCE that demonstrates what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    what I'm trying is similar to the functionality in the eclipse environment.
    if, for example I have four windows opening (like the first picture - the explorer the code, the javadoc and the task list window) and I will close the task window the window of the code will expand it's size, resulting in the second screen I attached.
    Attached Images Attached Images

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Hiding\Showing parts of the frame

    Hmm, with the right layout (BorderLayout maybe) and setting the visibility, I would expect that to be pretty easy. Or maybe you're looking for a JSplitPane?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    I don't sure,
    I am using currently GridBagLayout. but before that I try to use BorderLayout and that also won't help.
    After I set the visibly for component into false, his place become empty.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Hiding\Showing parts of the frame

    That's not what I would expect with BorderLayout. Like I said, if you want help, you'll have to provide an SSCCE that demonstrates the problem. Just using different colored JPanels should be enough for an SSCCE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    I found where the problem.
    I wrote little program with 5 panel, and 5 buttons that each button make panel hide\show and indeed the panel expand as I want.

    But when I add the panels with JScrollPane it's not work:
    instead writing:
    add(northPanel, BorderLayout.NORTH);
    writing:
    add(new JScrollPane(northPanel), BorderLayout.NORTH);

    I try to see if I can eliminate the use of JScrollPane in my program.

    thanks for help.

  10. #10
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    by the way, it's acting the same also with GridBagLayout.

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Hiding\Showing parts of the frame

    Like I said, I can't really help without seeing an SSCCE. Don't forget the highlight tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    But you did help...
    thanks again

  13. #13
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    oo my, just now I understand why it's happen,
    because when I do it in the second way:
    add(new JScrollPane(southPanel), BorderLayout.SOUTH);

    I actually had the scrollPane to the frame, and after it I set the visible for the panel...
    but the scrollPane still there, so there is no reason for the frame to change it's size.

  14. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Hiding\Showing parts of the frame

    Bingo. Sounds about right. Did you get it working now?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    Yes,

    only in case of setting the visibility of JScrollPane I have to call method pack() of the frame, to see the change.

  16. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Hiding\Showing parts of the frame

    Weird. Doesn't that resize the frame? Is that what you want? Are you using BorderLayout? I would really expect the component in the center to take any extra space.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    Junior Member
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hiding\Showing parts of the frame

    Again it's work.
    But if the components that I set his visible to false is JPanel it's enough to set it's visible, if it's JScrollPane I also need to call pack();

    you can run the code example and see that if you try to hide the south
    panel (which is inside scrollpane) it's not working properly
    if you adding the call to pack() after setting it's visible it's will work.


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import java.rmi.Naming;
     
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
     
    import trivia.server.TriviaServer;
    import trivia.server.TriviaServerImpl;
     
    public class test2 extends JFrame{
     
    	private JPanel westPanel;
    	private JPanel eastPanel;
    	private JPanel northPanel;
    	private JPanel southPanel;
    	private JPanel centerPanel;
     
    	private JMenuItem west;
    	private JMenuItem east;
    	private JMenuItem north;
    	private JMenuItem south;
    	private JMenuItem center;
    	JScrollPane scroll;
    	public test2()
    	{
    		setLayout(new BorderLayout());
     
    		westPanel=new JPanel();
    		westPanel.add(new JLabel("WEST"));
    		westPanel.setBackground(Color.GREEN);
    		add(westPanel, BorderLayout.WEST);
     
    		eastPanel=new JPanel();
    		eastPanel.add(new JLabel("EAST"));
    		eastPanel.setBackground(Color.RED);
    		add(eastPanel, BorderLayout.EAST);
     
    		northPanel=new JPanel();
    		northPanel.add(new JLabel("NORTH"));
    		northPanel.setBackground(Color.YELLOW);
    		add(northPanel, BorderLayout.NORTH);
     
    		southPanel=new JPanel();
    		southPanel.add(new JLabel("SOUTH"));
    		southPanel.setBackground(Color.MAGENTA);
    		scroll=new JScrollPane(southPanel);
    		add(scroll, BorderLayout.SOUTH);
     
    		centerPanel=new JPanel();
    		centerPanel.add(new JLabel("CENTER"));
    		centerPanel.setBackground(Color.PINK);
    		add(centerPanel, BorderLayout.CENTER);
     
    		ActionListener ma=new MyAction();
    		west=new JMenuItem("WEST");
    		east=new JMenuItem("EAST");
    		north=new JMenuItem("NORTH");
    		south=new JMenuItem("SOUTH");
    		center=new JMenuItem("CENTER");
     
    		west.addActionListener(ma);
    		east.addActionListener(ma);
    		north.addActionListener(ma);
    		south.addActionListener(ma);
    		center.addActionListener(ma);
     
    		JMenuBar mb=new JMenuBar();
    		JMenu visible=new JMenu("visible");
    		visible.add(west);
    		visible.add(east);
    		visible.add(south);
    		visible.add(north);
    		visible.add(center);
    		mb.add(visible);
     
    		setJMenuBar(mb);
     
    	}
     
    	private class MyAction implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource()==west)
    				westPanel.setVisible(!westPanel.isVisible());
    			else if(e.getSource()==east)
    				eastPanel.setVisible(!eastPanel.isVisible());
    			else if(e.getSource()==north)
    				northPanel.setVisible(!northPanel.isVisible());
    			else if(e.getSource()==south)
    				scroll.setVisible(!scroll.isVisible());
    			else if(e.getSource()==center)
    				centerPanel.setVisible(!centerPanel.isVisible());
     
    		}
    	}
     
     
    	public Dimension getPreferredSize()
    	{
    		return new Dimension(500, 500);
    	}
     
    	public static void main(String[] args) 
    	{
     
    		test2 frame=new test2();
     
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    		frame.setSize(500, 500);
    		frame.setVisible(true);
     
    	}
    }

  18. The Following User Says Thank You to liron50 For This Useful Post:

    KevinWorkman (April 29th, 2011)

  19. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Hiding\Showing parts of the frame

    Wow, that's really weird. I would not expect it to work that way. I don't have a great explanation for that. But your solution seems to work fine, so I guess it's not that big a deal.

    By the way, thanks for the SSCCE. That really showed what you were talking about. I appreciate that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Repeating different parts of a program
    By swiftxjames in forum Loops & Control Statements
    Replies: 3
    Last Post: November 17th, 2010, 04:29 PM
  2. showMessageDialog is not showing!
    By jonathan920 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2010, 04:14 AM
  3. showing page
    By ighor10 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 26th, 2010, 08:52 AM
  4. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 02:42 PM
  5. Data hiding and maintainability
    By humdinger in forum Object Oriented Programming
    Replies: 11
    Last Post: December 5th, 2009, 01:10 PM