Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #152
| From | "freesoft_2000" <freesoft_2000@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: JTable Cell Renderers |
| Message-ID | <d6cd61bd2d47a854cbe65ddf5711eeec@localhost.talkaboutprogramming.com> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <0T3kvafeIs4c@uni.chka.de> |
| Date | 2011-04-27 15:25 +0000 |
| Organization | TDS.net |
To: comp.lang.java.gui
Hi everyone,
I tried writing and using the below code by extending the
DefaultCellEditor class and uisng it but it only got worse as now if i
apply the font to the selected text in the cell all the text dissapears
Here is the code of the class theat extends the default cell editor class
class CellPaneEditor extends DefaultCellEditor
{
JTextPane TextPane1 = new JTextPane();
StyleContext sc = new StyleContext();
DefaultStyledDocument dse = new DefaultStyledDocument(sc);
StyledEditorKit StyledEditorKit1 = new StyledEditorKit();
public CellPaneEditor()
{
super(new JTextField());
TextPane1.setEditorKit(StyledEditorKit1);
TextPane1.setDocument(dse);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,int row,
int column)
{
return TextPane1;
}
}
Here is the entire code using the above class which compiles
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.text.*;
public class TabTest implements ActionListener, ItemListener
{
JFrame fr = new JFrame ("Frame");
JButton Button1 = new JButton("Add Coloum");
JButton Button2 = new JButton("Add Row");
JComboBox ComboBox1;
DefaultTableModel TableModel1 = new DefaultTableModel(0, 0);
JTable Table1 = new JTable(TableModel1);
JScrollPane ScrollPane1 = new JScrollPane(Table1,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
String FontFamily = "Arial";
Dimension Size1 = new Dimension();
//add
//The below command line is the constructor for the JTextPane
JTextPane TextPane1 = new JTextPane();
//The below two command lines creates instances for fonts
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleContext sc = new StyleContext();
//The below command line sets up the variable for font updating
MutableAttributeSet mas;
//The below command line is the default document class which
//has one argument as explained below
//The first argument sets the Style Context of the styled document
DefaultStyledDocument dse = new DefaultStyledDocument(sc);
StyledEditorKit StyledEditorKit1 = new StyledEditorKit();
//end
public void initialize ()
{
Container pane = fr.getContentPane();
pane.setLayout(new FlowLayout());
fr.setSize(250,300);
fr.setLocation(300,300);
fr.setBackground(Color.lightGray);
//The below command line must be set to false so that user
//resizing is allowed
Table1.setAutoCreateColumnsFromModel(false);
Table1.setGridColor(Color.black);
TextPane1.setEditorKit(StyledEditorKit1);
TextPane1.setDocument(dse);
Size1.width = 350;
Size1.height = 250;
ScrollPane1.setPreferredSize(Size1);
Table1.setModel(TableModel1);
Table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Table1.setDefaultEditor(Object.class, new CellPaneEditor());
pane.add(ScrollPane1);
pane.add(Button1);
pane.add(Button2);
combofontfamilyinitialize();
pane.add(ComboBox1);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Button1.addActionListener(this);
Button2.addActionListener(this);
ComboBox1.addItemListener(this);
fr.pack();
fr.setVisible(true);
}
public void combofontfamilyinitialize ()
{
//This function fills the combo box with the system available font
families
GraphicsEnvironment ge1 =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] k = ge1.getAvailableFontFamilyNames();
ComboBox1= new JComboBox(k);
}
public void setAttributeSet(AttributeSet attr)
{
//This function only set the specified font set by the
//attr variable to the text selected by the mouse
int xStart, xFinish, k;
xStart = TextPane1.getSelectionStart();
xFinish = TextPane1.getSelectionEnd();
k = xFinish - xStart;
if(xStart != xFinish)
{
dse.setCharacterAttributes(xStart, k, attr, false);
}
else if(xStart == xFinish)
{
//The below two command line updates the JTextPane according to what
//font that is being selected at a particular moment
mas = StyledEditorKit1.getInputAttributes();
mas.addAttributes(attr);
}
}
public void insertcolumn (JTable table2)
{
//This function adds a column dynamically to the end of the JTable
TableModel1 = (DefaultTableModel)table2.getModel();
TableColumn col = new TableColumn(TableModel1.getColumnCount());
TableModel1.addColumn(" ");
//The below command line adds the new column to the JTable
table2.addColumn(col);
TableModel1.fireTableStructureChanged();
}
public void actionPerformed(ActionEvent event)
{
JComponent b = (JComponent)event.getSource();
int d;
String str3 = null;
String str4 = null, str5 = null;
Object Object1 = null;
Object Object2 = null;
if(b == Button1)
{
//The below command line removes the cell editor of the JTable to
//prevent any repitation of data from being added to the JTable
Table1.removeEditor();
insertcolumn(Table1);
}
else if(b == Button2)
{
//The below command line removes the cell editor of the JTable to
//prevent any repitation of data from being added to the JTable
Table1.removeEditor();
//The below two command lines creates and adds an empty object
//an a row into the current JTable
Object[] v = new Object[0];
TableModel1.addRow(v);
}
}
public void itemStateChanged(ItemEvent event)
{
JComponent c = (JComponent)event.getSource();
boolean d;
if(c == ComboBox1)
{
Table1.editCellAt(0,0);
FontFamily = (String)ComboBox1.getSelectedItem();
TextPane1 = (JTextPane)Table1.getEditorComponent();
if(TextPane1 != null)
{
StyleConstants.setFontFamily(sas, FontFamily);
setAttributeSet(sas);
Button1.setText("Received");
}
}
}
public static void main(String args[])
{
TabTest a = new TabTest();
a.initialize();
}
}
class CellPaneEditor extends DefaultCellEditor
{
JTextPane TextPane1 = new JTextPane();
StyleContext sc = new StyleContext();
DefaultStyledDocument dse = new DefaultStyledDocument(sc);
StyledEditorKit StyledEditorKit1 = new StyledEditorKit();
public CellPaneEditor()
{
super(new JTextField());
TextPane1.setEditorKit(StyledEditorKit1);
TextPane1.setDocument(dse);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,int row,
int column)
{
return TextPane1;
}
}
Why the JTable is reacting in this way i am not sure and hope someone can
point out to me why this is happening.
Sorry about the code not compiling earlier to a careless mistake of mine.
Sorry about it
Thank You
Yours Sincerely
Richard West
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar
JTable Cell Renderers "freesoft_2000" <freesoft_2000@THRWHITE.remove-dii-this> - 2011-04-27 15:25 +0000
Re: JTable Cell Renderers "usenet" <usenet@THRWHITE.remove-dii-this> - 2011-04-27 15:25 +0000
Re: JTable Cell Renderers "freesoft_2000" <freesoft_2000@THRWHITE.remove-dii-this> - 2011-04-27 15:25 +0000
Re: JTable Cell Renderers "usenet" <usenet@THRWHITE.remove-dii-this> - 2011-04-27 15:25 +0000
csiph-web