Olá pessoal, sou novo no forúm e novo em java tambem pra falar a verdade comecei com java a três dias,
eu já programava PHP então OO pra mim foi facinho, a unica coisa que não estou me dando muito bem é swing,
ai pra isso eu uso o netBeans.
Então vamos ao meu problema eu encontrei esse pacote javax.swing.text.html que pode ser utilizado para criar um editor mais o problema é a forma que eu aplico ele procurei vários tutoriais mais não concegui aplicar nenhuma de suas classes.
Se alguem me ajudar a deixar só um texto em negrito utilizando o netbenas o resto eu consigo me virar;
já tenho um JEditorPane, no meu caso está nomeado de campo e um botao para deixar em negrito no caso botaoNegrito.
Cara acho que eu sou muito burro,
quando você me falou do StyledEditorKit.StyledTextAction eu procurei na internet sobre o assunto e até achei esse metodo ai mais não consegui aplicar ao meu botão,
segue o código abaixo:
importjava.awt.event.ActionEvent;importjavax.swing.JEditorPane;importjavax.swing.text.MutableAttributeSet;importjavax.swing.text.SimpleAttributeSet;importjavax.swing.text.StyleConstants;importjavax.swing.text.StyledEditorKit;/* * editor.java * * Created on 10 de Outubro de 2010, 09:50 *//** * * @author Carlos Maniero */publicclasseditorextendsjavax.swing.JFrame{/** Creates new form editor */publiceditor(){initComponents();botaoNegrito.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){actionPerformed(evt);}});}publicclassBoldActionextendsStyledEditorKit.StyledTextAction{publicBoldAction(){super("");}publicvoidactionPerformed(ActionEvente){JEditorPaneeditor=getEditor(e);if(editor!=null){StyledEditorKitkit=getStyledEditorKit(editor);MutableAttributeSetattr=kit.getInputAttributes();booleanbold=(StyleConstants.isBold(attr))?false:true;SimpleAttributeSetsas=newSimpleAttributeSet();StyleConstants.setBold(sas,bold);setCharacterAttributes(editor,sas,false);}}}/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */// <editor-fold defaultstate="collapsed" desc=" Generated Code "> privatevoidinitComponents(){jScrollPane1=newjavax.swing.JScrollPane();editor=newjavax.swing.JEditorPane();botaoNegrito=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jScrollPane1.setViewportView(editor);botaoNegrito.setText("negrito");javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,400,Short.MAX_VALUE).addComponent(botaoNegrito,javax.swing.GroupLayout.DEFAULT_SIZE,400,Short.MAX_VALUE));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,254,Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(botaoNegrito,javax.swing.GroupLayout.PREFERRED_SIZE,23,javax.swing.GroupLayout.PREFERRED_SIZE)));pack();}// </editor-fold> /** * @param args the command line arguments */publicstaticvoidmain(Stringargs[]){java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){neweditor().setVisible(true);}});}// Variables declaration - do not modify privatejavax.swing.JButtonbotaoNegrito;privatejavax.swing.JEditorPaneeditor;privatejavax.swing.JScrollPanejScrollPane1;// End of variables declaration }
Cara funcionou!!!
o problema é que eu colocava o código depois do metodo initComponents(); porque o netbeans não permite editar o código que ele gera,
ai eu abri com a melhor IDE que já inventaram (notepad)…rs
ai funcionou certinho.