Results 1 to 3 of 3
  1. #1
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default need guidance in component placement

    screenshots

    I've posted on this before, got sidetracked.

    I created an rpn calculator as an exercise to learn GUI, and it was my first attempt at using swing.

    What I am finding is that there is a great deal of ambiguity in component placement as opposed to what I am used to coding using the tools of the evil empire (visual studio - mfc and visual basic), where I have exact control over size and placement. Html is similarly ambiguous, and I can sort of understand that Java might do the same thing. But Java can also be used to develop stand alone applications and I would like more control for desktop apps.

    The url shows my two attempts, one using netbeans and the other handcoded using gridbag layout manager.

    The netbeans is reasonably close to what I want, but everytime I change the size or location of one item, other items change size and location. Properties doesn't seem to have locations and only preferred sizes. I feel like I am chasing my tail.

    Grid bag gives me even less control. An answer to a previous post suggested that it is not something people like to use, but the sun tutorial indicates it has more flexibility than other methods do.

    What strategies should I use for getting more control over this particular layout, and what fuctions, classes, or techniques should I be studying to gain this level of control?

    I did notice that there is a forms document with netbeans. I took a quick look at it, but didn't study it. Is it an option to edit that file, assuming I can decipher it.

    General question on posting: Can I post an image directly? If I have to access an image via a url, I just as well write and post a web page.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The key is to nest layouts. Here's one example:
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class CalcGUI
    {
      private static final int EB_GAP = 8;
      private JPanel mainPanel = new JPanel();
      private KeyBoard keyboard = new KeyBoard();
      private JTextArea stack = new JTextArea(10, 5);
      private JTextField accumulator = new JTextField(5);
      
      public CalcGUI()
      {
        JLabel stackLabel = new JLabel("Stack", SwingConstants.LEFT);
        JLabel accumLabel = new JLabel("Accumulator", SwingConstants.LEFT);
        JScrollPane stackScroll = new JScrollPane(stack);
        JPanel keyboardPanel = keyboard.getPanel();
        JPanel accumPanel = new JPanel(new BorderLayout());
        accumPanel.add(accumulator, BorderLayout.PAGE_START);
        
        stackLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        accumLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        stackScroll.setAlignmentX(Component.LEFT_ALIGNMENT);
        keyboardPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        accumPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        Dimension accumD = accumPanel.getPreferredSize();
        accumPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, accumD.height));
        
        Dimension minSize = new Dimension(10, 10);
        Dimension prefSize = minSize;
        Dimension maxSize = new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
        
        mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        mainPanel.add(stackLabel);
        mainPanel.add(stackScroll);
        mainPanel.add(new Box.Filler(minSize, prefSize, maxSize)); 
        mainPanel.add(accumLabel);
        mainPanel.add(accumPanel);
        mainPanel.add(new Box.Filler(minSize, prefSize, maxSize));
        mainPanel.add(keyboardPanel);
      }
      
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("Calculator GUI");
        frame.getContentPane().add(new CalcGUI().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    
    class KeyBoard
    {
      public static final String[][] KEY_STRINGS =
      {
        {"1", "2", "3", "/"},
        {"4", "5", "6", "*"},
        {"7", "8", "9", "-"},
        {"0", ".", "Enter", "+"}
      };
      private static final int GL_GAP = 5;
      private static final Font ENTER_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 16);
      private static final Font KEY_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);
      private JPanel mainPanel = new JPanel();
      private JButton[][] keys = 
        new JButton[KEY_STRINGS.length][KEY_STRINGS[0].length];
      
      public KeyBoard()
      {
        mainPanel.setLayout(new GridLayout(4, 4, GL_GAP, GL_GAP));
        for (int row = 0; row < KEY_STRINGS.length; row++)
        {
          for (int col = 0; col < KEY_STRINGS[row].length; col++)
          {
            JButton btn = new JButton(KEY_STRINGS[row][col]);
            Font font = (KEY_STRINGS[row][col].length() > 1)?
                ENTER_FONT : KEY_FONT;
            btn.setFont(font);
            mainPanel.add(btn);
            keys[row][col] = btn;
          }
        }
      }
      
      public void addNumberKeyListener(ActionListener al)
      {
        for (int row = 0; row < keys.length; row++)
        {
          for (int col = 0; col < keys[row].length; col++)
          {
            char keyChar = keys[row][col].getText().charAt(0);
            if (Character.isDigit(keyChar))
            {
              keys[row][col].addActionListener(al);
            }
          }
        }
      }
      
      public void addOpKeyListener(ActionListener al)
      {
        for (int row = 0; row < keys.length; row++)
        {
          for (int col = 0; col < keys[row].length; col++)
          {
            char keyChar = keys[row][col].getText().charAt(0);
            if (!Character.isDigit(keyChar))
            {
              keys[row][col].addActionListener(al);
            }
          }
        }
      }
    
      public JPanel getPanel()
      {
        return mainPanel;
      }
    }

  3. #3
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    Thank you. Much more than I expected. I just compiled it and it is much closer to what I wanted, and gives me something to experiment with. Next week maybe:-). Right now, I started a three hour tour and I'm still on the island days later.

Similar Threads

  1. Find Component by name
    By amjad905 in forum Advanced Java
    Replies: 9
    Last Post: 04-25-2009, 05:55 PM
  2. Guidance in writing a compiler
    By Jeremy in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 11:25 PM
  3. Guidance
    By gizdev in forum New To Java
    Replies: 5
    Last Post: 09-07-2008, 07:14 PM
  4. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 09:09 AM
  5. Help with custom component
    By Falcon1 in forum AWT / Swing
    Replies: 8
    Last Post: 07-21-2007, 12:39 PM

Posting Permissions

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