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 3 of 3

Thread: What in SWINGS name do i use to get this outcome?!

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default What in SWINGS name do i use to get this outcome?!

    (Credit: Copeg has given good insight so far, and primarily offered the layout manager wrapper trick, which does work!)
    EDIT: I have decided that Copeg's solution isn't really a hack, and is common practice, so I am using the wrapper trick.
    I want to now add a invisible layer on top of this to more or less protect it from user interaction.

    My program builds this:
    grid.JPG

    it is a bunch of individual 80x80 JPanels(called Tiles) inside a GridLayout JPanel(called BattleGrid).

    I need to place something over top of the BattleGrid that is invisible but I can add JPanels (Tiles) to by position and those will be visible, and will stay over the BattleGrid(in relative position) when ever the user moves the entire JFrame.

    Thanks,
    Jonathan
    Attached Images Attached Images
    Last edited by JonLane; February 25th, 2012 at 04:54 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What in SWINGS name do i use to get this outcome?!

    I'm assuming you're painting the grid onto some component (i.e. a JPanel)?

    The best solution is to override the paintComponent() method in such a way that it dynamically calculates what needs to be painted. Swing will automatically repaint on a resize, regardless of how it's resized (either manually or managed by the LayoutManager).

    ex.: Draw a 10x10 grid that fills the JPanel.

    import java.awt.Graphics;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Test extends JPanel
    {
     
    	@Override
    	public void paintComponent(Graphics g)
    	{
    		for (int i = 0; i <= getWidth() / 10; ++i)
    		{
    			g.drawLine(10 * i, 0, 10 * i, getHeight());
    		}
    		for (int j = 0; j <= getHeight() / 10; ++j)
    		{
    			g.drawLine(0, 10 * j, getWidth(), 10 * j);
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		JFrame f = new JFrame();
    		f.add(new Test());
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    	}
    }

    As far as maintaining the right toolbar to a static size, you could try looking into using the following combination:
    1. Have the "main" LayoutManager which contains the grid and the toolbar be a GridBagLayout. I believe if you set the weightx of the grid to 1 and the weighty of the toolbar to 0 then you'll have a static width toolbar and the grid will fill everything else.
    2. Set the main toolbar component to have a BoxLayout. I'm not too familiar with this type of LayoutManager so you'll have to play around with it, but from the looks of it you should be able to specify the maximum and minimum sizes of components you add to it, so in theory setting the max/min size equal to each other results in a fixed size component.

    edit: A better solution might be to use a Toolbar. Again, this isn't something I'm familiar with so you'll have to do some investigation yourself, but it looks promising and likely more functional than the half-hacked solution I gave above.
    Last edited by helloworld922; February 25th, 2012 at 12:43 PM.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: What in SWINGS name do i use to get this outcome?!

    I edited the original question to avoid confusion.
    http://docs.oracle.com/javase/tutori.../rootpane.html
    Last edited by JonLane; February 25th, 2012 at 05:09 PM.

Similar Threads

  1. please correct my programme on swings
    By jeevan reddy mandali in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 6th, 2012, 03:32 AM
  2. Java Swings
    By rosebrit3 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 19th, 2011, 08:16 AM
  3. [SOLVED] Can anyone mail me an e-book on Swings?
    By Lord Voldemort in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2011, 07:57 AM

Tags for this Thread