f



Passing java.lang.Class type as an argument to Java method

I am trying to set the default cell renderer for a JTable from within
Matlab. All my efforts to this point have been based on Sun's 'How To
Use Tables' tutorial. According to methodsview('JTable') the
signature of the function is

setDefaultRenderer(java.lang.Class,javax.swing.table.TableCellRenderer
)

I cannot get this function to executed, and I think its because I
cannot create a java.lang.Class argument from within Matlab.
Following a corresponding code snipit:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
columnNames = {
 'First Name', 'Last Name', 'Sport', ...
 '# of Years', 'Vegetarian', 'Fav. Color'};
           
data = {
 'Mary','Campione','Snowboarding',5,false,Color.RED; ...
 'Alison','Huml','Rowing',3,true,Color.BLUE; ...
 'Kathy','Walrath','Knitting',2,false,Color.GREEN; ...
 'Sharon','Zakhour','Speed reading',20,true,Color.CYAN; ...
 'Philip','Milne','Pool',10,false,Color.ORANGE};
 
% Creat a window with the given name
frame = JFrame('JFrame for Table');
 
% Creat a table and corresponding scrollpane
panel = JPanel();
table = JTable(data, columnNames);
scrollpane = JScrollPane(table);
 
% Set Table Stuff
table.setPreferredScrollableViewportSize(Dimension(500, 70));
table.setDefaultRenderer(java.lang.Class(Color.BLACK),
ColorRenderer(true));

panel.add(scrollpane);
 
frame.getContentPane().add(panel);
frame.setVisible(true);
 
ColorRenderer is a valid and on the class path.

Thank you all for the help.

Regards,
Matt
UCI, CNLM
0
mkrauski (7)
1/14/2005 12:19:15 AM
comp.soft-sys.matlab 211265 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

3 Replies
862 Views

Similar Articles

[PageSpeed] 16

In article <eef88e7.-1@webx.raydaftYaTP>,
 "Matthew Krauski" <mkrauski@uci.edu> wrote:

> I am trying to set the default cell renderer for a JTable from within
> Matlab. All my efforts to this point have been based on Sun's 'How To
> Use Tables' tutorial. According to methodsview('JTable') the
> signature of the function is
> 
> setDefaultRenderer(java.lang.Class,javax.swing.table.TableCellRenderer
> )
> 
> I cannot get this function to executed, and I think its because I
> cannot create a java.lang.Class argument from within Matlab.
> Following a corresponding code snipit:

<snip>

> table.setDefaultRenderer(java.lang.Class(Color.BLACK),
> ColorRenderer(true));

<snip>

That's where the problem is.  It looks like you're trying to call some 
java.lang.Class constructor that takes an instance of java.awt.Color (or 
a plain old java.lang.Object) as an input argument.  The problem is, no 
such constructor exists!  In fact, if you look at the JavaDoc for 
java.lang.Class, there are NO public constructors for it.  This isn't a 
MATLAB problem, it's an incorrect use of Java.

To get your hands on an instance of java.lang.Class, at runtime, I 
recommend using the static method of java.lang.Class, "forName()."  So 
you would do something like this...

table.setDefaultTable(java.lang.Class.forName('java.awt.Color'), 
ColorRederer(true));

Hope this helps,
--
Bob Gilmore, Software Engineer,
The MathWorks, Inc.
0
bgilmore (160)
1/14/2005 6:39:32 AM
Bob Gilmore wrote:
>
> That's where the problem is. It looks like you're trying to call
> some
> java.lang.Class constructor that takes an instance of
> java.awt.Color (or
> a plain old java.lang.Object) as an input argument. The problem
> is, no
> such constructor exists! In fact, if you look at the JavaDoc for
> java.lang.Class, there are NO public constructors for it. This
> isn't a
> MATLAB problem, it's an incorrect use of Java.
>
> To get your hands on an instance of java.lang.Class, at runtime, I
> recommend using the static method of java.lang.Class, "forName()."
> So
> you would do something like this...
>
> table.setDefaultTable(java.lang.Class.forName('java.awt.Color'),
> ColorRederer(true));
>
> Hope this helps,
> --
> Bob Gilmore, Software Engineer,
> The MathWorks, Inc.

Bob,

Thank you for the help. With your corrections the Matlab code worked,
but did not give me the result I expected.

After a little more brushing up, I plan on implementing the table in
pure java.

Regards,
Matt
UCI, CNLM
0
mkrauski (7)
1/14/2005 5:20:20 PM
> After a little more brushing up, I plan on implementing the table
> in
> pure java.
>
> Regards,
> Matt
> UCI, CNLM

Bob,

I hope you are still monitoring this thread. Your help would once
again be greatly appreciated.

I am developing an application which will be used for the
visualization and review of Epoched EEG data. It will be composed of
a set of simple controls (buttons, text fields,...), two tables
allowing the selection of data channels and epochs, and data
panel(s). The data panel will contain a series of axes on which the
selected channels/epochs will be plotted.

My hope is to implement the controls, tables, and panel(s) in Java
and use Matlab to draw the plots. All the relevant Java components
will be members of a single class (PoetMainPanel), which will also
handle a set of custom events. The events will be used to trigger
changes in the Matlab application. For example, (1) user changes
channel table selection, (2) PoetMainPanel fires
ChannelSelectionChanged event, and (3) Matlab redraws the
corresponding plots.

Is it possible to have Matlab handles custom Java events? If so how?

The following is a pseudo code snippet of what I have working and
what I intend...

 
javaaddpath '\myjavapath'
import javax.swing.*;
import java.awt.*;
 
mainPanel = PoetMainPanel;

h = figure;
% create java component for PoetMainPanel top level JComponent.
[comp, cont] = javacomponent(mainPanel.getPane(),[],h);
% make the container take up the whole figure window
set(cont,'Units','normalized');
set(cont,'Position',[0 0 1 1]);

set(mainPanel, ...
    'ChannelSelectionChanged',@ChannelChangedCallback);
% ... register other callbacks
 
return;

function ChannelChangedCallback(args)
% do some cool plotting stuff
 
Regards,
Matt
UCI, CNLM
0
mkrauski (7)
1/20/2005 9:30:55 PM
Reply: