Article ownership belongs to me, please contact me if you want to use it, if you want to pay, please indicate the source and the author. Powered by GTM is very happy to write down your own development 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 (Hashmap 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 = contain. Size () - 1; while (i> = 0) {ComboBox.additem (Contain.get (i -));} Table.PrepareEditor (New Defaultcelleditor (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 defaultcelleditor (jcbox), J, 1);} J -; } table.updateUI (); * / / * table.setDefaultEditor (Vector.class, new MyTableCellEditor ()); table.setDefaultRenderer (Vector.class, new MyComboBoxRenderer ()); * /} public int getColumnCount () {return 2 PUBLIC INT getRowCount () {int RV = data.size (); return rv;} public string getColumnname (int co) {return "; // This is very interesting, no this" "There is no common header, Everyone can cancel it and then look at the effect} public object getValueat (int Row, int co) {if (col == 0) {Object [] l = data.keyset (). Toservation (); // This is to build JTABLE The specific method of each Cell and the set correspond to return ol [rot];
} el2 = data.values (). Toarray (); return}} / * * jtable buy}} / * * jtable uses this method 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. * / / * * DON '' NEED TO IMPLEMENT THISMETHOD UNSD YOUR TABLE'S * Editable. * / Public Boolean IscelleDitable (int Row, Int Col) {// Note That the data / cell address is constant, // no matter where the cell appears onscreen. Return col == 1;} / * * don't need to import THIS Method unless, * / public void setvalueat (Object value, int rot, int co) {if (col == 1) {Object [] l = data.values (). toarray (); l Row] = value;} fiRetablecellupdated (row, col);}} Since the beginning is changed from the example of others, it is misleaded because JTable itself comes from the default Editor like ComboBox and Checkbox, etc. It can generally be obtained by using a getColumnClass method in Model. Note !! This is the key, but also consumes a holiday for my national day holiday !! If this method is renovated in this internal class, it will be able to obtain the default editor to rewrite It is very convenient, as follows: Public void setupsportColumn (JTable Table, TableColumn C olumn) {// Set up the editor for the sport cells JComboBox comboBox = new JComboBox ();. comboBox.addItem ( "Snowboarding"); comboBox.addItem ( "Rowing"); comboBox.addItem ( "Knitting"); comboBox .additem ("speted ready"); ComboBox.Additem ("pool"); ComboBox.Additem ("None of the Above"); Column.SetCelleditor (New Defaultcelleditor (ComboBox)); // SET UP TOOL TIPS for Tips for the Sport Cells. DefaultTableCellrenderer RENDERER = New DefaultTableCellrenderer (); renderer.SetTooltiptext ("Click for Combo Box"); Column.SetCellrenderer (rendere);
} Then add a getColunMClass method to the internal class just above, return to the Class of a column of a row (note, if the Model structure is Object [] [], you can find it in the Internet) but Not suitable for our needs, because only 2 lines are only 2 lines, and there is no column of performance. So in JTABLE itself, it is his getcelleditor (int Row, Int Column), this is my repeated single-step debugging The final conclusion, because each Cell of JTable 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 is used in the same This is a nightmare, when I discovered. Then I spent a lot of time, I found that its prepareEditor (TableCelleditor Editor, TableClass, TableCelleditor Editor) can be passed by the object. Class to get the corresponding editor and render, but in jtable, the key is getCelleditor (int Row, int column) Can you correctly return the class of a column of a row because I use the HashMap (such intention, in addition to the key value) Just when the object is taken, it automatically encapsulates a JCOMBOBOX to modify it. But actually getting the getcelleditor (int Row, int column) internal code only uses coluMn to do not use ROW !! If you use Model's getColumnClass Getting the parameters of the ROW, I still chose inheritance without destroying the integrity of the package, and rewrive the getcelleditor (int Row, Int Column). Return getDefaulteditor (this.getModel (). GetValueat (Row, Column). GetClass ()); then encapsulate yourself and renderer in GUI, such as: public class mycomboxrenderer imports TableCellrenderer () {} public {} 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;