Using the JTextArea and TableCellRenderer with JTable

I am looking to use a custom cellrenderer with the JTable. I am doing it because I want the ability to have multiline text in a table. Problem is, I am having trouble getting started.

This is what I have so far:

[code]from javax.swing import JTable
from javax.swing import JTextArea

import javax.swing.table.TableCellRenderer
from javax.swing import table
import system

class MultiLine(JTextArea):
def init(self):
pass

def MultiLineTableCellRenderer():
    setOpaque(True)
    pass

[/code]

There are several examples of doing this in JAVA on the web, but I can’t find any Python or Jython and cannot seem to figure out how to extend and implement the java functions in a Jython class.

Here is an example of one of the JAVA web examples.

http://coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2005-05/msg00264.html

If you only need multi-line text you could use html in the text (using “Text Value
Second Line value”, for example), but you must set the row height in some upper value for all the rows (even the one-line rows).

For IA people’s: maybe a new extension function like “getRowHeightAt” could do this in a fast way. What you think?

I have a function to iterate throught the rows and set the height. I am looking for a method to retrieve how many rows are being displayed and then set the height accodingly. In essence the row height would resize itself. This is because one of the columns is a comments feild and the user could have a whole lot of text. At runtime the columns could be adjusted so you can’t just simply insert
statements. Also, it would be nice if the field responded properly to carriage returns

The example below uses html and
statements to create multiline text and then we count the
statements to get the line count, but this doesn’t work in the scenario above.

[code]data = event.source.parent.getComponent(‘Table 1’).data
data2 = system.dataset.toPyDataSet(data)

i=0
for row in data2:
str2 = str(row[‘Setup’])
c = str.count(str2,’
’)

c = c+1
h=c*18
t = event.source.parent.getComponent('Table 1').getTable()
t.setRowHeight(i,h)
#system.gui.messageBox(str(row))
i=i+1[/code]

I don’t know if I understand what you want, but I think that this code could help you:

table = event.source.parent.getComponent('Table 1')
i = 1
h = table.getViewport().getVisibleRect().getHeight()
for row in table.getRowsInViewOrder():
	h = h - table.getTable().getRowHeight(row)
	if h <= 0:
		break
	i = i + 1
print "Rows displayed: %s " % (i)
rows = table.getTable().getRowCount()
print "Total rows: %s" % (rows) 

I am right?

Regards,

You misunderstand.

I want to display multiline text within a single cell without having to develop some fancy function to insert
statements.

The Jtable uses a JLabel CellRenderer by default. I am trying to figure out how to use a JTextArea CellRenderer for a specific column within Ignition. This should give me a column of cells that responds like a JTextArea Component.

I’m getting closer!!!

I have the TableCellRenderer working an can now render a column as a JtextArea with multiline within the table. This allows me to use word wrap within a table cell.

Now all I need to do is figure out how to get the size (or line count) of the cell. I have tried both .lineCount() and .getPreferredSize.

.lineCount() does not count wrapped lines and .getPreferredSize doesn’t seem to be working either.

[code]from javax.swing import JTable
from javax.swing import JTextArea
from javax.swing.table import TableCellRenderer
from java.awt import Dimension

class MyCellRenderer(JTextArea, TableCellRenderer):
def init(self):
pass

def TextAreaCellRenderer ():
	pass

def getTableCellRendererComponent(self ,table, value, selected, cellHasFocus,row,col):

	if selected==1:
		self.background = table.getSelectionBackground()
	else:
		self.background = table.getBackground()
	
	
	self.text = str(value) 
	#self.opaque = 1
	self.setLineWrap(1)
	self.setWrapStyleWord(1)
	h = table.getRowHeight(row)
	x=self.getLineCount() * 18
	
	self.text = str(value) + ' ' + str(self.getLineCount())
	
	if x > h:
		table.setRowHeight(row,x)
	return self

t = event.source.parent.getComponent(‘Table 1’).getTable()
t.getColumnModel().getColumn(1).setCellRenderer(MyCellRenderer())
t.getColumnModel().getColumn(2).setCellRenderer(MyCellRenderer())

[/code]

[quote=“djones”]You misunderstand. [/quote] Sorry for that.

I’m stay in “I am looking for a method to retrieve how many rows are being displayed” and for that sentence was the code that I post. I don’t pay sufficient attention to this: “At runtime the columns could be adjusted so you can’t just simply insert
statements.”

For the rest, I am not enough familiar with this Java swing component and I see that you alone are doing well, so I supose that I not helping here.

Good luck in the final stretch!

This is where i got so far…

The only thing that I think is a little hoaky is the scale factor that I added to the font width. This code finds the width of the column and the width of each “character” of the font. This gies you how many characters fit on a line. The test is split using plitlines function. Each of these is divided by the number of characters per line to give how many wrapped lines of text between newline characters. Using the math.ceil function we ensure that we have always rounded up instead of truncating the line count.

The cell is rerendered each time the data is updated or the column widths are manually resized. This resizes the row height automatically.

Like I said though, because these are true type fonts the width varies based on whether it is an ‘m’ or an ‘l’, so the .columnwidth gives the width for a lowercase ‘M’ as the widest. I applied a scale factor to put it closer to the average width for all the characters. A little hoaky, but it seems to work fine.

[code]from javax.swing import JTable
from javax.swing import JTextArea
from javax.swing.table import TableCellRenderer
from java.awt import Dimension
from javax.swing.text import Element
from javax.swing.text import Utilities
import system
import math

class MyCellRenderer(JTextArea, TableCellRenderer):
def init(self):
pass

