You are not logged in.

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.

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

1

Saturday, October 12th 2013, 4:30pm

JBGrid - Update

Framework rev. 114 - I hope theuserbl will take care of providing the .jar

Hey there,

I have added a couple of features to the JBGrid:



JBGrid1.ClearData()
reinitializes the grids' DataMatrix and Header.
All other values and settings are kept. This makes it easier and faster to populate an existing JBGrid when your data updates or the underlying datasource changes a lot.
You don't have to overwrite all existing fields of the DataMatrix anymore.

JBGrid1.AutoCheckBox = True
True/False string values are being recognized, displayed and edited with a CheckBox.

JBGrid1.BackColorCellsAlternating = vbYellow
For compatibility, since we allready have .BackColorCells, I named it .BackColorCellsAlternating. (I would rather like to change them both to .BackColorRows1 and .BackColorRows2 ...!)
Every Cells' BackColor of every other row is being set accordingly (zebra)
When you set them both the same color all cells are the same, obviously!
It is worth putting this in the framework, since every cell is being painted here anyway.
If you need single cells to have a different BackColor use e.g.
JBGrid1.CellBackColor(0,1) = vbRed

The .CellForeColor ( and JBGrid1.CellBackColor(row, col) )is now also visible when the cell or row is selected.

JBGrid1.GradientHeader = True
Fills the JBGrids' header with a gradient paint blending from .BackColorHeader to color.white
(color.white is hardcoded here, let me know if that is a problem!!)
If JBGrid1.FlatHeader = True is set at the same time no gradient paint is used; you will get a minimalistic header!
JBGrid1.GradientHeader = False
The standard JBGrid.Header is being used.
I have changed the internals a bit. The renderer is not using a new Jlabel anymore but rather modifies the existing Jtables' header by painting on it.

JBGrid1.ShowSortIndicator = True
Since the JBGrid uses its own DataModel (HashMap) Javas' Jtable.setAutoCreateRowSorter(True) does not really work. The rowsorter usually provides a sortarrow in the columns' header that we don't have.
I implemented a custom drawn SortIndicator to the JBHeaderRenderer. It does not look as smooth as the original thing (Java uses an icon) but I wanted something fast. Suggestions are very wellcome!
This does not sort anything. It is a visual indicator! All sorting has to be done while populating the JBGrid.

JBGrid1.SortIndicatorIndex = 0 '0 = down, -1 = none, 1 = up
Is being set by clicking on the JBGrid.ColumnHeader or in code regardless JBGrid1.ShowSortIndicator = True or False!
When the user clicks on the same Column a second time the SortIndicatorIndex reverses/togles. It changes to -1 when an emty part of the header is being clicked (JBGRid1.ColHeaderIndex = -1).

Resizing a column no longer triggers a click event!

I also fixed some other bugs where click events where not fired accordingly.

Have a look at the following samplecode for demonstration.

I did some testing on Win7 (different LAFs) and MacOS Snowleopard -> looks good to me.
There are allready so many features to the JBGrid, it is hard to test them all.
I found some bugs using different LAF's. ComboBox throws errors, does not look right. Same with CellButton. Those bugs have been there before I touched the code!! I did not yet look into it though. Seems like some LAFs mess around with opaqueness, borders, insets ...

Using this new version you will HAVE TO CHANAGE EXISTING PROJECTS.
At least the .BackColorCellsAlternating value has to be set.

Jabaco does not know what standard values to use for new properties of a controll. So using those new features you will find the initial settings a bit anoying. e.g. .BackColorCellsAlternating will be black!!
You can change that by editing the Jabaco.xml file of you Jabaco installation path.

BACKITUP though!!

Example:
to the section
<Object Type="JBGrid">
add the following new line
<Param Name="BackColorCellsAlternating" Value="-2147483643"/>


Well, have fun playing arround with it and let me know if something is not working for you...


Dani


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


PS: Oh, and you will need to add those icons (just use your own in the code...) to you resources' folder

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

2

Sunday, October 13th 2013, 4:00am

Quoted

Framework rev. 114 - I hope theuserbl will take care of providing the .jar

Done:
[ Jabaco-rev115.jar ]

Greatings
theuserbl

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

3

Wednesday, October 16th 2013, 12:00am

Hi Dani !

In your example, the colored cells changes not the color, if they are selected.

So I tried something out.

In JBCellRenderer:

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


Please try it out.

With this change, the selected cells have the selection-color with a little bit of the color of the background of the cell. That gives the feeling, the selection is a little bit transparent.

Greatings
theuserbl

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

4

Wednesday, October 16th 2013, 7:57am

Hey there theuserbl,

I am dealing with financial programms so I need back- and forecolors on selected rows to represent certain values.

Funny I have been playing around with that too!
Also I wanted to get the sortindicator a lighter shade of the gridcolor.
Just a different approach in the paint method:

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)


