Results 1 to 3 of 3
  1. #1
    Metzler is offline Member
    Join Date
    Mar 2013
    Location
    Germany, Nuremberg
    Posts
    1
    Rep Power
    0

    Default GridLayout not work like I want

    Hello,

    I'm just beginning with Java/Swing.

    I want a grid which is surrounded with a scrollbar. The cell's size should never be changed. When I scale down the window then the behavior is like I wanted. But when I scale up the window then every time the cell's size is optimized to the window width.
    You can show an example Image or the exe/jar for better understanding.

    Image: https://dl.dropbox.com/u/88709713/Temp/GridExample.png
    Wanted Behavior in WPF/XAML: https://dl.dropbox.com/u/88709713/Te...xample-WPF.exe
    Curent Behavior in Java: https://dl.dropbox.com/u/88709713/Te...mple-Swing.jar

    I use NetBeans with designer.

    Here is the code I use in Java:

    Java Code:
    public class GridTestForm extends javax.swing.JFrame {
    
        /**
         * Creates new form GridTestForm
         */
        public GridTestForm() {
            BorderLayout bLayout = new BorderLayout();
            this.setLayout(bLayout);
            
            initComponents();
            
            jPanel1.setLayout(new GridLayout(4,4, 1, 1));
            for (int i = 0; i < 4*4; i++)
            {
                Panel panel = new Panel();
                panel.setPreferredSize(new Dimension(64,64));
                panel.setBackground(Color.red);
                jPanel1.add(panel);
            }
        }
    The jPanel1 is a member added by designer.

    Here is my code in XAML/WPF:
    XML Code:
    <Window x:Class="RollNut.TileNut.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"
            
    >
        <Window.Resources>
            <Style x:Key="GridStyle" TargetType="Grid">
                <Setter Property="Width" Value="64" />
                <Setter Property="Height" Value="64" />
            </Style>
        </Window.Resources>
        
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <Grid Background="Lime" HorizontalAlignment="Center" VerticalAlignment="Center">
            
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
    
                <Grid Grid.Column="0" Grid.Row="0" Style="{StaticResource GridStyle}" Background="Red" />
                <Grid Grid.Column="1" Grid.Row="0" Style="{StaticResource GridStyle}" Background="Green" />
                <Grid Grid.Column="2" Grid.Row="0" Style="{StaticResource GridStyle}" Background="Blue" />
                <Grid Grid.Column="3" Grid.Row="0" Style="{StaticResource GridStyle}" Background="Yellow" />
    
                <Grid Grid.Column="0" Grid.Row="1" Style="{StaticResource GridStyle}" Background="Blue" />
                <Grid Grid.Column="1" Grid.Row="1" Style="{StaticResource GridStyle}" Background="Purple" />
                <Grid Grid.Column="2" Grid.Row="1" Style="{StaticResource GridStyle}" Background="Orange" />
                <Grid Grid.Column="3" Grid.Row="1" Style="{StaticResource GridStyle}" Background="Magenta" />
    
                <Grid Grid.Column="0" Grid.Row="2" Style="{StaticResource GridStyle}" Background="Red" />
                <Grid Grid.Column="1" Grid.Row="2" Style="{StaticResource GridStyle}" Background="Green" />
                <Grid Grid.Column="2" Grid.Row="2" Style="{StaticResource GridStyle}" Background="Blue" />
                <Grid Grid.Column="3" Grid.Row="2" Style="{StaticResource GridStyle}" Background="Yellow" />
    
                <Grid Grid.Column="0" Grid.Row="3" Style="{StaticResource GridStyle}" Background="Blue" />
                <Grid Grid.Column="1" Grid.Row="3" Style="{StaticResource GridStyle}" Background="Purple" />
                <Grid Grid.Column="2" Grid.Row="3" Style="{StaticResource GridStyle}" Background="Orange" />
                <Grid Grid.Column="3" Grid.Row="3" Style="{StaticResource GridStyle}" Background="Magenta" />
            </Grid>
        </ScrollViewer>
    </Window>

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: GridLayout not work like I want

    Quote Originally Posted by Metzler View Post
    I'm just beginning with Java/Swing.

    (snip)

    I use NetBeans with designer.
    Bad combination. Learn to write Swing code before attempting to fathom the quirks of the designer.
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Re: GridLayout not work like I want

    Swing layouts typically resize when their container resizes. That's a feature. Personally, I find that the GridBagLayout is the easiest to control, although it takes some time to understand. The trick is to make sure that at least one column and row can resize without ruining your layout.
    The Java Tutorial. Read it.

Similar Threads

  1. GridLayout question
    By stuckonjava in forum New To Java
    Replies: 6
    Last Post: 12-09-2012, 05:07 PM
  2. Alignment in GridLayout
    By Zamereon in forum AWT / Swing
    Replies: 2
    Last Post: 02-27-2011, 11:49 AM
  3. help! Gridlayout
    By eiramae in forum Java Applets
    Replies: 3
    Last Post: 02-19-2011, 05:11 AM
  4. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM
  5. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 11:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •