Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
'framework rev. 114 up must be installed 'add JBGrid1, Text1 to a Form Option Explicit Public Sub Form_Load() 'new properties JBGrid1.AutoCheckBox = True JBGrid1.BackColorCellsAlternating = vbYellow 'AlternatingRowColor JBGrid1.GradientHeader = True JBGrid1.ShowSortIndicator = True JBGrid1.SortIndicatorIndex = 0 '-1 'old properties JBGrid1.ColHeaderIndex = 1 JBGrid1.RowHeightGlobal = 20 JBGrid1.FlatHeader = False JBGrid1.Editable = jbEditOnDblClick JBGrid1.BackColorHeader = RGB(178, 180, 191) ' JBGrid1.ForeColorHeader = vbBlue ' JBGrid1.GridColor = RGB(212, 208, 200) ' => SortIndicatorColor ' JBGrid1.CellButton.BackColor = vbWhite 'EditorBackColor JBGrid1.Cols = 7 JBGrid1.Rows = 20 GridTest() End Sub Public Sub Command1_Click() JBGrid1.ClearData() 'reinitializes DataMatrix and Header; all other values and settings are kept JBGrid1.Cols = 7 JBGrid1.Rows = 6 GridTest() End Sub Public Sub GridTest() ' On Error Resume Next Dim x As Integer, y As Integer For y = 0 To JBGrid1.Cols JBGrid1.Header(y) = "Header " & y ' JBGrid1.Header(y) = "<html><b>Header </b>" & y & "</html>" ' JBGrid1.Header(y) = "<html><b>Header</b><br>" & y & "</html>" ' JBGrid1.Header(y) = "<html><b><u>T</u>wo</b><br>lines</html>" '"Header" & y 'need to be added to resources JBGrid1.HeaderIcon(2) = resources.LoginPng JBGrid1.HeaderIcon(4) = resources.RssPng For x = 0 To JBGrid1.Rows Select Case y Case 0 JBGrid1.DataMatrix(x, y) = "Item_" & x & "/" & y Case 1 JBGrid1.DataMatrix(x, y) = "Item_" & x & "/" & y Case 2 JBGrid1.DataMatrix(x, y) = "True" If x = 4 Then JBGrid1.DataMatrix(x, y) = "" Case 3 Dim dblNumber As Double = Round(Rnd()* x - 5, 2) JBGrid1.DataMatrix(x, y) = dblNumber JBGrid1.CellForeColor(x, y) = IIF(dblNumber <= 0, vbRed, vbGreen) JBGrid1.DataMatrix(x, y).TextAlign = fmTextAlignRight Case 4 JBGrid1.DataMatrix(x, y) = "" JBGrid1.DataMatrix(x,y).Picture = resources.RssPng ' JBGrid1.DataMatrix(x,y).Icon = ArrowdownPng Case 5 JBGrid1.DataMatrix(x, y) = "" JBGrid1.CellBackColor(x, y) = RGB(Rnd()*(256),Rnd()*(256),Rnd()*(256)) ' Case 6 ' JBGrid1.DataMatrix(x, y) = Null End Select Next Next JBGrid1.DataMatrix(0, 0).BackColor = RGB(255, 220, 30) ' JBGrid1.DataMatrix(0, 0).CellBackColor = RGB(255, 220, 30) JBGrid1.CellForeColor(0,0) = vbBlue JBGrid1.CellBackColor(0,1) = vbRed JBGrid1.CellForeColor(0,1) = vbGreen JBGrid1.CellBackColor(2,0) = vbCyan JBGrid1.CellForeColor(2,1) = vbGreen JBGrid1.CellBackColor(2,2) = vbBlue JBGrid1.CellBackColor(4,1) = vbGreen JBGrid1.SetFocus() 'JBGrid1.Parent.setAutoCreateRowSorter(True) 'JBGrid1.Parent.getColumnModel().getColumn(0).setPreferredWidth(50) 'JBGrid1.Parent.setAutoResizeMode(0) End Sub Public Sub JBGrid1_BeforeCellEdit(row As Integer, col As Integer) Select Case col Case 0 'JBCellEditor button's dimension is too small so "..." shows; it is not the caption of the button! JBGrid1.ComboList = "..." 'invokes CellButton Case 1 JBGrid1.ComboList = "Test1|Test2|" & _ "Test3|Test4|Test5|Test6|Test7|" & _ "Test8|Test9" 'invokes ComboList, use '|' as item seperator Case 4 JBGrid1.StopEdit() 'prevents editing Case 5 JBGrid1.CellButton.Caption = "color" JBGrid1.ComboList = "..." 'invokes CellButton Case Else JBGrid1.ComboList = "" 'reset to DefaultEditor End Select End Sub Public Sub JBGrid1_CellButtonClick() Text1.AppendText "HitTest: CellButtonClick " & JBGrid1.Row & "/" & JBGrid1.Col & vbCrLf Me.Caption = "HitTest: CellButtonClick " & JBGrid1.Row & "/" & JBGrid1.Col End Sub Public Sub JBGrid1_AfterCellEdit(row As Integer, col As Integer) Text1.AppendText "HitTest: AfterCellEdit " & JBGrid1.Row & "/" & JBGrid1.Col & vbCrLf End Sub Public Sub JBGrid1_Click() Text1.AppendText "HitTest: Click " & JBGrid1.Row & "/" & JBGrid1.Col & vbCrLf Me.Caption = "HitTest: Click " & JBGrid1.Row & "/" & JBGrid1.Col End Sub Public Sub JBGrid1_DblClick() Text1.AppendText "HitTest: DblClick " & JBGrid1.Row & "/" & JBGrid1.Col & vbCrLf End Sub Public Sub JBGrid1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Text1.AppendText "HitTest: MouseUp " & JBGrid1.Row & "/" & JBGrid1.Col & vbCrLf End Sub Public Sub JBGrid1_ColHeaderClick() Text1.AppendText "HitTest: ColHeaderClick " & JBGrid1.Row & "/" & JBGrid1.Col & " " & JBGrid1.ColHeaderIndex & vbCrLf Me.Caption = "HitTest: ColHeaderClick " & JBGrid1.ColHeaderIndex End Sub |
Quoted
Framework rev. 114 - I hope theuserbl will take care of providing the .jar
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Public Function getTableCellRendererComponent(table As JTable, value As Object, isSelected As Boolean, hasFocus As Boolean, row As Integer, col As Integer) As Component [...] If isSelected = True Then If myGrid.DataMatrix(row, col).CellBackColor <> -1 Then 'Call getTableCellRendererComponent.setBackground(RGBtoColor(myGrid.DataMatrix(row, col).CellBackColor)) Dim ColSelR As Integer = getTableCellRendererComponent.getBackground.getRed Dim ColSelG As Integer = getTableCellRendererComponent.getBackground.getGreen Dim ColSelB As Integer = getTableCellRendererComponent.getBackground.getBlue Dim ColBackR As Integer = myGrid.DataMatrix(row, col).CellBackColor / 1 And 255 Dim ColBackG As Integer = myGrid.DataMatrix(row, col).CellBackColor / 256 And 255 Dim ColBackB As Integer = myGrid.DataMatrix(row, col).CellBackColor / 65536 And 255 ' Dim ColMixR As Long = (ColSelR + ColBackR) / 2 ' Dim ColMixG As Long = (ColSelG + ColBackG) / 2 ' Dim ColMixB As Long = (ColSelB + ColBackB) / 2 Dim ColMixR As Long = (ColSelR / 4 * 3 + ColBackR / 4) Dim ColMixG As Long = (ColSelG / 4 * 3 + ColBackG / 4) Dim ColMixB As Long = (ColSelB / 4 * 3 + ColBackB / 4) Call getTableCellRendererComponent.setBackground(RGBtoColor(RGB(ColMixR, ColMixG, ColMixB))) 'done by Parent.setSelectionBackground()in JBGrid 'Else ' Call getTableCellRendererComponent.setBackground(RGBtoColor(myGrid.BackColorSel)) End If [...] End Function |
Jabaco Source |
|
1 2 3 4 5 6 7 8 |
Dim cRed As Integer = myGrid.Parent.getTableHeader.getForeground().getRed() Dim cGreen As Integer = myGrid.Parent.getTableHeader.getForeground().getGreen() Dim cBlue As Integer = myGrid.Parent.getTableHeader.getForeground().getBlue() Dim cHSB(2) As Single cHSB = Color.RGBtoHSB(cRed,cGreen,cBlue,cHSB) Dim c As color c = Color.getHSBColor(cHSB(0), cHSB(1), 0.5*(1+cHSB(2))) g.setColor(c) |
Jabaco Source |
|
1 |
Call Base.setUI(New VBA#MultiLineLabelUI) |
I am dealing with financial programms
Ok, then I think about, to load it the next time up to svn.Quoted
I like the effect of your sample a lot. It 'feels' right.
Colors could shine through a bit brighter for my taste.
Quoted
I am just allways worried about stuffing too manny Calls into those Renderers.
My computer is 10 years old !!
Hmmm.. currently I haven't looked special at it, too. I think, at first I will try to use it in little Java-examples.Quoted
To be honest, I did not even look at MultiLineLabelUI
Jabaco Source |
|
1 2 3 4 5 |
[...] JLabel l = new JLabel( "This is the first line\nThis the second", icon, SwingConstants.LEFT ); [...] l = new JLabel( "This \nlabel \nhas \none \nword \non \neach \nline.", icon, SwingConstants.LEFT ); [...] |
This post has been edited 3 times, last edit by "theuserbl" (Oct 17th 2013, 12:45am)
Quoted
It would be a nice feature to get that to work for the header and the JBGrid cells.
Quoted
This approach has two drawbacks. First, it will bloat your code or resource strings since you'll need to add the HTML tags somewhere. Second, you'll loose control of how text is being drawn once HTML mode is set. When you use HTML in a Swing component, it will be rendered as a View (see javax.swing.plaf.basic.BasicHTML for implementation details).
This post has been edited 3 times, last edit by "theuserbl" (Oct 17th 2013, 12:51am)
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
JTable multiline cell renderer By BotungeOctober 09, 2009 18:57 I've searched the web a while in order to find a proper nicely written multiline cell renderer for a JTable, but all the solutions which I have found so far somehow does not meet my requirements, or they are just clumsy written. The first example I found was this http://www.java2s.com/Code/Java/Swing-Components/MultiLineCellExample.htm, but somehow it does not meet the requirements which i have. The renderer should be able to render individual row with different heights. And they should be able to resize etc. when manipulating the table. By combining the above example with my own code, i came to the solution below, which is nice and clean and does not rely on anything outside the renderer. /** * Multiline Table Cell Renderer. */ public class MultiLineTableCellRenderer extends JTextArea implements TableCellRenderer { private List<List<Integer>> rowColHeight = new ArrayList<List<Integer>>(); public MultiLineTableCellRenderer() { setLineWrap(true); setWrapStyleWord(true); setOpaque(true); } 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()); } setFont(table.getFont()); if (hasFocus) { setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); if (table.isCellEditable(row, column)) { setForeground(UIManager.getColor("Table.focusCellForeground")); setBackground(UIManager.getColor("Table.focusCellBackground")); } } else { setBorder(new EmptyBorder(1, 2, 1, 2)); } if (value != null) { setText(value.toString()); } else { setText(""); } adjustRowHeight(table, row, column); return this; } /** * Calculate the new preferred height for a given row, and sets the height on the table. */ private void adjustRowHeight(JTable table, int row, int column) { //The trick to get this to work properly is to set the width of the column to the //textarea. The reason for this is that getPreferredSize(), without a width tries //to place all the text in one line. By setting the size with the with of the column, //getPreferredSize() returnes the proper height which the row should have in //order to make room for the text. int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth(); setSize(new Dimension(cWidth, 1000)); int prefH = getPreferredSize().height; while (rowColHeight.size() <= row) { rowColHeight.add(new ArrayList<Integer>(column)); } List<Integer> colHeights = rowColHeight.get(row); while (colHeights.size() <= column) { colHeights.add(0); } colHeights.set(column, prefH); int maxH = prefH; for (Integer colHeight : colHeights) { if (colHeight > maxH) { maxH = colHeight; } } if (table.getRowHeight(row) != maxH) { table.setRowHeight(row, maxH); } } } The thing to not about the code above, it the setSize() call to the textarea. The height is not important, by the width is. getPreferredSize() is used to calculate the proper height of the cell, but getPreferredSize() only works if the width of the textarea has been set. If the width has not been set, getPreferredSize() thinks that it can lay out the text in the area in one line. By setting the width with setSize() we force getPreferredSize() to calculate a height for the text where the text cannot be wider then the width we've set using setSize(). As the renderer used an double arraylist to contain the preferred height of each cell, the same instance of the renderer must be used on all columns. The example belov just uses String in all column, but one might have domain specific object which are to be renderer. A trick to overcome this could be to make a domain specific renderer (MyDomainRenderer), which extends the MultiLineRenderer and encodes domain specific information into a String which is then passed to the MultiLineRenderer. public class MyDomainMultiLineCellRenderer extends MultiLineTableCellRenderer { public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { String toRender = ""; if (value instanceof SomeClass) { toRender = "SomeClass" + ((SomeClass)value).getSpecificMethod(); } else { //etc. } super.getTableCellRendererComponent(table, toRender, isSelected, hasFocus, row, column); } } To test the stuff above, you could write a test program like this: import java.awt.Component; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; public class TestJTableMultiline extends JFrame { public TestJTableMultiline() { super("Multi-Line Cell Example"); setDefaultCloseOperation(DISPOSE_ON_CLOSE); DefaultTableModel dm = new DefaultTableModel() { public Class<String> getColumnClass(int columnIndex) { return String.class; } public boolean isCellEditable(int row, int column) { return false; } }; dm.setDataVector( new Object[][]{ {"A0, Line1\nA0, Line2\nA0, Line3", "B0, Line1\nB0, Line2", "C0, Line1"}, {"A1, Line1", "B1, Line1\nB1, Line2", "C1, Line1"}, {"A2, Line1", "B2, Line1", "C2, Line1"} }, new Object[] {"A", "B", "C"}); JTable table = new JTable(dm); table.setDefaultRenderer(String.class, new MultiLineTableCellRenderer()); TableRowSorter<? extends TableModel> sort = new TableRowSorter<DefaultTableModel>(dm); table.setRowSorter(sort); JScrollPane scroll = new JScrollPane(table); getContentPane().add(scroll); setLocationByPlatform(true); setSize(400, 430); setVisible(true); } public static void main(String[] args) { TestJTableMultiline frame = new TestJTableMultiline(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } } And that kind of that... Now you have a proper multi line cell renderer, which can be reused over and over again since it has not been poluted with domain specific code. |
I found something very prmissing though. It could be inspiring:
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
dm.setDataVector( new Object[][]{ {"A0, Line1\nA0, Line2\nA0, Line3", "B0, Line1\nB0, Line2", "C0, Line1"}, {"A1, Line1", "B1, Line1\nB1, Line2", "C1, Line1"}, {"A2, Line1", "B2, Line1", "C2, Line1"} }, new Object[] {"A", "B", "C"}); |
Quoted
I just needed some functions to calculate standarddeviation, certain regressions, point of intercept and so on:
Quoted
It is the same like the MultiLineLabelUI in Jabaco.
Quoted
So, to begin a new line you have to write "\n".
Where is the difference to the HTML-solution? Where is it better to write "\n" instead of "<br>" ?
Quoted
I don't know, if this is part of the VisualBasic Finacial class.
Have a look at
Right.Still, if you try to use that for the tableheader with previous rev. of the framework?
It seems like it ignores ' \n '; html works fine.
Have uploaded it. And now the Jabaco Framework Binaries having its own place in the Jabaco-Wiki:Quoted
So, are you going to upload the ttransparent rowselector?
Currently it have not. Do you think, that it will be an good idea to have one?Quoted
Should there be a variable for transparency?
Transparency is often a problem, because it needs to know both colors: The color which comes over something and the color of the part, where it comes over. It isn't not that easy to find everytime both.Quoted
If I remember right, I don't have Jabaco available right now, there are a couple places in the framework where you can allready set a value for transparency. But none seem to work! Right?
This is written for that part. The solution to mix the foreground and background is old. If you have a picture and write there a transparent text over it, then pixel for pixel is mixed the color of the font with the color of the background.Quoted
Maybe some other parts of the framework can benefit from your solution. You might want to put together a function for Global.
Just a thought, I will have a look later.
Ok. I wait for the picture.Quoted
As I mentioned before, there is another BUG with the JBGrid on MacOS when you have the ComboBox as CellEditor the ComboBox does not fill the whole Cell. It centers to the middle of the Cell. When the CellHeight / CellWidth gets bigger the ComboBox does not size to those values. So the Cell is much bigger than the Component. The CellButton stretches to the Cell! This issue might be LAF dependent, I don't know. But it looks very ugly.
I will post a picture later.
Quoted
Have uploaded it. And now the Jabaco Framework Binaries having its own place in the Jabaco-Wiki
Quoted
Quoted
Should there be a variable for transparency?
Currently it have not. Do you think, that it will be an good idea to have one?
Also I am currently not sure, how to use it then. The property would be in JBGrid. The changes are in JBCellRenderer.
Quoted
Ok. I wait for the picture.
Jabaco Source |
|
1 |
ComboBox1.Parent.setUI(New javax#swing#plaf#metal#MetalComboBoxUI())
|
But I never got a multiline text there except for when I use html code <br> !!
It would be a nice feature to get that to work for the header and the JBGrid cells.