Enum question

L

Larry Coon

I'm going to try to not be long-winded with this explanation,
but I don't know how good a job I'm going to do. Suppose I
have a bean:

public class MyBean {
String firstValue;
String secondValue;

myBean() { }

public String getFirstValue() {
return firstValue;
}
public String setFirstValue(String s) {
firstValue = s;
}
public String getSecondValue() {
return secondValue;
}
public String setSecondValue(String s) {
secondValue = s;
}
}

I'm going to display a collection of these beans in a JTable,
so I write a table model. My first inclination would be to
add a couple of static arrays with column names & widths, ie:
public static final int[] COL_WIDTHS = {30, 50};
public static final String[] COL_NAMES =
{"First value", "Second value"};

But to make it cleaner, I thought I'd use an enum:

import java.util.*;
import javax.swing.table.*;

public class MyBeanTableModel extends AbstractTableModel {
public enum Column {
FIRST_VALUE(30, "First value"),
SECOND_VALUE(50, "Second value");

private int colWidth;
private String colHeading;

Column(int colWidth, String colHeading) {
this.colWidth = colWidth;
this.colHeading = colHeading;
}

public int colWidth() { return colWidth; }
public String colHeading() { return colHeading; }

private static Column getColumn(int col) {
return Column.values()[col];
}
}

private List<MyBean> list = new ArrayList<MyBean>();

public int getPreferredWidth(int col) {
return Column.getColumn(col).colWidth();
}

public String getColumnName(int col) {
return Column.getColumn(col).colHeading();
}

public int getRowCount() {
return list.size();
}

public int getColumnCount() {
return Column.values().length;
}

public Object getValueAt(int row, int col) {
switch(col) {
case 0: return list.get(row).getFirstValue();
case 1: return list.get(row).getSecondValue();
}

return null;
}
}


I have two questions:

1. My method getColumn() in the enum seems kludgy. Is there
a better way to return a member of the enumeration based
on its ordinal position within the enumeration values?

2. My use of the case construct in getValueAt() also seems
kludgy. Is there a way to avoid the case by including the
name of the getter method in the enumeration? Something
like (this is completely invalid syntax, but hopefully it
conveys what I'm trying to accomplish):

public enum Column {
FIRST_VALUE(30, "First value", MyBean.getFirstValue),
SECOND_VALUE(50, "Second value", MyBean.getSecondValue);

private int colWidth;
private String colHeading;
private Method getter;

Column(int colWidth, String colHeading, Method method) {
this.colWidth = colWidth;
this.colHeading = colHeading;
this.method = method;
}

public Method method() {
return method;
}

// . . . .
}


then getValueAt would look something like:

public Object getValueAt(int row, int col) {
return Column.getColumn(col).method().invoke(list.get(row));
}

Thanks for any advice.


Larry Coon
University of California
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top