Java JTable Problems
I've scoured the internet looking for bits and pieces of a solution to my problem, but I haven't seemed to come up with anything yet, and I've really been struggling to solve this.
In short, I need to create a JDialog that embeds a JTable. This JTable has to be dynamic i开发者_开发技巧n regards to its amount of columns. Also, I'd like the first two rows of the JTable to have combo boxes in each of their cells. Row one all has the same combo box, and row two all has the same combo box, different from row one. From there, I'm just filling in static text data for a set number of other rows. So the table should be of the form:
combo, combo, combo, combo, ...
combo, combo, combo, combo, ... text, text, text, text, ...I'm having a lot of trouble a) making the column number dynamic (it will depend on an array passed) and b) making just the first two rows combo boxes - the only way I can find anywhere makes an entire column combo boxes.
If it helps at all, I'm using Netbeans IDE. Thank you very much in advance for your time, and if any further knowledge is needed, just let me know.
how can I return a TableCellEditor returning a JComboBox?
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableComboBoxByRow extends JFrame
{
List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);
public TableComboBoxByRow()
{
// Create the editors to be used for each row
String[] items1 = { "Red", "Blue", "Green" };
JComboBox comboBox1 = new JComboBox( items1 );
DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
editors.add( dce1 );
String[] items2 = { "Circle", "Square", "Triangle" };
JComboBox comboBox2 = new JComboBox( items2 );
DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
editors.add( dce2 );
String[] items3 = { "Apple", "Orange", "Banana" };
JComboBox comboBox3 = new JComboBox( items3 );
DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
editors.add( dce3 );
// Create the table with default data
Object[][] data =
{
{"Color", "Red"},
{"Shape", "Square"},
{"Fruit", "Banana"},
{"Plain", "Text"}
};
String[] columnNames = {"Type","Value"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model)
{
// Determine editor to be used by row
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel( column );
if (modelColumn == 1 && row < 3)
return editors.get(row);
else
return super.getCellEditor(row, column);
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}
public static void main(String[] args)
{
TableComboBoxByRow frame = new TableComboBoxByRow();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
For the dynamic number of columns part, this is defined by the TableModel of your JTable. Use a DefaultTableModel with the appropriate number of columns, or, even better, implement your own table model by extending AbstractTableModel, and implement getColumnCount()
based on the array containing your data.
For the combo part, I guess you want to use a combo to edit the data in the table. You the need to extend JTable and redefine the getCellEditor() method in order to return a TableCellEditor returning a JComboBox if the row is 0 or 1. And you may return super.getCellEditor(row, column)
if the row is bigger than 1.
You should also read the Java Tutorial about JTable, which has sections about everything you want to do.
This thread might help out: Transpose (Rotate) JTable
精彩评论