How to set word wrap for JComboBox? - java

I want to display multiple-line text for each item in combobox. Words should go to the next line automatically if the exceed the width of combobox. How to achieve this?
String meanings;// array which contains pretty long sentences
JComboBox<String> meaningsComboBox=new JComboBox<String>(meanings);

Maybe wrapping each value into html will help you(this trick work with JLabels), example here.
In another way, I think, you need make custom renderrer, example here

Related

How do I wrap text in a jTextPane?

I'm currently using a jTextArea, and that has a setLineWrap() method to indicate whether text should be wrapped or not. Do jTextPanes have an equivalent to this? I know it can wrap words, but I need it to wrap characters.
For example, "oooooooooooo..." should be wrapped and displayed as two or more lines of o's instead of one long line with a scroll bar. So is this possible with jTextPanes?
JTextPanes word wrap automatically, you have to set the size.
jTextPane.setSize(someValue);
The size should be set to something small.
But this doesn't help you because it doesn't do that with characters... Try using <br> maybe? Or check this link: http://java-sl.com/wrap.html

Changing the font of a text in a JTextPane dynamically

I want to change the certain texts written on a JTextPane dynamically. I have a String array containing the words which should be changed
String ListMethod [] = {"forward", "backward", "left", "right"};
I've gone through some posts and many suggest to use JTextPane or JEditorPane to edit text but most of the answers given work on static text. I want to do it in a way such that as I type "forward" or "backward", etc... in the textpane, it detects this words and changes the color. How can I go about it?
Thanks for your help.
See here how to implement a DocumentListener Value Change Listener to JTextField. The have a look at javax.swing.text.Highlighter and javax.swing.text.HighlightPainter.
You have to capture the appropriate event and perform actions. For example in your case you can create an ActionListener that changes color and use registerKeyBoardAction to attach it on your JTextPane.
Oracle has a good tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/index.html on event listeners. I suggest you start getting yourself familiar there

Get input values from JComboBox

How can I get the input for an editable JComboBox. When user gives an input to the combo how I can get the input text from it?
You need to get the edited text from the combobox editor via combo.getEditor().getItem().
If you need the text that is selected on a JComboBox and you are sure it's a String and not any other object, just use something like String text = (String)myCombobox.getSelectedItem().
If the thing you have in your Model is other than a String, then you need to cast it to the appropriate class, and then use the toString() method of that object.
If you need more help, you should paste a bit of your code, at least declaration and inicialization of your JComboBox...
Just have a look at the oracle tutorial. They do explain how to handle the common swing components http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

Checkbox List in JDialog

I'm trying to use a JDialog box that has a search text field that as text is entered, it shortens a list to those that match. On this list I would like to display each line with a checkbox that can be selected. I might also want the ability for it to function like a select all list where Ctrl + Click on the line would select the item and check its box. How do I get this done?
Jlist is exactly what you're looking for. You can use a list of checkboxes and ctrl-click them.
I would use a JTable with two columns. The first column can be your check box and the second column could be the text.
Read the section from the Swing tutorial on "How to Use Tables" in particular the part on Sorting and Filtering for an example.

Selecting specified text in an HTML formatted JEditorPane

I am displaying text in a Java JEditorPane using HTML to fomrat the text. I am also designing a search function that finds text in the JEditorPane selects the text and then scrolls to it. My problem is creating an algorithim that will actually specify the beginning and ending position for the selection.
If I simply retrieve the text using myeditorpane.getText(), then find the search string in the result, the wrong selection start and end positions are calculated with the wrong text being selected (the tags are throwing the calculation off). I tried removing the html tags by executing a replace all function text.().replaceAll("\<.*?>","") before searching for the text (this replace all removes all text in between the tags) but still the wrong selection points are calculated (although I'm getting close :-)).
Does anyone have an easy way to do this?
Thanks,
Elliott
You probably want to be working with the underlying Document, rather than the raw text, as suggested in this HighlightExample.
You need to find the start location of the text. I guess something like:
int offset = editorPane().getDocument().getText().indexof(...);
Then to scroll you can use:
editorPane.scrollRectToVisible( editorPane.viewToModel(offset) );
Read up on Text and New Lines for more info.

Categories