Swing-jtable Renderer and editor using demo

Source: Internet
Author: User

The content, appearance, and event response of JTable are largely controlled by renderers and editors. Specifically, the renderer is responsible for the appearance of cells such as foreground color, background color, and cell hints, and the editor is responsible for cell contents and event responses. The editor defaults to the form of a text box, or you can use a drop-down menu, a button, a radio button, and so on. The following is a demo program to illustrate the basic use of renderers and editors.


 Packagejbuttontableexample;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;ImportJavax.swing.JCheckBox;ImportJavax.swing.JFrame;ImportJavax.swing.JScrollPane;Importjavax.swing.JTable;ImportJavax.swing.table.DefaultTableModel; Public classJbuttontableexampleextendsJFrame { Publicjbuttontableexample () {Super("Jbuttontable Example"); //adding TableModel and tabular dataDefaultTableModel DM =NewDefaultTableModel (); Dm.setdatavector (NewObject[][] {"button 1", "foo" },                { "Button 2", "Bar"},NewObject[] {"button", "String" }); JTable Table=NewJTable (DM); //Add rendererTable.getcolumn ("button"). Setcellrenderer (Newbuttonrenderer ()); //Add editorTable.getcolumn ("button"). Setcelleditor (NewButtoneditor (NewJcheckbox ())); //GUI SettingsJScrollPane scroll =NewJScrollPane (table);        Getcontentpane (). Add (scroll); SetSize (400, 100); SetVisible (true); }     Public Static voidMain (string[] args) {jbuttontableexample frame=Newjbuttontableexample (); Frame.addwindowlistener (NewWindowadapter () { Public voidwindowclosing (windowevent e) {system.exit (0);    }        }); }}

The above is the main program, the construction of the GUI, table and model. and set the renderer and editor for the table.


 Packagejbuttontableexample;ImportJava.awt.Color;ImportJavax.swing.JButton;Importjavax.swing.JComponent;Importjavax.swing.JTable;ImportJavax.swing.table.TableCellRenderer;classButtonRendererextendsJButtonImplementsTablecellrenderer { Publicjcomponent gettablecellrenderercomponent (JTable table, Object value,BooleanIsSelected,BooleanHasfocus,intRowintcolumn) {          //value from editorString Text = (Value = =NULL) ? "": Value.tostring (); //button TextsetText (text); //Cell Hintssettooltiptext (text); //Background ColorSetBackground (Color.Black); //Front ViewSetforeground (Color.green); return  This; }    }

Custom renderers must implement the Tablecellrenderer and Gettablecellrenderercomponent methods. In this method, you can specify the renderer's text, cell hints, colors, fonts, and so on. Note that the text here is only the displayed content, not the actual data of the cell.


 Packagejbuttontableexample;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJavax.swing.DefaultCellEditor;ImportJavax.swing.JButton;ImportJavax.swing.JCheckBox;Importjavax.swing.JComponent;ImportJavax.swing.JOptionPane;Importjavax.swing.JTable;classButtoneditorextendsDefaultcelleditor {protectedJButton button;//represent the celleditorcomponent      PrivateString Cellvalue;//Save Celleditorvalue       Publicbuttoneditor (Jcheckbox checkBox) {Super(CheckBox); Button=NewJButton (); Button.setopaque (true); Button.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {joptionpane.showmessagedialog (button, Cellvalue+ ": ouch!"); //Refresh Rendererfireeditingstopped ();      }        }); }       Publicjcomponent gettablecelleditorcomponent (JTable table, Object value,BooleanIsSelected,intRowintcolumn) {        //value from cell valueCellvalue = (Value = =NULL) ? "": Value.tostring (); returnbutton; }      PublicObject Getcelleditorvalue () {return NewString (Cellvalue); }    }

The Editor implements a button, and the corresponding business code is in its event response function. The custom renderer must inherit the Defaultcelleditor and reload its Gettablecelleditorcomponent method. gettablecelleditorcomponent parameters provide table objects, cell values, cell check marks, and cell coordinates, and return the JComponent component that represents the renderer for that cell. In this method, we save the cell value as a member variable cellvalue and return a button. The Getcelleditorvalue method is used to return a cell value, which needs to be specified by our display. In the event response function of the button, this demo outputs the corresponding cell value via the popup dialog. Then, the Fireeditingstopped method is called to flush the renderer. Note that this step does not have to be small, because the business code in the renderer is likely to change the cell value, causing the renderer to have to be refreshed.

The results are as follows:

Swing-jtable Renderer and editor using demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.