I used the HSB colorspace because of the RGB problems shading zero value bases colors!!
The above only shows a step of getting there.

I like the effect of your sample a lot. It 'feels' right.
Colors could shine through a bit brighter for my taste.

I am just allways worried about stuffing too manny Calls into those Renderers.
My computer is 10 years old !!


Dani

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

5

Wednesday, October 16th 2013, 6:45pm

@ theuserbl

There is something else I am curious about...

In the JBHeaderRenderer we have a Call

Jabaco Source

1
Call Base.setUI(New VBA#MultiLineLabelUI)


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.
(Automatischezeilenhöhenanpassungundumbrechungdestextes)
To be honest, I did not even look at MultiLineLabelUI 8o

Standard JLabel has no linebreak ability!

Since it is in the VBA tree of the framework I thought you might want to look into it.

Oh, and look at my post above ;)


Dani

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

6

Wednesday, October 16th 2013, 9:48pm

I am dealing with financial programms

And do you need a complete VBA.Financial class?
You know, there is a implementation for MonoBasic, which could be ported to Java:
[ http://www.jabaco.org/board/p2719-can-be…-framework.html ]


Quoted

I like the effect of your sample a lot. It 'feels' right.
Colors could shine through a bit brighter for my taste.
Ok, then I think about, to load it the next time up to svn.

Quoted

I am just allways worried about stuffing too manny Calls into those Renderers.
My computer is 10 years old !!

I also think, that it is better, to have a small framework. But most improvements are extending it, to make it bigger.

Quoted

To be honest, I did not even look at MultiLineLabelUI
Hmmm.. currently I haven't looked special at it, too. I think, at first I will try to use it in little Java-examples.
But I have looked at the history of it.
There is only one change by OlimilO, which formattes the documentation text a little bit.

So, this here is the first implementation from Manual from Revision 6:
[ http://code.google.com/p/jabacoframework…abelUI.java?r=6 ]

I have at first seen, that other have the same code
[ http://code.google.com/r/alexfdz-screenc…4a3827daf825747 ]
[ http://code.google.com/r/alexfdz-screenc…a63f1987876a823 ]
[ http://grepcode.com/file/repository.grep…ineLabelUI.java ]

But on the original JetBrains-Git
[ http://git.jetbrains.org/?p=idea/communi…acc0cf5;hb=HEAD ]
[ http://git.jetbrains.org/?p=idea/communi…cea503ea0358275 ]
stands the answer:
" Based on Zafir Anjum example."

And Google says, that Zafir Anjum is one of the founder of [ CodeGuru ].
He have also written some posts with examples in the Java-Area of CodeGuru.com.


Oh, over this
[ http://code.google.com/p/dda2008/source/…ineLabelUI.java ]
I finally have find the post:
[ http://web.archive.org/web/2007051910003…icles/198.shtml ]
It comes with an example, how to use it. :)


Ahhhh....
As you can see, I have now a lot of edited this post, because I find everytime a little bit more out.

And what is the result?
Here some part of the example code:

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 );
[...]



With other words:
With this MultiLineLabelUI, you can exactly the same like with HTML in the label. The only difference is, with HTML you use "<br>" and here "\n".
:(



Greatings
theuserbl

This post has been edited 3 times, last edit by "theuserbl" (Oct 17th 2013, 12:45am)


theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

7

Wednesday, October 16th 2013, 10:21pm

I see, that Java itself have a class called MultiLabelUI, but which is different implemented.

It is:
javax.swing.plaf.multi.MultiLabelUI

The UI-classes in Java are for creating the own LookAndFeel.
In this package are a lot of "Multi"-things.

But the implementation of Jabaco seems really to implement a UI for multiple lines in a JLabel.
Especialy the non-static function
public String[] splitStringByLines( String str )


Greatings
theuserbl

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

8

Wednesday, October 16th 2013, 10:44pm

Quoted

It would be a nice feature to get that to work for the header and the JBGrid cells.


I have find this side
[ http://samuelsjoberg.com/archive/2009/10…labels-in-swing ]
with an other BSD-licensed implementation of MultiLineLabelUI and with sample to it an so on.

There it works fine...


Edit:
Here what stands on the side:

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).


So, you can only choose between a Label, JBGrid, etc.
- which have an integrated Line-Break. Then after every line the text go after the end of the line to the next line
- OR you have support for HTML
Both together seems not to be possible.


Greatings
theuserbl

This post has been edited 3 times, last edit by "theuserbl" (Oct 17th 2013, 12:51am)


Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

9

Thursday, October 17th 2013, 7:59am

Hey there theuserbl,

nice investigation, thanks!

I tried invoking MultiLines with some elder versions of the framework but it did not work.
I wonder if it did for Manuel!?

I will look at the source again later. Maybe I am missing the correct 'keywords' to invoke linebreaks.

You got a lot of information on this one but I think it is all much to involved.

I found something very prmissing though. It could be inspiring:

http://blog.botunge.dk/post/2009/10/09/J…l-renderer.aspx

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've got to head for work now ...


See ya,


Dani


By the way ...

most of the math stuff is done by my database H2

I just needed some functions to calculate standarddeviation, certain regressions, point of intercept and so on:


theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

10

Thursday, October 17th 2013, 11:15am

I found something very prmissing though. It could be inspiring:

It is the same like the MultiLineLabelUI in Jabaco.

In your mentioned program stand

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"});


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 just needed some functions to calculate standarddeviation, certain regressions, point of intercept and so on:

I don't know, if this is part of the VisualBasic Finacial class.
Have a look at
[ https://github.com/mono/mono-basic/blob/…ic/Financial.vb ]
if there is something usefull in it.

Greatings
theuserbl

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

11

Thursday, October 17th 2013, 11:52am

Quoted

It is the same like the MultiLineLabelUI in Jabaco.


You are right, as I said, I did not look at the VBA framework code yet ( well, just now I did!)

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>" ?


I agree...
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. Thats why I outcommented the Call... in the source code.

Quoted

I don't know, if this is part of the VisualBasic Finacial class.
Have a look at


I think you got me wrong, the chart is part of my programm so I got all the functions I needed. I just wanted to demonstrate what I was looking for. I put together a little Module so I can easily plot to different controls.


So, are you going to upload the ttransparent rowselector? Should there be a variable for transparency?
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?
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.


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.


Dani

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

12

Friday, October 18th 2013, 3:21am

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.
Right.

Quoted

So, are you going to upload the ttransparent rowselector?
Have uploaded it. And now the Jabaco Framework Binaries having its own place in the Jabaco-Wiki:
[ http://www.jabaco.org/wiki/Latest_JabacoFramework_Binary ]
Don't know, if I will link it in the front page of the wiki in the future. But currently the side is there. :-)
And if anybody other want to compile and upload the latest binary, he can also put it than in there.

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

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?
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

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.
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

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.
Ok. I wait for the picture.
And it is not because of the colors?
On Linux existing a lot of problems with the "system-colors". I am often thinking, if it would be a good idea to integrate a non-color.
And with "non-color" I mean, that no color is set. So for every control is the default color of the LookAndFeel be used.
Because only on Windows with the WindowsLookAndFeel is the default color and the colors used in Jabaco, the same.

One of the reasons, why I have decided against it was, that it needed later a modified Jabaco.xml. But this is now already modified. So there isn't much, which speaks against it, to implement the non-color.

Oh, and btw: I have added Jabaco.xml to svn.

Greatings
theuserbl

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

13

Friday, October 18th 2013, 7:57am

Hey there theuserbl,

Quoted

Have uploaded it. And now the Jabaco Framework Binaries having its own place in the Jabaco-Wiki


I LIKE IT... :thumbsup:
something we have been waiting for for a long time!

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.


It is probably allright the way it is now. We have to be carefull with too many properties here. But it would be no problem to read the mygrid.RowColorSelTransparency Property from within the Renderer!

Quoted

Ok. I wait for the picture.






I have the suspicion that the ComboBox on the Mac in general does not except higher values for its height, I am not sure though I will test this...


Dani

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

14

Friday, October 18th 2013, 10:02am

OK, I looked into it

Apple is very restrictive with style as we know.
So the ComboBox simply can not be set to the required height of the JBGrid Cell!

There is one way arround that, we will have to change the LAF for the ComboBox used by the JBGrid

Jabaco Source

1
ComboBox1.Parent.setUI(New javax#swing#plaf#metal#MetalComboBoxUI())


It works, I have tested it; but the ComboBox button is a little too big on the Mac.
And of course dependig on the LAF used on a System this might not suit everybody.
It is probably not a solution.

Dani

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

15

Sunday, October 20th 2013, 3:50pm

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.

I have now seen, that it already work, when you use the HTML-tags.
Then <br> breaks the line always. If the word ends on the line or not.
But without <br> it breaks, after the line ends, and continues in the next line. That what you want.

I hae seen it, on the attached program, where I have used JBGrid.

Greatings
theuserbl
theuserbl has attached the following file:
  • NamCom.zip (13.47 kB - 190 times downloaded - latest: Today, 12:59am)

Posts: 4

Date of registration: Sep 29th 2015

  • Send private message

16

Wednesday, September 30th 2015, 5:38pm

JBgrid cell font doesn't change

Dani,

I got last update for JBGrid. When I try to change font (i.e. JBGrid1.fontname = "Tahoma" ), header texts font will change, not text font in each single cell. Do you know a workaround?

Maurizio

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

17

Wednesday, September 30th 2015, 7:56pm

Hey there,

Quoted

JBGrid1.FontName = "Tahoma"


works fine for me...



Are you using the latest Jabaco.jar?
I tested with 152! Have a look at the footer...


Dani

Rate this thread
WoltLab Burning Board