Article ownership belongs to me, please contact me if you want to use it, if you want to post, please indicate the source and the author. Powered by GTM I am happy to write down your own experience, once again change, from the bottom of the data. GUI of the interface, once again challenge, I think I am growing. Waiting for me to develop new things or solve new problems, write a history of blood and tears, the National Day holiday will give this attribute watch. Too many, Into the topic: I have known JB know that JB's Design is a good interface. This time I have participated in the project to write a APLHA version, so I want to write a similar IDE, I am responsible for the property sheet and tree. Original implementation A JTable is not difficult (although I have a few detours at the beginning, I have to engage in jlist), as long as it is a property name, it is worth it. Event response or drag is not the main problem, it is easy to implement, but Implementation can be a bit trouble, if it is a different manifestation, if it is in different column, if it is in the same cloumn ... I wrote an internal class to implement the establishment of JTABLE (Take a look at the relevant basic tutorial) for the establishment of the JTABLE (the specific API), for the future dynamic generation data, because the data connection I can set, so I will use HashMap The property name is the attribute value, the following is the specific code: Class MyTableModel Extends AbstractTableModel {HashMap Data;
Public void setdata (havehmap hm, jtable table) {data = hm
/ * while (j> = 0) {type = array [j] .getclass (). TOSTRING (); if ("class java.util.vector)) {Contain = (Vector) array [j ]; JcomboBOX ComboBox = new jcomboBox (); int i = contain.size () - 1; while (i> = 0) {ComboBox.Additem (contain.get (i -));} Table.PrepareEditor (New Defaultcelledge) (ComboBox), J, 1);} IF (type.equals ("class java.lang.boolean)) {boolean tf = (boolean) array [j];
JCHECKBOX JCBOX = New jcheckbox ("", tf.BooleanValue ()); Table.GetCelleditor (J, 1) = New Defaultcelledge (JCBOX), J, 1);
} J -;
} Table.UpdateUi (); * /
/ * Table.SetDefaulteditor (Vector.class, New MyTableCelleditor ()); Table.SetDefaultrendere (Vector.class, New MyComboxRenderer ()); * /} public int getColumnCount () {Return 2;}
Public int achievecount () {int RV = DATA.SIZE (); Return RV;}
Public String getColumnName (int COL) {return ""; // This is very interesting, no this "" There is no common header, you can cancel it and then look at the effect}
Public Object getValueat (int Row, int co) {i (inc == 0) {Object [] l = data.keyset (). ToArray (); // This is to establish a specific method of jtable each Cell and SET corresponds to Return OL [row];
} else {Object [] ol2 = data.values (). Toarray (); return l l2 [row];}
/ * * JTable Uses this method to determine the default renderer / * Editor for Each Cell. IF We Didn't Implement this method, * THEN THE Last Column Would Contain Text ("True" / "false"), * Rather Than A Check box. * /
/ * * Do not need the cell appears to implement this method unless your table's * editable. * / Public boolean isCellEditable (int row, int col) {// Note that the data / cell address is constant, // no matter where OnScreen. Return COL == 1;}
/ * * DON '' = data. VALUES (). Toarray (); l [row] = value;} fiRetablecellupdated (row, col);}
}
Since it is changed from the example from others, it is misleaded because JTable itself comes with the default Editor like ComboBox and Checkbox, etc., can generally be obtained by getColumnClass method in Model. Note !! Here is the key, but also consumes the culogue of my national holiday !! If you renote this method in this internal class, you will be able to get the default Editor to rewrite, it is very convenient, as follows: Public void setupsportColumn (JTable Table, TableColumn Column ) {// set up the editor for the sport cells. JCOMBOBOX ComboBox = new jComboX (); ComboBox.Additem ("Snowboarding"); ComboBox.Additem ("Rowing"); ComboBox. "); ComboBox. AddItem ("Speed Reading"); ComboBox.Additem ("pool"); ComboBox.Additem ("None of the Above"); Column.Setcelleditor (New DefaultCelleditor (ComboBo));
. // Set up tool tips for the sport cells DefaultTableCellRenderer renderer = new DefaultTableCellRenderer (); renderer.setToolTipText ( "Click for combo box"); Column.setCellRenderer (renderer);} and add a getColunmClass just above the inner classes Method, returning to a Class of a column of a row (note, if the Model structure is Object [] [], you can find it in the Internet) but is not suitable for our needs, because the property table is only 2 lines. And there may be different performances. So in JTABLE itself is his getcelleditor (int Row, int column), this is the final conclusion I have obtained in repeated single-step debugging, because JTable's renderer And Editor needs to be positioned. The so-called positioning is to find the corresponding editor and render, and its internal implementation is obtained according to Column, that is, the whole column uses the same editor. This is a disaster, when I found it Then I spent a lot of time, I found that its prepareEditor (int ROW, INT Column), and setdefaulteditor (Class ColumnClass, TableCelleditor Editor) The latter can get the corresponding editor and render by the object's Class, but In jtable, the key is that getcelleditor (int Row, int column) can correctly return Class of a column of a list because I use hashmap (such intention to correspond to the corresponding object is a vector, automatic packaging A JCOMBOBOX is modified). But actually getting a getcelleditor (int ROW, INT Column) internal code only uses the column roasting without use Row !! If you use Model's getColumnClass, you cannot get the ROW parameters, without destroying the integrity of the package Under the premise, I still chose inherit, put getcelleditor. . Row, int column) rewrite return getDefaultEditor (this.getModel () getValueAt (row, column) .getClass ()); then encapsulated in the GUI and their Editor Renderer, such as: public class MyComboBoxRenderer implements TableCellRenderer {public MyComboBoxRenderer () {}
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {Vector vv = null; if (value instanceof Vector) {vv = (Vector) value;
JCOMBOBOX BOX = New JCOMBOBOX (VV);
if (isSelected) {setForeground (table.getSelectionForeground ()); box.setBackground (table.getSelectionBackground ());} else {setForeground (table.getForeground ()); setBackground (table.getBackground ());} // Select The current value box.setSelectedItem (Value);
return box;} return null;}} there Renderer public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {// This is the component that will handle the editing of the cell value JComboBox box;
// This method is called when a cell value is edited by the user. Public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {// 'value' is value contained in the cell located at ( RowIndex, vcolindex) // jcombobox box = null; vector vv = null; // if (value instanceof vector) {vv = (vector) value;
Box = New JCOMBOBOX (VV);
if (isSelected) {setForeground (table.getSelectionForeground ()); box.setBackground (table.getSelectionBackground ());} else {setForeground (table.getForeground ()); setBackground (table.getBackground ());}
// select the current value box.setSelectedItem (Value);
Return Box; //} // Return NULL;} can be modified by JCOMBOBOX according to if a vector is a vector, if it is Boolean, you can use Checkbox. It is also the most important thing to understand the mechanism of JTABLE. Ok I have solved the problem here, if you have any questions, please contact me.