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

    Default GridLayout not work like I want


    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.

    Wanted Behavior in WPF/XAML:
    Curent Behavior in Java:

    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();
            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));
    The jPanel1 is a member added by designer.

    Here is my code in XAML/WPF:
    XML Code:
    <Window x:Class="RollNut.TileNut.MainWindow"
            Title="MainWindow" Height="350" Width="525"
            <Style x:Key="GridStyle" TargetType="Grid">
                <Setter Property="Width" Value="64" />
                <Setter Property="Height" Value="64" />
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <Grid Background="Lime" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                <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" />

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: GridLayout not work like I want

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


    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)

    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
    Rep Power

    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