Problem with Java table

7 views (last 30 days)
Arwel
Arwel on 8 Aug 2019
Hi,
I am making a Java gui, and I want to put a JTable in there. The table needs to have 7 colums, the first of which is an (uneditable) name, the fifth is all combo-boxes to select from three options. The remainer are numerical fields, but I want some custom formatting of the numbers.
So, what I've tried is to make the table in Matlab, to write a custom table model, and also a custom renderer for the numerical columns in Java. Here's what I've tried so far
clear java;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
%Make a simple table
myTable = JTable(10,7);
scr = JScrollPane();
scr.setSize(100,100);
fr = JFrame;
fr.setSize(600,400);
fr.getContentPane().add(scr);
fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
headings = {'Parameter Name','Lower Bound','Value','Upper Bound','Prior Type','Mu','Sigma'};
names = {'Dave','Bob','Jim','Freddie','Mary','Sue'};
val = [ 3.1 300 92 3e6 5 28];
min = [ 0 0 0 0 0 0];
max = [ 10 10 10 10 10 10];
rows = length(names);
%Put all the values into an array
for r = 1:rows
Vals{r,1} = names{r};
Vals{r,2} = min(r);
Vals{r,3} = val(r);
Vals{r,4} = max(r);
Vals{r,5} = '';
Vals{r,6} = 0;
Vals{r,7} = Inf;
end
%Apply the TableModel to the JTable
model = bayesGuiMainTableModel(Vals,headings);
table = JTable(model);
% Use the number renderer for the numerical columns
numRows = [1,2,3,5,6];
for i = 1:length(numRows)
aColumn = table.getColumnModel().getColumn(numRows(i));
rend = numberTableCellRenderer();
aColumn.setCellRenderer(rend);
end
%Make a JComboBox
whichPriorCbox = JComboBox();
whichPriorCbox.addItem(java.lang.String('Uniform'));
whichPriorCbox.addItem(java.lang.String('Gaussian'));
whichPriorCbox.addItem(java.lang.String('Jeffreys'));
%set(whichPriorCbox,'ActionPerformedCallback',@boxChange);
aColumn = table.getColumnModel().getColumn(4);
aColumn.setCellEditor(DefaultCellEditor(whichPriorCbox));
renderer = DefaultTableCellRenderer();
aColumn.setCellRenderer(renderer);
scr.setViewportView(table);
fr.show
Then......
import javax.swing.table.DefaultTableModel;
import java.io.Serializable;
import javax.swing.JComboBox;
import javax.swing.table.TableCellRenderer;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.DefaultCellEditor;
public class bayesGuiMainTableModel extends DefaultTableModel implements Serializable{
public bayesGuiMainTableModel(Object[][] data, Object[] columnNames) {
this.setDataVector(data, columnNames);
}
@Override
public void setValueAt(Object value, int row, int column)
{
switch (column) {
case 0:
break;
case 4:
System.out.println(String.valueOf(value));
break;
case 1:
case 2:
case 3:
case 5:
case 6:
if (value instanceof Double) {
super.setValueAt(value,row,column);
} else if (value instanceof String) {
try {
super.setValueAt(Double.valueOf((String)value),row,column); //for debugging
}
catch (NumberFormatException nfe) {
super.setValueAt(Double.valueOf("NaN"),row,column);
break;
}
}
fireTableCellUpdated(row, column);
}
}
@Override
public Class getColumnClass(int columnIndex) {
return super.getColumnClass(columnIndex);
}
@Override
public boolean isCellEditable(int row, int column) {
switch(column){
case 0:
default:
return true;
}
}
}
....and.....
import java.text.DecimalFormat;
import javax.swing.JTable;
import java.awt.Component;
import java.text.NumberFormat;
import javax.swing.JLabel;
import javax.swing.table.TableCellRenderer;
public class numberTableCellRenderer extends JLabel implements TableCellRenderer {
Number numberValue;
NumberFormat nf;
DecimalFormat df = new DecimalFormat("#.####");
DecimalFormat df2 = new DecimalFormat("0.000E0");
String _formattedValue;
public numberTableCellRenderer() {
};
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int col) {
String _value = value.toString();
Double num_value = Double.parseDouble(_value);
if (num_value == null) {
_formattedValue = "-";
} else {
if (((num_value > 1000 ) || (num_value < -1000)) || ((num_value < 9e-3 ) && (num_value > -9e-3)))
_formattedValue = df2.format(value);
else
_formattedValue = df.format(value);
}
JLabel testLabel = new JLabel(_formattedValue);
testLabel.setHorizontalAlignment(CENTER);
return testLabel;
}
}
Now, this kind of works after a fashion. The table appears as I want with the numerical colums dispalyed neatly as I want. The problem is though with the combobox. I can click on it and see the options, select an option, see that it's been selected via the debug println, catch the event in an actionperformedcallback in Matlab and verify the selection, but the selection doesn't display i.e. the combobox stays blank.
So, I'm missing something - probably a problem with the renderer maybe??? I don't know..... I'm not that good with Java tbh, but this is maddening as I'vd had comboboxes working just fine in Java tables beforem and I just can't see what I'm doing differently here.
Does anyone know how to get this working???
Cheers,
Arwel

Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!