Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> package infonode; /** * * @author Deepak */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.table.*; import javax.swing.text.*; public class CoolTable extends AbstractTableModel{ public static void main(String[] arg){ JFrame f = new JFrame("Cool Table"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable t = new JTable(new CoolTable()); t.setDefaultRenderer(Object.class, new StyledCellRenderer()); t.setDefaultEditor(Object.class, new StyledCellEditor()); t.setRowHeight(50); System.out.println(t.isCellEditable(0, 0)); f.add(new JScrollPane(t)); f.pack(); f.setVisible(true); } private Object[] data; /** Creates a new instance of CoolTable */ public CoolTable() { final String[] dataSrc = "This is a test".split(" "); data = new Object[dataSrc.length]; System.arraycopy(dataSrc, 0, data, 0, data.length); } public int getRowCount() { return data.length; } public int getColumnCount() { return 1; } public boolean isCellEditable(int r, int c) { return true; } public Object getValueAt(int r, int c){ if(data[r] instanceof String) { try{ DefaultStyledDocument doc = new DefaultStyledDocument(); doc.insertString(0, data[r].toString(), null); data[r] = doc; } catch(BadLocationException x) { x.printStackTrace(System.err); System.exit(1); } } return data[r]; } public void setValueAt(int r, int c, Object val){ data[r] = val instanceof DefaultStyledDocument ? val : val.toString(); } public static class StyledCellRenderer implements TableCellRenderer { private static Border etchedBorder = BorderFactory.createEtchedBorder(); private JTextComponent styledRenderer = new JTextPane(); public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { styledRenderer.setDocument((Document)value); styledRenderer.setBackground(isSelected ? Color.LIGHT_GRAY : Color.WHITE); styledRenderer.setBorder(hasFocus ? etchedBorder : null); return styledRenderer; } } public static class StyledCellEditor extends DefaultCellEditor { private JTextPane styledEditor = new JTextPane(); public StyledCellEditor() { super(new JTextField()); editorComponent = new JScrollPane(styledEditor); installActions(); delegate = new DefaultCellEditor.EditorDelegate() { public void setValue(Object val) { styledEditor.setDocument((Document)val); } public Object getCellEditorValue() { return styledEditor.getDocument(); } }; styledEditor.addFocusListener(new FocusAdapter(){ public void focusLoast(FocusEvent e){ delegate.stopCellEditing(); } }); } private void installActions() { styledEditor.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.ALT_MASK), "superscript" ); styledEditor.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.ALT_MASK), "subscript" ); styledEditor.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK), "bold" ); styledEditor.getActionMap().put("superscript", new SuperscriptAction(true)); styledEditor.getActionMap().put("subscript", new SuperscriptAction(false)); styledEditor.getActionMap().put("bold", new StyledEditorKit.BoldAction()); } } private static class SuperscriptAction extends AbstractAction { private MutableAttributeSet superscript = new SimpleAttributeSet(); public SuperscriptAction() { this(true); } public SuperscriptAction(boolean isSuperscript) { StyleConstants.setSuperscript(superscript, isSuperscript); } public void actionPerformed(ActionEvent e) { final JTextPane styledEditor = (JTextPane)e.getSource(); styledEditor.getStyledDocument().setCharacterAttributes( styledEditor.getSelectionStart(), styledEditor.getSelectionEnd() - styledEditor.getSelectionStart(), superscript, false ); } } } </code></pre> <p>I hope it will help you :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload