JPanel in JTable cell

  • Follow


I have JTable  , and i want to have in each cell of table some custome 
panel whith buttons and textboxes ,the same in every cell.Is it posible 
to catch an event from components in that panel?

i would appritiate advice ,thnx
0
Reply helena 6/28/2005 8:35:12 AM

Hi,

Try writing custom renderers and editors for your table

0
Reply ravi 6/28/2005 10:49:07 AM


Creating custom cell renderer is easy by using QuickTable. QuickTable
is a free java component (http://quicktable.org) which is an
implementation on top of JTable , but hides all the complexities and
allows to use the JTable methods. So you can just replace Jtable with
QuickTable.


For example, we create a File selector cell using a JPanel and JButton.



  //we create a File selecter cell editor
  //this has a label & a button, when user wants to change the file to
something else
  //instead of directly editing this cell, they click on the button
which opens a file
  // dialog and user selects the file

  class FileCell extends JPanel implements CellComponent
  {
      JLabel jl = new JLabel();
      JButton jb = new JButton("...");
      JFileChooser jf;

      public FileCell()
      {
        //use a border layout , rather than the used flow layout
        add(jl);
        add(jb);

        // add a actionlistener to open file dialog when user clicks on
the button
        jb.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
              if( jf == null)
              {
                jf = new JFileChooser();
              }

              String val = jl.getText();

              if(  val != null   &&   !("".equals(val)) )
              {
                jf.setCurrentDirectory(new File(val));
              }

              int returnVal = jf.showOpenDialog(null);

              if(returnVal == JFileChooser.APPROVE_OPTION)
                jl.setText(jf.getSelectedFile().getPath());
          }
        });
      }

      public void setValue(Object value)
      {
        jl.setText( (String)(value==null?"":value ));
      }

      public Object getValue()
      {
        String val = jl.getText();

        if(  val != null   &&   !("".equals(val))  )
          return val;
        else
          return null;
      }


      public JComponent getComponent()
      {
         return this;
      }

  }

0
Reply Liz 7/12/2005 7:08:11 PM

2 Replies
585 Views

(page loaded in 0.061 seconds)

Similiar Articles:













7/22/2012 6:37:30 PM


Reply: