I'm using ModifyListener to get information from Text that changes.
Is there a way to get the Text's text in the listener (like a method of Event e)?
Thank you!
Next example helps you :
Text text = new Text(shell, SWT.SINGLE);
text.addModifyListener(new ModifyListener() {
#Override
public void modifyText(ModifyEvent e) {
Text source = (Text) e.getSource();
System.out.println("text = " + source.getText());
}
});
text.pack();
According to docs e.getSource() return:
The object on which the Event initially occurred.
Related
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object selected = c.getSelectedItem();
s = (String)selected;
tf1.setText(s);
}
});
when setting text it gives error attempt to mutate in notification. besides this if i write label.setText(s); it gives no error and set text according to selected item in combo... c is JComboBox object
I have two RadioButton to select either Italic or Plain Style for a TextArea.(They are added to ButtonGroup so only one can be selected)
I have the actionPerformed method as
public void actionPerformed(ActionEvent ae) {
Font currentFont = taText.getFont(); //taText is TextArea
Font fontToSet;
String command = ae.getActionCommand();
if (command.equals("Italic")) {
System.out.println("Italic clicked");
fontToSet = new Font(currentFont.getFontName(),Font.ITALIC,currentFont.getSize());
}
else {
System.out.println("Plain clicked");
fontToSet = new Font(currentFont.getFontName(), Font.PLAIN, currentFont.getSize());
}
taText.setFont(fontToSet);
}
The problem is that when I click on "Italic", the text becomes italic, but when clicking Plain, text does not become plain. Yet the message Plain Clicked gets displayed on command line.
What is the problem?
change your code to
public void actionPerformed(ActionEvent ae) {
Font currentFont = taText.getFont(); //taText is TextArea
Font fontToSet;
String command = ae.getActionCommand();
if (command.equals("Italic")) {
System.out.println("Italic clicked");
fontToSet =currentFont .deriveFont(Font.ITALIC);
else {
System.out.println("Plain clicked");
fontToSet = currentFont .deriveFont(Font.PLAIN);
}
taText.setFont(fontToSet);
}
the problem is , getFontName() calls Font2D to get the name and it returns the value based on the current applied styles, but getName() will still return the same font name. but better to use deriveFont();
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..
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
I have a JTextPane that displays HTML text. HTML text has hyperlinks with tags ...
I want to invoke a java function when the user clicks on a link within the html text displayed on the JTextPane.
How can I achieve this? If there is a need to implement an event listener? If so what is the appropriate event listener that is to be handled?
The listener type you are looking for is a HyperlinkListener, some code snippet:
final JTextPane pane = new JTextPane();
pane.setEditable(false);
pane.setContentType("text/html");
pane.setPage("http://swingx.java.net");
ToolTipManager.sharedInstance().registerComponent(pane);
HyperlinkListener l = new HyperlinkListener() {
#Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
try {
pane.setPage(e.getURL());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
};
pane.addHyperlinkListener(l);