def TextAreaCellRenderer ():
	pass

def getTableCellRendererComponent(self ,table, value, selected, cellHasFocus,row,col):
	from javax.swing.text import Utilities
	import system
	import math
	if selected==1:
		self.background = table.getSelectionBackground()
	else:
		self.background = table.getBackground()
	
	
	self.text = str(value) 
	#self.opaque = 1
	self.setLineWrap(1)
	self.setWrapStyleWord(1)
	h = table.getRowHeight(row)
	
	
	
	
	TC = self.getDocument().getLength()
	LC = 0
	
	Wide = table.getColumnModel().getColumn(col).getWidth()
	sc=int(Wide/self.getColumnWidth() * 1.5)
	s = str(self.text)
	
	SPL = str.splitlines(s)
	for substring in SPL:
		y = int(math.ceil(float(len(substring)) / sc))
		LC=LC+y
		
		
	
	
	self.text = str(value)
	x=LC * 18
	if x > h:
		table.setRowHeight(row,x)
	return self

t = event.source.parent.getComponent(‘Table 1’).getTable()
t.getColumnModel().getColumn(1).setCellRenderer(MyCellRenderer())
t.getColumnModel().getColumn(2).setCellRenderer(MyCellRenderer())

[/code]

This is some impressive work. I would like to say: you’d be a good candidate for working with the Module SDK - you might find it easier to write custom stuff like this in Java directly.

That said, you can find the actual string width like so:

(“self” here is your instance, which is a JTextArea and thus also a JComponent)

fm = self.getFontMetrics(self.font) width = fm.stringWidth(myString)

Carl,

I have several projects started in Eclipse that I developed for integration with Ignition. They are compliled and run a java applications at the moment. One of which is pretty cool. It uses OpenCV and the integrated webcam as a barcode scanner to read 1D and 2D barcodes. The idea is that you would be able to scan a barcode from within Ignition using this module as a method of data entry.

The place that I stumble is getting from a java application to a compiled Ignition module. I have an open post in the SDK section of the forum detailing the issue, but it hasn’t solicited any response.

Me and a couple guys from work are going to the user conference and I was hoping that there would be a section devoted to module development and I could get over the hump. The documentation (at least last time I looked) for the module development as a little thin.

-Doug

[quote=“djones”]Carl,

I have several projects started in Eclipse that I developed for integration with Ignition. They are compliled and run a java applications at the moment. One of which is pretty cool. It uses OpenCV and the integrated webcam as a barcode scanner to read 1D and 2D barcodes. The idea is that you would be able to scan a barcode from within Ignition using this module as a method of data entry.

The place that I stumble is getting from a java application to a compiled Ignition module. I have an open post in the SDK section of the forum detailing the issue, but it hasn’t solicited any response.

Me and a couple guys from work are going to the user conference and I was hoping that there would be a section devoted to module development and I could get over the hump. The documentation (at least last time I looked) for the module development as a little thin.

-Doug[/quote]

I’ve gone back to answer your initial question in the SDK forum. Sorry we let that one slip.

We’ll be around at the user conference and will gladly help you guys get going with SDK, whether there’s a dedicated session for it or not. (There might be… I’m not very familiar with the schedule though. I know there’s a developer Q&A session, but I think that’s just a general thing.)

There is a session on the SDK at the community conference (Tuesday, 1:30pm)

We will also be available for custom 1-on-1 sessions on that Thursday and Friday, get in touch with your sales rep to schedule one.

[quote=“djones”][u]Carl,

I have several projects started in Eclipse that I developed for integration with Ignition. They are compliled and run a java applications at the moment. One of which is pretty cool. It uses OpenCV and the integrated webcam as a [color=#4f4f4f]barcode scanner to read 1D and 2D barcodes[/color]. The idea is that you would be able to scan a barcode from within Ignition using this module as a method of data entry.

The place that I stumble is getting from a java application to a compiled Ignition module. I have an open post in the SDK section of the forum detailing the issue, but it hasn’t solicited any response.

Me and a couple guys from work are going to the user conference and I was hoping that there would be a section devoted to module development and I could get over the hump. The documentation (at least last time I looked) for the module development as a little thin.

-Doug[/u][/quote]

Have you fixed the issue of barcode scanning? Should it support the usb barcode scanner?

hongdida, I’m not sure who you’re asking, but you might be interested to know that our MES group is working on a barcode module. It is currently in release candidate phase. Ask your salesperson for details.

Can a java barcode reader be integrated and combined with the Ignition SCADA platform? If so, it will help me a lot on barcode reading development.

In what way? You want to actually read barcodes from raster images?

[quote][u]I have several projects started in Eclipse that I developed for integration with Ignition. They are compliled and run a java applications at the moment. One of which is pretty cool. It uses OpenCV and the integrated webcam as a barcode scanner to read 1D and 2D barcodes. The idea is that you would be able to scan a barcode from within Ignition using this module as a method of data entry.

The place that I stumble is getting from a java application to a compiled Ignition module. I have an open post in the SDK section of the forum detailing the issue, but it hasn’t solicited any response.

Me and a couple guys from work are going to the user conference and I was hoping that there would be a section devoted to module development and I could get over the hump. The documentation (at least last time I looked) for the module development as a little thin. [color=#333333]How to integrate barcode scanner into java swing[/color], you may take some studies on this via some [color=#333333]barcode website[/color].[/u]

-Doug

Have you fixed the issue of barcode scanning? Should it support the usb barcode scanner?[/quote]

First time to hear about usb barcode scanner, what’s that? Barcode scanner with a USB interface?