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 / March 2004

Tip: Looking for answers? Try searching our database.

JTable cellEditor and Renderer

Thread view: 
Carl Austin - 30 Mar 2004 14:21 GMT
I have a JTable that contains cells which need to be JTextPanes with the
StyledDocument set to a custom one that I have written. I have created a
cell editor and renderer that does this but unfortunately this only creates
one instance of an EditorTextPane (subclass of JTextPane) for all of the
cells and just changes the value of the text within. This means that all the
cells in all rows are set to the same size when I set the size of just one
EditorTextPane as this is in effect the same EditorTextPane as all the rest
of the cells.
My question is, how can I go about creating a renderer and editor that uses
a different EditorTextPane for each cell of the table so resizing can be
done separately for all.
Below is included the editor/renderer class and also how I assign these to
the JTable.

Thanks

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;

public class EditorCellRenderer extends AbstractCellEditor implements
TableCellEditor, TableCellRenderer {

private EditorTextPane cellText = new EditorTextPane();

   protected EventListenerList listenerList = new EventListenerList();
   protected ChangeEvent changeEvent = new ChangeEvent(this);

public EditorCellRenderer(EditorTextPane secPane, JPanel tablePanel, JTable
table, JScrollPane tableScroll) {

 //set up the text pane with the correct document
 cellText.getDocument().addUndoableEditListener(new
EditorUndoableEditListener(Editor.undoAction, Editor.redoAction,
Editor.undo));
 SimpleAttributeSet attrs = new SimpleAttributeSet();
       StyleConstants.setFontFamily(attrs, "SansSerif");
       StyleConstants.setFontSize(attrs, 12);
       cellText.setCharacterAttributes(attrs, false);
 TrackChangesStyledDocument trackDoc = new
TrackChangesStyledDocument(cellText, secPane, tablePanel, table,
tableScroll);
 cellText.setStyledDocument(trackDoc);
 cellText.setBorder(BorderFactory.createLineBorder(Color.black));

}

   public void addCellEditorListener(CellEditorListener listener) {

      listenerList.add(CellEditorListener.class, listener);

   }

   public void removeCellEditorListener(CellEditorListener listener) {

      listenerList.remove(CellEditorListener.class, listener);

   }

   protected void fireEditingStopped() {

 CellEditorListener listener;
 Object[] listeners = listenerList.getListenerList();
 for (int i = 0; i < listeners.length; i++) {

  if (listeners[i] == CellEditorListener.class) {

     listener = (CellEditorListener) listeners[i + 1];
     listener.editingStopped(changeEvent);

  }

 }

   }

   protected void fireEditingCanceled() {

 CellEditorListener listener;
 Object[] listeners = listenerList.getListenerList();
 for (int i = 0; i < listeners.length; i++) {

  if (listeners[i] == CellEditorListener.class) {

     listener = (CellEditorListener) listeners[i + 1];
     listener.editingCanceled(changeEvent);

  }

 }

   }

   public void cancelCellEditing() {

      fireEditingCanceled();

   }

   public boolean stopCellEditing() {

 fireEditingStopped();
 return true;

   }

   public boolean isCellEditable(EventObject event) {

      return true;

   }

   public boolean shouldSelectCell(EventObject event) {

      return true;

   }

   public Object getCellEditorValue() {

      return cellText.getText();

   }

public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {

       if (value != null) {

        cellText.setText(value.toString());

       } else {

        cellText.setText("");

       }
 return cellText;

}

public Component getTableCellRendererComponent(JTable table,
 Object value, boolean isSelected, boolean hasFocus, int row, int column) {

       if (value != null) {

        cellText.setText(value.toString());

       } else {

        cellText.setText("");

       }
 return cellText;

   }

}

code to set the renderer/editor for the table:

 table.setDefaultEditor(Object.class, new EditorCellRenderer(secPane,
tablePanel, table, tableScroll));
 table.setDefaultRenderer(Object.class, new EditorCellRenderer(secPane,
tablePanel, table, tableScroll));

Carl
Carl Austin - 30 Mar 2004 16:10 GMT
Please ignore this question, bad day today and wasn't thinking straight. The
simple answer is just to store each JTextPane within the Table model and
write a renderer and editor that displays them correctly. Easy!

Carl

> I have a JTable that contains cells which need to be JTextPanes with the
> StyledDocument set to a custom one that I have written. I have created a
[quoted text clipped - 166 lines]
>
> Carl


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



©2008 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.