Frequently asked questions about JTable

Source: Internet
Author: User

Lose focus exit edit state;

Table.setclientperpoty ("Terminateeditonfocuslost", true);

Set some columns not editable;

Custom TableModel rewrite iscelleditable method;

Refresh the JTable data:

Table.setmodle (Modle);

Delete data:

Model.remove (int row);

Get the value in JTable:

Jtable1.getvalueat ();

Sort Sorter

Tablerowsorter<tablemodel> sorter = new tablerowsorter<tablemodel> (DTM);
Jtable1.setrowsorter (sorter);

Specific implementation look at the code: when the form starts, no content is displayed.

Package Com.bdc;import Java.awt.borderlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JOptionPane ; Import Javax.swing.jpanel;import Javax.swing.jscrollpane;import Javax.swing.jtable;import Javax.swing.table.defaulttablemodel;import Javax.swing.table.tablemodel;import Javax.swing.table.TableRowSorter; public class Jtabletest extends JFrame {string[][] data={{"AAA", "111"},{"BBB", "222"},{"CCC", "333"}; String[] cols= {"User", "password"};D Efaulttablemodel DTM; JTable jtable1;public jtabletest () {//Set basic Properties Setalwaysontop (True); Setdefaultcloseoperation (exit_on_close); SetSize ( 300,300);//Add Component jtable1= new JTable (DTM); Jtable1.putclientproperty ("Terminateeditonfocuslost", true); Exit the edit state when the focus is lost, and click the Modify button to get the modified value; add (jtable1); JScrollPane jsp= New JScrollPane (Jtable1), add (JSP); JPanel jp1=new JPanel (); Add (Jp1,borderlayout.south); JButton listall=new JButton ("Show All"); Jp1.add (Listall); JButton delete=new JButton ("Delete"); Jp1.add (delete); JButton modify=new JButton ("Modify"); Jp1.add (modify);//Add Event Listener; Listall.addactionlistener (new ActionListener () {@ overridepublic void actionperformed (ActionEvent e) {//Customize a TableModel, re-iscelleditable, set to the first column is not editable; dtm=new DefaultTableModel (Data,cols) {@Overridepublic boolean iscelleditable (int row, int column) {return column==1?true:false ;}};/ /Create a sequencer, set as the sorting property of table; Sorting can be done by clicking on a column name.  Tablerowsorter<tablemodel> sorter = new tablerowsorter<tablemodel> (DTM); Jtable1.setrowsorter (sorter); Jtable1.setmodel (DTM);}}); Delete.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {int a= Jtable1.getselectedrow ();d Tm.removerow (a); Delete a row; Jtable1.setmodel (DTM);}}); Modify.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {int aa= Jtable1.getselectedrow (); int bb=jtable1.getselectedcolumn (); if (aa==-1) {return;} Joptionpane.showmessagedialog (Jtabletest.this, "successfully modified to" +jtable1.getvalueat (AA, BB));}); Public static void Main (string[] args) {new Jtabletest (). setvisible (True);}} 

  

 

Frequently asked questions about JTable

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.