select text from Jlist component - java

I have a Jlist that component is JTextpane that contains String such as "hello world"
I want to select word "hello" by selecting Jlist component
How can I know which word is selected, "hello" or "world"
I use method Jtextpane.getSelection() but it doesn't work
Any help will be appreciated!

I guess you mean that you are creating JTextPanes as the components in your list cell renderer. The problem here is that the components that are returned by the cell renderer are used only for painting the cells content, no actual functionality of the component is available.
A solution would be to not use a JList but instead using a big JTextPane with HTML content.

Related

How to render a Component in JTable using TableModel?

I have successfully displayed a JTable using an AbstractTableModel, but I want to add delete button for each row in the last column, in the getValueAt method that returns Object there is no way that I can return JButton, JLabel or any JComponent that is clickable. I tried that and I only get the object description toString.
Is there another solution to render JComponent in a JTable without using the TableModel approach?
Is there another solution to render JComponent in JTable without using the TableModel approach?
The TableModel is used to hold the data for the data for the model.
The JTable implements the view of the data for each column. The renderer is just a picture of the data. You can easily render the data to look like a button, however a renderer does not respond to any event.
The JTable does support editors and that is how you interact with real components. When you edit a normal cell a JTextField is placed in the cell location so you can type data into the cell and then the data is save to the model.
So if you want to click on a button then you need to use a button as an editor.
Check out Table Column Button for a class that uses a JButton as the renderer and editor. You then provide the class an Action to be invoked when the button is clicked.
Read the section from the Swing tutorial on Concepts: Renderers and Editor for more information. There is also a section on Using Other Editors.
One way : TableColumn.setCellEditor(jbutton_instance) on a hand added column.

After searching a hashmap, how do I have it highlight result in jlist?

I have created a program that allows you to enter a country/capital in 2 separate jtextfields on click from a jbutton, this adds the capital ( which is the value in the hashmap ) ( it also sets country as the key in the hashmap, but does not display it in the jlist )
After the jList is populated, I have a separate jtextfield/ jbutton that you enter the country to search for and it is supposed to highlight the capital in the Jlist
How do I get it to highlight the capital in the jlist after search ( or after the search jbutton is pressed ? )
Something like this?
searchText.setText(search);
String search = (String) jList1.getSelectedValue();
Taken from Java API on JLists:
Painting of cells in a JList is handled by a delegate called a cell renderer, installed on the list as the cellRenderer property. The renderer provides a java.awt.Component that is used like a "rubber stamp" to paint the cells. Each time a cell needs to be painted, the list's ListUI asks the cell renderer for the component, moves it into place, and has it paint the contents of the cell by way of its paint method. A default cell renderer, which uses a JLabel component to render, is installed by the lists's ListUI.
And here's the link to read more: http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html

JList with 3 JLabel inside

I'm looking for a possibility to show a List with comments in Java.
So I thought I could use the JList and make an own CellRenderer.
The problem is that I want to show the name, the date and the comment in one Item of the list.
How could this be realized with a JList and a CellRenderer? Or do I have to use something else instead of the JList?
It sounds as if you want a JTable rather than a JList. That will give you different columns where you can put your name, date and comment.
I'm sure you could also solve this using a CellRenderer that is a JPanel on which you put whatever you want, but I'd advise you to try out JTable first.
If you use a JLabel as the ListCellRenderer component (like DefaultListCellRenderer) you could prefix the text with "<html>" and format the "layout" of the list cells with HTML.

Display multiple lines within a Jlist cell

How can I display multiple lines within a single cell of a JList. In jtable, it is achieved by adding a JTextarea to the table cell renderer. Similiarly, Is there any code to add a custom cell renderer for a JList that holds a textarea? If yes, can you please give me a code snippet for the same?
Or do you think there is any other better method to display multiple lines within the cell of Jlist instead of using a textarea? please help me!!
Beside usage of HTML code in Swing component (like Jigar says), you can also specify a ListCellRenderer that uses a TextArea, exactly like you do in your JTable.
Try with html.
"line one <br/> linetwo"

JComboBox sizing issue in a table with Multi line Cells

I recently had a problem where I needed to have a field that can wrap the text and increase the height of the row as the text is wrapped, similar to Microsoft Excel. I managed to get it working correctly, the only problem is that the table contains multiple JComboBoxes. When the row's height increases from the field that wraps the text, the size of the JComboBox window and ArrowButton also increase. I am using a DefaultCellEditor for the JComboBox fields, and created my own Editor/Renderer to be used with the JTextArea field. Once the JComboBox's value is selected, the value gets displayed correctly in the field, the only problem is while I am selecting the value, the JComboBox window and ArrowButton could be HUGE depending on the size of the row. Is there any way to increase the height of the row, but have the JComboBox field height remain the same instead of growing to fill the column that it's in? I am thinking I might need to make a Custom Cell Editor for the JComboBox fields as well instead of using the default. Thanks in advance!
I am thinking I might need to make a
Custom Cell Editor for the JComboBox
fields as well instead of using the
default
That would probably be the solution since the size of the editor is determined by the size of the cell.
I would try using a JPanel with a BorderLayout as the editor component. Then you add your editor to the North of the panel.
It would be the easiest editor to create since all the mouse events and key events are passed to the editor I believe, which means the panel will get the events, not the combo box. So I guess you would need to forward these events to the combo box.
First, is the JComboBox in a BorderLayout and set to BorderLayout.CENTER?
If so, I'd change it to a different layout such as AbsoluteLayout so it does not stretch to fill the cell.
Also, I will also refer you to this post Putting JComboBox into JTable

Categories