0

I have a JTable 2 columns, column name and check-box. If the user clicks the check box next to the name of the column that creates a new field for data entry. I wish the unclick check box input field has disappeared.

How can I do that?

enter image description here

My code to add a new field:

 headerTable.getModel().addTableModelListener(new TableModelListener() {

                @Override
                public void tableChanged(TableModelEvent e) {
                    if(e.getColumn() >= 0  && e.getFirstRow()>-1){
                        int id =  e.getFirstRow();
                        String colName = (String)headerTable.getValueAt(e.getFirstRow(), 1);
                        boolean colValue = (boolean)headerTable.getValueAt(e.getFirstRow(), 2);

                        System.out.println("Row : " + e.getFirstRow() +
                                   " value :" + headerTable.getValueAt(e.getFirstRow(), 2));
                        appListener.getColumnId(id);
                        //create texfield
                        if(colValue==false){
                            System.out.println("Delete");
                        }                         
                        jTextField = new JTextField(20);
                        textField.put(id,jTextField);
                        if (textField != null && !textField.isEmpty()) {
                            textField.get(textField.size()-1);
                            System.out.println("Add");
                        }
                        JLabel kolor1name = new JLabel(colName+": ");
                        operationContent.add(kolor1name,"");
                        operationContent.add(jTextField, "growy, wrap");

                        revalidate();
                        repaint();
                    }
                }
            });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
lukassz
  • 3,135
  • 7
  • 32
  • 72

2 Answers2

1

Quick-n-dirty way:

if(colValue){
    jTextField = new JTextField(20);

    textField.put(id,jTextField);

    if (textField != null && !textField.isEmpty()) {
        textField.get(textField.size()-1);
        System.out.println("Add");
    }

    JLabel kolor1name = new JLabel(colName+": ");
    operationContent.add(kolor1name,"");
    operationContent.add(jTextField, "growy, wrap");

} else {                         
    System.out.println("Delete");
    Component[] comps = operationContent.getComponents();
    operationContent.remove(comps[comps.length - 2]);
    operationContent.remove(comps[comps.length - 1]);
}
revalidate();
repaint();

But this way may (not always) cause layouting issues (panel looks bad after removal).

Better way: provide full relayout of the panel "operationContent"

operationContent.removeAll();
// add all the components above the kolor1name
if(colValue){
    jTextField = new JTextField(20);

    textField.put(id,jTextField);

    if (textField != null && !textField.isEmpty()) {
        textField.get(textField.size()-1);
        System.out.println("Add");
    }

    JLabel kolor1name = new JLabel(colName+": ");
    operationContent.add(kolor1name,"");
    operationContent.add(jTextField, "growy, wrap");
}
revalidate()
repaint();

This way is ok, but in some cases can also provide layouting issues. But if you want to add more than 1 row (for example when user select 2 checkboxes in table) it's colud be the best way for you.

Another way: use the CardLayout. This only will work when you always add one field (with label) independed on how many comboboxes in the table was selected.

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • The display is ok, but it always removes the last element that is on the list, but not always the case though. For example. Will I choose the second element, third, and fifth then unclick the second, and the list clears my fifth and not second. – lukassz Feb 23 '15 at 23:52
  • @user3193748 try the second way. simply remove all the fields an provide a complete relayouting depending on the selected checks. – Sergiy Medvynskyy Feb 24 '15 at 08:05
1

You can create a Vector of all the fields that you want. And when the user clicks on the check-box, you can add it to the Vector. for eg:

Vector fields = new Vector();

And when you get the notification, you add the name of the field in the vector, and call the refreshPanel() method, which removes all the fields in it, and add the new fields to it.

// In the tableChanged(...) method
fields.addElement(colname);
refreshPanel();

Then the refreshPanel() method:

public void refreshPanel()
{
     String fNames[] = (String[]) fields.toArray();
     panel.removeAll();
     for(String fname : fNames)
     {
          //add the fields in `newPanel`, the field name is in `fname` variable
     }
     revalidate();
     repaint();
}

And when the user deselects the check-box, then remove the field name from the Vector:

fields.removeElement(colname);
refreshPanel();
dryairship
  • 6,022
  • 4
  • 28
  • 54