First letter styled - java

I want to have a styled text in JTextPane, here is a listener to JTextPane:
private CaretListener listener = new CaretListener() {
#Override
public void caretUpdate(CaretEvent e) {
StyleConstants.setBold(sas, false);
helpTextPane.setCharacterAttributes(sas, true);
}
};
the problem ist that the first letter is bold (i do not want it):
I know that the problem is with the attributes and i need resetet these attributes but how ?

Use InputAttributes to set/reset the bold.
MutableAttributeSet sas=((StyledEditorKit)yourTextPane.getEditorKit()).getInputAttributes();

Related

Select All text inside CellEditor

I have a tableViewer where I can edit 1 column. Everytime the cellEditor from this column gains focus I want all the text that is displayed to be selected. In a normal SWT text control, we would just do text.selectAll(); , so I've set this listener for the the cellEditor inside the column EditingSupport class:
editor.getControl().addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
Text text = (Text) editor.getControl();
text.selectAll();
}
});
I know I can cast the cellEditor control to a Text input because I've tested it with a normal setText(); and it Works! However, the selectAll(); is not working, how can I fix this?
SOLUTION
I found the error, the code in my question above works perfectly, the reason I wasn't able to see the selectAll(); doing something is in this method:
#Override
protected Object getValue(Object element) {
String valor = MathUTILS.getBigDecimalFormatted(((ColaboradoresDespesasDocumentosLinhas) element).getValor(), PatternVARS.PATTERN_BIGDECIMAL_MILLION);
Text text = InterfaceFormatUTILS.getControlNumberFormat(editor, PatternVARS.PATTERN_BIGDECIMAL_BILLION);
return valor;
}
This is also a method from the EditingSupport class, and for some reason formatting the text control (Text text = InterfaceFormatUTILS.getControlNumberFormat(editor, PatternVARS.PATTERN_BIGDECIMAL_BILLION);) deselects all the text. Also, doing the selectAll(); in this method doesn't work..

Tabbed JTextField selection workaround doesn't work for JFormattedTextField

I've added a FocusAdapter to my JFormattedTextField to select its contents when focus is lost, so that the text is selected when tabbed back into but not clicked into.
final FocusAdapter listener = new FocusAdapter()
{
#Override
public void focusLost(FocusEvent e)
{
super.focusLost(e);
AccessibleEditableText text = e.getComponent().getAccessibleContext().getAccessibleEditableText();
text.selectText(0, text.toString().length());
}
};
yearPublishedTextField.addFocusListener(listener);
However, this doesn't work for this particular text field since I added the following formatter:
NumberFormat nf = NumberFormat.getIntegerInstance();
nf.setGroupingUsed(false);
yearPublishedTextField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(nf)));
I've noticed that, on reacquiring focus, the caret gets set to the zero position. Can anyone explain why the formatter is doing this, and suggest a way to work around this issue?
The fact that a formatter was updating the field made my workaround useless, so I solved this by simply formatting the yearPublishedTextField using regex replacement, as I only needed basic formatting anyway.
yearPublishedTextField.addFocusListener(new FocusAdapter()
{
#Override
public void focusLost(FocusEvent e)
{
super.focusLost(e);
yearPublishedTextField.setText(yearPublishedTextField.getText().replaceAll("[^0-9]", ""));
yearPublishedTextField.setSelectionStart(0);
yearPublishedTextField.setSelectionEnd(yearPublishedTextField.getText().length());
}
});

Nebula gridTableViewer cell content should wrap

I am using nebula gridTableViewer for my application, in its cell some time content is so lengthy, so I want to show as WRAP (in multiline). I did changes for it but it is not reflecting:
I added SWT.WRAP but not works, I tried to wrap the text in LabelProvider too but it also not works so how could i do it?
Should i need to add listener for this.
Adding SWT.WRAP to the gridTableViewer would not cause the Text in the grid cells to wrap.
gridColumn.setWordWrap(true) causes the text entered in the cell to be wrapped after the entered value is applied to the editor(setvalue called). To wrap the text interactively, SWT.WRAP should be added as a style to the TextCellEditor or to Text widget placed in the cells.
gridViewerColumn.getColumn.setWordWrap(true);
gridTableViewer.setEditingSupport(new EditingSupport(gridTableViewer) {
....
#Override
protected CellEditor getCellEditor(Object element) {
TextCellEditor cellEditor = new TextCellEditor(gridTableViewer.getGrid(),
SWT.WRAP)
return cellEditor;
}
Or
....
final GridItem item = grid.getItem(grid.getTopIndex());
for (int i = 0; i < grid.getColumnCount(); i++) {
final Text text = new Text(grid, SWT.WRAP);
Listener textListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut:
item.setText(column, text.getText());
....
}
}
}
}
I have used the SWT.WRAP in the TextCellEditor before, and it works.

Keeping the format on text retrieval

I am making a network application that has a chat function. On the chat I have one JTextPane
for displaying messages and one more for input. Then I have some buttons that allow to add style on the input text(bold,italic,font size,colour). The text is formatted correctly on input pane , although when moved to the display pane(once the correct JButton is pressed) it only has the format of last character. How can I move the text while keeping its original format?For example if I write "Hello Worl d" on the input , display shows "Hello Worl d"
textPane is the input pane
Where set :
final SimpleAttributeSet set = new SimpleAttributeSet();
Code for making input text bold(same of adding other styles) :
bold.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
StyledDocument doc = textPane.getStyledDocument();
if (StyleConstants.isBold(set)) {
StyleConstants.setBold(set, false);
bold.setSelected(false);
} else {
StyleConstants.setBold(set, true);
bold.setSelected(true);
}
textPane.setCharacterAttributes(set, true);
}
});
code for moving text from the input pane to the display pane :
getInput.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String input = textPane.getText();
textPane.setText("");
if(!input.endsWith("\n")){
input+="\n";
}
StyledDocument doc = displayPane.getStyledDocument();
int offset = displayPane.getCaretPosition();
try {
doc.insertString(offset, input, set);
} catch (BadLocationException ex) {
Logger.getLogger(ChatComponent.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Use the example to merge both Documents
http://java-sl.com/tip_merge_documents.html

Getting SWT StyledText widget to always scroll to its end

How do I get a SWT StyledText widget to always stay scrolled to the end it even as new lines of text gets appended to it?
I tried to look for some functions that could allow me to set the scroll position but I can't find any. There isn't a property that lets me do this either.
Simply add this line, after you've added text:
styledText.setTopIndex(styledText.getLineCount() - 1);
If you change the content of your StyledText on more than one place, use a listener on Modify, to not repeat yourself:
styledText.addListener(SWT.Modify, new Listener(){
public void handleEvent(Event e){
styledText.setTopIndex(styledText.getLineCount() - 1);
}
});
Another variation:
styledText.addModifyListener(new ModifyListener() {
#Override
public void modifyText(ModifyEvent e) {
styledText.setTopIndex(styledText.getLineCount() - 1);
}
});

Categories

Resources