1
I’m not finding a way to organize the lines within a Jtextpane, and I need to continue with styling but also with a Word Wrapper, there is some method or some component that does similar?
public class PanelTeste extends javax.swing.JFrame {
public PanelTeste() {
initComponents();
initTeste();
}
public void initTeste() {
//"panel" é o JTextPane
scroll.setViewportView(panel);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
StyledDocument doc = panel.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
try {
doc.insertString(0, "Algo aqui", keyWord);
} catch (Exception e) {
System.out.println(e);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
scroll = new javax.swing.JScrollPane();
panel = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
scroll.setViewportView(panel);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PanelTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PanelTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PanelTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PanelTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PanelTeste().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextPane panel;
private javax.swing.JScrollPane scroll;
// End of variables declaration
}
What is word wrapper? What have you done so far? Add a [mcve] so you can test the problem.
– user28595
Word Wrapper would be to break line type at the end of the field write a big word he send down instead of cutting it in half. Jtextarea to do, no problem. Posted what I already have.
– Hamon
There is no such method in Jtextpane, only in textarea.
– user28595
Yes. I just got on the Internet some classes that do it in a "forced" way, I edit the question, I answer it myself, as I do to leave the link to anyone who might want to use?
– Hamon
You can answer your question in the field below and mark as accepted.
– user28595
This worked for me: https://www.java-forums.org/awt-swing/59790-linewrapping-jtextpane.html#post286197
– user28595
This did not work with me. Anyway, posted what worked for me.
– Hamon