Results 1 to 2 of 2
  1. #1
    plesken is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Dynamically create n-column and n-rows forms in awt/swing

    Hi

    I'm newbie on this forum.
    I have no idea how code events when dynamically generate form with n-columns and n-rows.

    I explaine it on short example code:

    Java Code:
    for (int i = 1; i <= array.size(); i++) {
         JSONObject obj_i = (JSONObject) array.get(i);
         int idd = Integer.valueOf(obj_i.get("id").toString());
         int weight = Integer.valueOf(obj_i.get("weight").toString());
     
         if (Boolean.valueOf(obj_i.get("checkbox").toString())) {
            final JCheckBox chkbx_suffix_idd_i;
     
            switch (weight) {
            case 0:
                chkbx_suffix_idd_i =  new CheckBox(obj_i.get("desc").toString());
                chkbx_suffix_idd_i.setFont(new Font("Dialog", Font.BOLD, 13));
                break;
            case 1:
                chkbx_suffix_idd_i =  new JCheckBox("  " + obj_i.get("desc").toString());
                chkbx_suffix_idd_i.setFont(new Font("Dialog", Font.PLAIN, 12));
                break;
            case 2:
                chkbx_suffix_idd_i =  new JCheckBox("    " + obj_i.get("desc").toString());
                chkbx_suffix_idd_i.setFont(new Font("Dialog", Font.PLAIN, 12));
                break;
            default:
                chkbx_suffix_idd_i =  new JCheckBox(obj_i.get("desc").toString());
                chkbx_suffix_idd_i.setFont(new Font("Dialog", Font.PLAIN, 12));
                break;
            }
     
            chkbx_suffix_idd_i.setToolTipText("Kliknięcie spowoduje rozwinięcie pola o pozycje bardziej szczegółowe.");
            GridBagConstraints gbc_chkbx_suffix_idd_i = new GridBagConstraints();
            gbc_chkbx_suffix_idd_i.anchor = GridBagConstraints.WEST;
            gbc_chkbx_suffix_idd_i.insets = new Insets(0, 0, 5, 5);
            gbc_chkbx_suffix_idd_i.gridx = 0;
            gbc_chkbx_suffix_idd_i.gridy = i;
            chkbx_suffix_idd_i.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent arg0) {
                     if (chkbx_suffix_idd_i.getSelectedObjects() != null) {
                           chkbx_suffix_idd_i.setVisible(true);
                     } else {
                           chkbx_suffix_idd_i.setVisible(false);
                     }
                 }
            });
            panel.add(chkbx_suffix_idd_i, gbc_chkbx_suffix_idd_i);
       } else {
       final JLabel lbl_suffix_idd_i;
     
       switch (weight) {
          case 0:
              lbl_suffix_idd_i = new JLabel(obj_i.get("desc").toString());
              lbl_suffix_idd_i.setFont(new Font("Dialog", Font.BOLD, 13));
              break;
          case 1:
              lbl_suffix_idd_i = new JLabel("       " + obj_i.get("desc").toString());
              lbl_suffix_idd_i.setFont(new Font("Dialog", Font.PLAIN, 12));
              break;
          case 2:
              lbl_suffix_idd_i = new JLabel("          " + obj_i.get("desc").toString());
              lbl_suffix_idd_i.setFont(new Font("Dialog", Font.PLAIN, 12));
              break;
          case 3:
              lbl_suffix_idd_i = new JLabel("             " + obj_i.get("desc").toString());
              lbl_suffix_idd_i.setFont(new Font("Dialog", Font.PLAIN, 12));
              break;
          default:
              lbl_suffix_idd_i = new JLabel(obj_i.get("desc").toString());
              lbl_suffix_idd_i.setFont(new Font("Dialog", Font.PLAIN, 12));
              break;
          }
     
          GridBagConstraints gbc_lbl_suffix_idd_i = new GridBagConstraints();
          gbc_lbl_suffix_idd_i.insets = new Insets(0, 0, 5, 5);
          gbc_lbl_suffix_idd_i.anchor = GridBagConstraints.WEST;
          gbc_lbl_suffix_idd_i.gridx = 0;
          gbc_lbl_suffix_idd_i.gridy = i;
          panel.add(lbl_suffix_idd_i, gbc_lbl_suffix_idd_i);
        }
     
        for (int k = 0; k <= period; k++) {       
           int a = k + 1;
           final JTextField textField_suffix_idd_k = new JTextField();
     
           switch (weight) {
           case 0:
                 textField_suffix_idd_k.setFont(new Font("Dialog", Font.BOLD, 12));
                 break;
           case 1:
                 textField_suffix_idd_k.setFont(new Font("Dialog", Font.PLAIN, 12));
                 break;
           default:
                 textField_suffix_idd_k.setFont(new Font("Dialog", Font.PLAIN, 12));
                 break;
           }
     
           textField_suffix_idd_k.setBackground(Color.WHITE);
           textField_suffix_idd_k.setColumns(5);
           GridBagConstraints gbc_textField_suffix_idd_k= new GridBagConstraints();
           gbc_textField_suffix_idd_k.fill = GridBagConstraints.HORIZONTAL;
           gbc_textField_suffix_idd_k.insets = new Insets(0, 0, 5, 5);
           gbc_textField_suffix_idd_k.gridx = a;
           gbc_textField_suffix_idd_k.gridy = i;
           panel.add(textField_suffix_idd_k, gbc_textField_suffix_idd_k);
      }
    The array is a json tab.
    It's generating label and textfield:

    Plain text row 1 textfield11 textfield12 textfield13
    Plain text row 2 textfield21 textfield22 textfield23
    Plain text row 3 textfield31 textfield32 textfield33
    ....
    Plain text row N textfieldN1 textfieldN2 textfieldN3

    The problem which I would like to solve:
    a) I check checkbox on "Plain text row 1" and hide "Plain text row 2", "Plain text row 3", textfield21, textfield22, textfield23, textfield31, textfield32, textfield33
    b) sum of textfield21 and textfield31 equals textfield11 (textfield22 and textfield32 equals textfield12, etc..)

    I have no idea how programm it. Is anybody who help me?
    Thaks a lot.

    Plesken

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Dynamically create n-column and n-rows forms in awt/swing

    Break your problem up into smaller pieces, and concentrate on a single piece at a time instead of posting a hundred lines of code. You want to be able to hide and show components? Use CardLayout. You want to be able to set the content of a component based on actions taken in another? Use a Listener of some kind. If you need more help, then put together an SSCCE demonstrating the specific piece you're working on.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. how to add rows dynamically in JTable
    By tweet in forum AWT / Swing
    Replies: 1
    Last Post: 03-05-2012, 10:59 AM
  2. how to add rows dynamically and retrieving values in jsp
    By Gopinathsachin in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-27-2011, 03:06 PM
  3. Adding and Deleting rows in jTable dynamically?
    By asifzbaig in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2011, 09:32 PM
  4. Fix the rows and column of JTextPane
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 09-09-2010, 08:16 PM
  5. Replies: 1
    Last Post: 07-06-2010, 04:50 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
  •