How to save the modified value of an editable unit in jtable instantly

Source: Internet
Author: User

Let's take a look at this example. To save the changed status, pay attention to its setvalueat method.

Import javax. Swing. defaultcelleditor;
Import javax. Swing. jcombobox;
Import javax. Swing. jframe;
Import javax. Swing. jpanel;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtable;
Import javax. Swing. Table. abstracttablemodel;
Import javax. Swing. Table. defaulttablecellrenderer;
Import javax. Swing. Table. tablecellrenderer;
Import javax. Swing. Table. tablecolumn;
Import java. AWT. component;
Import java. AWT. dimension;
Import java. AWT. gridlayout;

/**
* Tablerenderdemo is just like tabledemo, doesn't that it
* Explicitly initializes column sizes and it uses a combo box
* As an editor for the sport column.
*/
Public class tablerenderdemo extends jpanel {
Private Boolean DEBUG = false;

Public tablerenderdemo (){
Super (New gridlayout (1, 0 ));

Jtable table = new jtable (New mytablemodel ());
Table. setpreferredscrollableviewportsize (new dimension (500, 70 ));
Table. setfillsviewportheight (true );

// Create the scroll pane and add the table to it.
Jscrollpane scrollpane = new jscrollpane (table );

// Set up column sizes.
Initcolumnsizes (table );

// Fiddle with the sport column's cell Editors/renderers.
Setupsportcolumn (table, table. getcolumnmodel (). getcolumn (2 ));

// Add the scroll pane to this Panel.
Add (scrollpane );
}

/*
* This method picks good column sizes.
* If all column heads are wider than the column's cells'
* Contents, then you can just use column. sizewidthtofit ().
*/
Private void initcolumnsizes (jtable table ){
Mytablemodel model = (mytablemodel) Table. GetModel ();
Tablecolumn column = NULL;
Component comp = NULL;
Int headerwidth = 0;
Int cellwidth = 0;

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.