Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

jCheckBox in a jTable

Thread view: 
Eefke Gemmeke - 15 Dec 2003 22:30 GMT
Hi all,
i don't manage to set a jCheckBox in a jTable...
Can anyone tell me what's wrong with my code.
Thanks

//******************************************************************
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.lang.*;
import javax.swing.JTable;

public class Sans_titre1  extends JPanel  implements TableModelListener {
 private final String[] columnNames = { "Rayon", "Produit", "Quantit?",
"Achet?"};
 private JButton buttonSave;
 private JTable table;
 private DefaultTableModel tableModel;

//******************************************************************
  public Sans_titre1() {
   tableModel = new DefaultTableModel(columnNames, 0);

    tableModel.addTableModelListener(this);

    Object[] data = { new String("Frais"), new String("Pommes"), new
Integer(5), new Boolean(true)};
    tableModel.addRow(data);

    table = new JTable(tableModel);

    javax.swing.table.TableColumn var_col;
    var_col = table.getColumnModel().getColumn(3);
    JCheckBox check = new JCheckBox();
    var_col.setCellEditor(new DefaultCellEditor(check));

    JScrollPane scrollPane = new JScrollPane(table);

    setLayout(new BorderLayout());
    setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    add(BorderLayout.NORTH, new JLabel("Mon panier", JLabel.CENTER));
    add(BorderLayout.CENTER, scrollPane);
    JButton buttonAdd = new JButton("Ajouter");
    buttonAdd.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        Object[] data = {
            new String("Test"), new String("Test"), new Integer(0),
            new Boolean(false)};
        tableModel.addRow(data);
      }
    });
    buttonSave = new JButton("Sauver");
    buttonSave.setEnabled(false);
    buttonSave.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        buttonSave.setEnabled(false);
      }
    });
    JPanel buttonsPanel = new JPanel();
    buttonsPanel.add(buttonAdd);
    buttonsPanel.add(buttonSave);

    add(BorderLayout.SOUTH, buttonsPanel);
    setPreferredSize(new Dimension(200, 250));
  }

   public void tableChanged(TableModelEvent e) {
     if (e.getType() == TableModelEvent.UPDATE) {
       buttonSave.setEnabled(true);
     }
   }
//******************************************************************
  public static void main(String[] args) {
    JFrame frame = new JFrame("Mon Panier");

    frame.getContentPane().setLayout(new BorderLayout());

    frame.getContentPane().add("Center", new Sans_titre1());

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };

    frame.addWindowListener(wndCloser);

    frame.pack();

    frame.show();
  }
}
//******************************************************************
Thomas Fritsch - 16 Dec 2003 12:48 GMT
>Hi all,
>i don't manage to set a jCheckBox in a jTable...

When running your code, I saw in column "Acheté", that this column is
rendered as a "true"/"false" String.
Only when clicking into it I saw a JCheckBox (with the check mark on/off)=2E
I suppose this is the problem you mean.

>Can anyone tell me what's wrong with my code.
>Thanks

The reason is, that you have managed to get a JCheckBox as the
TableCellEditor of column 3,
but still have the default JLabel as its TableCellRenderer.

>...
>public class Sans_titre1  extends JPanel  implements TableModelListener {
>  private final String[] columnNames = { "Rayon", "Produit", "Quantité", "Acheté"};

   private final Class[] columnClasses         { String.class, String.class, Integer.class, Boolean.class };

>  private JButton buttonSave;
>  private JTable table;
[quoted text clipped - 3 lines]
>   public Sans_titre1() {
>    tableModel = new DefaultTableModel(columnNames, 0)

     {
       // Override getColumnClass (the superclass always returned
Object.class).
       // The table will then select an appropriate CallEditor and(!)
CellRenderer
       // according to the returned column class.
       public Class getColumnClass(int column)
       {
         return columnClasses[column];
       }
     }

>;
>
[quoted text clipped - 5 lines]
>     table = new JTable(tableModel);
>  

    // You don't need the following 4 lines anymore, because of the
getColumnClass overriding above.

>     // javax.swing.table.TableColumn var_col;
>     // var_col = table.getColumnModel().getColumn(3);
[quoted text clipped - 6 lines]
>
>...

Greetings
   Thomas
Todd Corley - 16 Dec 2003 13:50 GMT
change
    var_col.setCellEditor(new DefaultCellEditor(check));
to
    var_col.setCellEditor( table.getDefaultEditor( Boolean.class ) );
    var_col.setCellRenderer( table.getDefaultRenderer( Boolean.class ) );


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.