Expand JList row height depending on content?

There are maybe easiest and nicest way, I think that JTable with one TableColumn (and without TableHeader)in all cases better as JList here is your Render MacOX version.

There are maybe easiest and nicest way, I think that JTable with one TableColumn (and without TableHeader)in all cases better as JList, here is your Render MacOX version then output should be import java.awt. *; import javax.swing. *; import javax.swing.table.

*; import javax.swing.text. *; //tips4java.wordpress.com/2008/10/26/text-... public class AutoWrapTest { public JComponent makeUI() { String columnNames = {" Text Area Cell Renderer "}; Object data = { {"123456789012345678901234567890"}, {"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddx"}, {"----------------------------------------------0"}, {">>>>>>>>>>>>>dddddddddddddddddddddddddddddddddddddddddddddddddd" + "dddddddxdddddddddddddddddddddddddddddddddddddddddddddd" + "dddddddddddx>>>>>>>>>>>>>>>>>>>>>>>>>|"}, {">>>>>>>>>>>>ddddddddddddddddddddddddddddddddddddddddddddddddddd" + "ddddddx>>>>>>>>>>>>>>>>>>>>>>>>>>|"}, {"a|"}, {">>>>>>>>bbbb>>>>>>>>>>>>>>>>>>>|"}, {">>>>>>>>>>>>>>>>>>|"}, {">>>>>>>>>>>>>dddddddddddddddddddddddddddddddddddddddddddddddddd" + "dddddddxdddddddddddddd123456789012345678901234567890dddddd" + "dddddddddddddddddddddddddddddddddddddx>>>>>>>>>>>>>>>>>>>>" + ">>>>>|"}, {">>>>>>>>>>>>>dddddddddddddd123456789012345678901234567890dddddd" + "dddddddddddddddddddddddddddddddddddddxdddddddddddddd123456" + "789012345678901234567890dddddddddddddddddddddddddddddddddd" + "ddddd123456789012345678901234567890ddddx>>>>>>>>>>>>>>>>>>" + ">>>>>>>|"},}; TableModel model = new DefaultTableModel(data, columnNames) { private static final long serialVersionUID = 1L; @Override public boolean isCellEditable(int row, int column) { return false; } }; JTable table = new JTable(model) { private static final long serialVersionUID = 1L; @Override public void doLayout() { TableColumn col = getColumnModel(). GetColumn(0); for (int row = 0; row Height; if (getRowHeight(row)!

= h) { setRowHeight(row, h); } } } super.doLayout(); } private int getPreferredHeight(JTextComponent c) { Insets insets = c.getInsets(); View view = c.getUI(). GetRootView(c). GetView(0); int preferredHeight = (int) view.

GetPreferredSpan(View. Y_AXIS); return preferredHeight + insets. Top + insets.

Bottom; } }; table. SetEnabled(false); table. SetShowGrid(false); table.

SetTableHeader(null); table.getColumnModel(). GetColumn(0). SetCellRenderer(new TextAreaCellRenderer()); //table.

SetPreferredScrollableViewportSize(table. GetPreferredSize()); JScrollPane sp = new JScrollPane(table); sp. SetVerticalScrollBarPolicy(ScrollPaneConstants.

VERTICAL_SCROLLBAR_ALWAYS); sp. SetPreferredSize(new Dimension(250, 533)); JPanel p = new JPanel(new BorderLayout()); p. Add(sp); return p; } public static void main(String args) { EventQueue.

InvokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } public static void createAndShowGUI() { JFrame f = new JFrame(); f. SetDefaultCloseOperation(WindowConstants. EXIT_ON_CLOSE); f.getContentPane().

Add(new AutoWrapTest().makeUI()); f. SetLocation(100, 100); f.pack(); f. SetVisible(true); } } class TextAreaCellRenderer extends JTextArea implements TableCellRenderer { private static final long serialVersionUID = 1L; private final Color evenColor = new Color(230, 240, 255); public TextAreaCellRenderer() { super(); setLineWrap(true); setBorder(BorderFactory.

CreateEmptyBorder(2, 2, 2, 2)); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table. GetSelectionForeground()); setBackground(table. GetSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); setBackground((row % 2 == 0)?

EvenColor : getBackground()); } setFont(table.getFont()); setText((value == null)? "" : value.toString()); return this; } }.

Great. But this seems to only work for JTextArea (what I was asking for). Now what if I want to put the TextArea in a Panel and use the Panel as the cells component.

How can I achieve, that the panel containing the Textarea still expands the height of the cell? Is there any chance to achieve a expandable cell height in general, no matter, what's the cells content. – haferblues Aug 8 at 11:05 @haferblues that about PreferredSize, doesn't matter if is there JTexArea of JPanel, be sure that JPanel would be better JComponents for that as plain JTextArea, for example stackoverflow.Com/questions/6355544/… – mKorbel Aug 8 at 11:12.

Not sure if there is an elegant way. One way that I know works is as follows : In getListCellRendererComponent() Use JLabel for the renderer Convert the text in question to HTML and use some logic to insert in to the text where desired. Then set the text as text for the JLabel component and return.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions