I made few checkboxes using swing in Java. I want to write a superscript text for the checkboxes but I'm not sure how.
The code currently looks like this.
JCheckBox hCheckBox = new JCheckBox("[M + H]+");
I want to have "+" sign inside the JCheckBox parameter superscripted. What's an easy way to do this?
Thank you in advance.
Java buttons support html in their text. You need to format the string a little differently though. Try this:
JCheckBox hCheckBox = new JCheckBox("<html>[M + H]<sup>+</sup></html>");
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
);
}
}
}
I hope it will help you :)
来源:https://stackoverflow.com/questions/3925917/how-do-i-write-superscript-word-for-checkbox-text-in-java