CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Threaded View

  1. #1

    Applet objects and GridBagLayout

    Howdy...

    In my never ending quest to learn Java, I have been working on making an applet that can be embedded in a web page. So far, all I am doing is just building the UI for the applet so I can get all the controls in. After that, I will go back and code the stuff that makes the controls work.

    But I have run into a snag, and have a couple questions. First, let me qualify this by saying that I have read several things on GridBagLayout and layouts in general. From what I read, on layouts, I decided to use that as it seems to give the most control when building the UI for my applet. However, I am having trouble with it.

    For starters, can someone explain exactly how the grid works? All the components for the GridBagConstratings are listed in rows and columns. I am assuming, from what I read that the width of each row and column is supposed to be expandabe based on the applications overall size. However, my controls are not coming out that well...

    Next, related to my last question is my code. Below, I have pasted in the code for my applet thus far. All it does is create two text areas and a blank panel. The panel is stupposed to be between the two text areas, but it ends up below both and to the right. The text areas end up side by side. Maybe I am just making a simple error and missing it... anyway, the other problem is that the text areas are very very tiny. I have tried various things, like changing the GBConstraints (for instance, gridwidth was changed from 6 (assuming it was an ambiguous 6 columns) to 300 (testing to be srue that it wasnt actually supposed to be pixels)... but still my TAs are messed up. Can someone review my code so far and maybe clue me in on where I am going wrong?

    Better yet, is there a better way to construct the UI other than GridBagLayout that will allow custom object placement? This was all done using J2EE 1.5...

    Code:
    import javax.swing.JApplet;
    import javax.swing.border.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    
    public class CWApplet extends JApplet {
        JTextArea inputTA = new JTextArea("Enter your text here",4,80);
        JTextArea outputTA = new JTextArea("output goes here",4,80);
        Border taBorder = BorderFactory.createLineBorder(Color.BLACK);
        GridBagLayout gbLayout = new GridBagLayout();
        GridBagConstraints gbCon = new GridBagConstraints();
    
        /* this makes it a little easer to work with GridBagConstraints */
        private void setGBConstraints(int row, int col, int width, int fill ){
            gbCon.gridy = row;
            gbCon.gridx = col;
            gbCon.gridwidth = width;
            gbCon.fill = fill;
        }
    
        public void init() {
            /*  General config here */
            setLayout(gbLayout);
    
            /*  Config input text area */
            inputTA.setEditable(true);
            inputTA.setEnabled(true);
            inputTA.setFocusable(true);
                    inputTA.setLineWrap(true);
                    inputTA.setWrapStyleWord(true);
                    inputTA.setBorder(taBorder);
            /* the next few lines are for configuring and using GridBagLayout */
            setGBConstraints(0,0,250,GridBagConstraints.HORIZONTAL);
            gbLayout.setConstraints(inputTA, gbCon);
            /* this allows us some scrolling, because JTextArea does not have
             *included scrolling */
            JScrollPane scrollInput = new JScrollPane(inputTA);
            add(scrollInput);
    
            Panel p = new Panel();
            p.setBackground(Color.DARK_GRAY);
            gbCon.gridx = 2;
            gbCon.gridy = 1;
            gbCon.gridwidth = 50;
            gbLayout.setConstraints(p, gbCon);
            add(p);
    
            /*  Config visual output text area */
            outputTA.setEditable(true);
            outputTA.setEnabled(true);
            outputTA.setFocusable(true);
                    outputTA.setLineWrap(true);
                    outputTA.setWrapStyleWord(true);
                    outputTA.setBorder(taBorder);
            setGBConstraints(2,0,250,GridBagConstraints.HORIZONTAL);
            gbLayout.setConstraints(outputTA, gbCon);
            JScrollPane scrollOutput = new JScrollPane(outputTA);
            add(scrollOutput);
    
    
        }
    }
    I am also attaching a jar file of the applet as it currently stands...
    Attached Files Attached Files

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured