Adding Components to JPanel - Help required

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davedwm
    New Member
    • Apr 2007
    • 11

    Adding Components to JPanel - Help required

    I'm trying to display a grid of TextPane's that are sized to fit a JPanel i've drawn into a Dialog class, the number of 'Cells' are entered into JTextFields and when the appropriate button is hit this method is executed. Each TextPane is stored as an object in the aray tableGrid. The code compiles fine, but when executed the JPanel remains blank.

    I've created did using a book that explains it in AWT and i'm using SWING but so far as i can tell this should work for SWING too

    Any help would be much appreciated.

    private void jButton1ActionP erformed(java.a wt.event.Action Event evt) {

    //Parse all user input values for Table generation
    rows = Integer.parseIn t(jTextFieldRow s.getText());
    cols = Integer.parseIn t(jTextFieldCol s.getText());
    width = Integer.parseIn t(jTextFieldWid th.getText());
    border = Integer.parseIn t(jTextFieldBor der.getText());

    //instantiate objects
    int rowHeight = 500/rows;
    int colWidth = 700/cols;
    String labelString;
    tableGrid = new Object[cols][rows];

    for (int y=0; y<rows; y++);
    {
    for (int x=0; x<cols; x++);
    {
    labelString = "jTextPain"+x+y ;
    JTextPane textPane = new JTextPane();
    tableGrid[x][y] = textPane;
    textPane.setSiz e(colWidth, rowHeight);
    textPane.setNam e(labelString);

    }
    }

    jPanelTable.set Layout(new GridLayout(rows ,cols));
    //Pack the panel with the objects
    for (int y=0; y<rows; y++);
    {
    for (int x=0; x<cols; x++);
    {
    JTextPane textPane = (JTextPane) tableGrid[x][y];
    jPanelTable.add (textPane);
    }
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by davedwm
    I'm trying to display a grid of TextPane's that are sized to fit a JPanel i've drawn into a Dialog class, the number of 'Cells' are entered into JTextFields and when the appropriate button is hit this method is executed. Each TextPane is stored as an object in the aray tableGrid. The code compiles fine, but when executed the JPanel remains blank.

    I've created did using a book that explains it in AWT and i'm using SWING but so far as i can tell this should work for SWING too

    Any help would be much appreciated.

    private void jButton1ActionP erformed(java.a wt.event.Action Event evt) {

    //Parse all user input values for Table generation
    rows = Integer.parseIn t(jTextFieldRow s.getText());
    cols = Integer.parseIn t(jTextFieldCol s.getText());
    width = Integer.parseIn t(jTextFieldWid th.getText());
    border = Integer.parseIn t(jTextFieldBor der.getText());

    //instantiate objects
    int rowHeight = 500/rows;
    int colWidth = 700/cols;
    String labelString;
    tableGrid = new Object[cols][rows];

    for (int y=0; y<rows; y++);
    {
    for (int x=0; x<cols; x++);
    {
    labelString = "jTextPain"+x+y ;
    JTextPane textPane = new JTextPane();
    tableGrid[x][y] = textPane;
    textPane.setSiz e(colWidth, rowHeight);
    textPane.setNam e(labelString);

    }
    }

    jPanelTable.set Layout(new GridLayout(rows ,cols));
    //Pack the panel with the objects
    for (int y=0; y<rows; y++);
    {
    for (int x=0; x<cols; x++);
    {
    JTextPane textPane = (JTextPane) tableGrid[x][y];
    jPanelTable.add (textPane);
    }
    }
    }
    after adding new components to a Frame/Panel, you need to call repaint for the changes to become visible.

    Comment

    • davedwm
      New Member
      • Apr 2007
      • 11

      #3
      Originally posted by r035198x
      after adding new components to a Frame/Panel, you need to call repaint for the changes to become visible.

      the idea of this function is to fill the panel with text boxes that are spaced apart, in the form of a grid specified by the number of rows and cols, stored as ints, their width is calculated so they are reduced to fit the more there are, however when drawn in the panel they appear a bit of a mess and its hard to see the arrangement, any advice on errors in the code would be appreciated.

      Thanks

      Comment

      • davedwm
        New Member
        • Apr 2007
        • 11

        #4
        Originally posted by davedwm
        the idea of this function is to fill the panel with text boxes that are spaced apart, in the form of a grid specified by the number of rows and cols, stored as ints, their width is calculated so they are reduced to fit the more there are, however when drawn in the panel they appear a bit of a mess and its hard to see the arrangement, any advice on errors in the code would be appreciated.

        Thanks
        Seem's to work but there is no spacing, the textpanes could be set too big, but with the parameters i have set they shouldn't be.

        Also even with the same numbers of rows and cols specified, each time the generate button is pressed, different numbers of text boxes appear, does this mean the array needs to be emptied? tho if the figures are the same i would presume it would just overwrite the old entries.
        Last edited by davedwm; Apr 9 '07, 03:06 PM. Reason: Ad more info

        Comment

        • davedwm
          New Member
          • Apr 2007
          • 11

          #5
          Originally posted by davedwm
          Seem's to work but there is no spacing, the textpanes could be set too big, but with the parameters i have set they shouldn't be.

          Also even with the same numbers of rows and cols specified, each time the generate button is pressed, different numbers of text boxes appear, does this mean the array needs to be emptied? tho if the figures are the same i would presume it would just overwrite the old entries.
          OK the last two posts can be pretty much ignored having played round with the grid layout parameters i've selected a gap between the text boxes which show how they appear, which i muist say doesn't work correctly, with the below code, the text boxes add them selves to the panel one by one, each time the method is executed the loop doesn't not seem to follow through and each text box prints the contents which tells me there must be a problem with the loop and the x and y variables which i cannot pinpoint,

          any help greatly appreciated.

          private void jButton1ActionP erformed(java.a wt.event.Action Event evt) {

          //Parse all user input values for Table generation
          rows = Integer.parseIn t(jTextFieldRow s.getText());
          cols = Integer.parseIn t(jTextFieldCol s.getText());
          width = Integer.parseIn t(jTextFieldWid th.getText());
          border = Integer.parseIn t(jTextFieldBor der.getText());

          //instantiate objects
          int rowHeight = 500/rows;
          int colWidth = 700/cols;
          String labelString;
          tableGrid = new Object[cols][rows];
          jPanelTable.set Layout(new GridLayout(rows ,cols,5,5));


          for (int y=0; y<rows; y++);
          {
          for (int x=0; x<cols; x++);
          {

          labelString = "jTextPain "+x+y;
          JTextPane textPane = new JTextPane();
          tableGrid[x][y] = textPane;
          textPane.setSiz e(colWidth, rowHeight);
          textPane.setNam e(labelString);
          textPane.setTex t(labelString);
          jPanelTable.add (textPane);

          }
          }


          jPanelTable.rep aint();

          }
          Last edited by davedwm; Apr 10 '07, 12:48 AM. Reason: forgot code

          Comment

          • davedwm
            New Member
            • Apr 2007
            • 11

            #6
            AHAHAHA

            ; at the end of the For loop statement.

            :-)

            thanks for the help

            Any tips on how to clear the panel? when you run the method again the Panel gets double packed.
            Last edited by davedwm; Apr 10 '07, 05:06 PM. Reason: added words

            Comment

            • davedwm
              New Member
              • Apr 2007
              • 11

              #7
              jPanelTable.rem oveAll();

              :-)

              Comment

              Working...