Java Experiment--a simple song information management system based on Swing (iv)

Source: Internet
Author: User
Tags event listener gettext rowcount

Original is not easy, reproduced please indicate the source.

In the Java experiment-a simple song information management system based on Swing (iii), we have drawn the login, registration and admin main interface, this film mainly explains the song management interface. The first involves the operation of the table, see the code in the textbook, the code is as follows:

 Packagetest.swing;Importjava.awt.BorderLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.MouseAdapter;Importjava.awt.event.MouseEvent;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JScrollPane;Importjavax.swing.JTable;ImportJavax.swing.JTextField;ImportJavax.swing.ListSelectionModel;ImportJavax.swing.table.DefaultTableModel; Public classExampleframe_06extendsJFrame {/**     *      */    Private Static Final LongSerialversionuid = 1L; PrivateDefaultTableModel TableModel;//Defining tabular Model objects    PrivateJTable table;//Defining Table Objects    PrivateJTextField Atextfield; PrivateJTextField Btextfield;  Public Static voidMain (String args[]) {exampleframe_06 frame=Newexampleframe_06 (); Frame.setvisible (true); }         Publicexampleframe_06 () {Super(); Settitle ("Maintain tabular Model"); SetBounds (100, 100, 500, 375);        Setdefaultcloseoperation (Jframe.exit_on_close); FinalJScrollPane ScrollPane =NewJScrollPane ();        Getcontentpane (). Add (ScrollPane, borderlayout.center); String[] ColumnNames= {"A", "B"};//define table column an array groupString[][] Tablevalues = {{"A1", "B1"}, {"A2", "B2" },                { "A3", "B3"}};//defining tabular data Arrays//Create a tabular model of the specified table column name and table dataTableModel =NewDefaultTableModel (tablevalues, columnnames); Table=NewJTable (TableModel);//Create a table of the specified tabular model//Table.setrowsorter (New Tablerowsorter (TableModel));//set the sequencer for a table//set the selection mode of the table to radioTable.setselectionmode (listselectionmodel.single_selection); //Add a mouse event listener to a tableTable.addmouselistener (NewMouseadapter () {//A Click event has occurred             Public voidmouseclicked (MouseEvent e) {//get the index of the selected row                intSelectedrow =Table.getselectedrow (); //get the value of the specified cell from the tabular modelObject OA = tablemodel.getvalueat (selectedrow, 0); //get the value of the specified cell from the tabular modelObject ob = Tablemodel.getvalueat (Selectedrow, 1); Atextfield.settext (Oa.tostring ());//assign a value to a text boxBtextfield.settext (Ob.tostring ());//assign a value to a text box            }        });        Scrollpane.setviewportview (table); FinalJPanel Panel =NewJPanel ();        Getcontentpane (). Add (Panel, Borderlayout.south); Panel.add (NewJLabel ("A:")); Atextfield=NewJTextField ("A4", 10);        Panel.add (Atextfield); Panel.add (NewJLabel ("B:")); Btextfield=NewJTextField ("B4", 10);        Panel.add (Btextfield); FinalJButton AddButton =NewJButton ("Add"); Addbutton.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {string[] rowvalues={atextfield.gettext (), Btextfield.gettext ()};//Create a table row arrayTablemodel.addrow (rowvalues);//add a row to the tabular model                intRowCount = Table.getrowcount () + 1; Atextfield.settext ("A" +rowCount); Btextfield.settext ("B" +rowCount);        }        });        Panel.add (AddButton); FinalJButton Updbutton =NewJButton ("Modify"); Updbutton.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {intSelectedrow = Table.getselectedrow ();//get the index of the selected row                if(Selectedrow! =-1) {//determine if a selected row existstablemodel.setvalueat (Atextfield.gettext (), Selectedrow,0);//modifying a specified value in a tabular modeltablemodel.setvalueat (Btextfield.gettext (), Selectedrow,1);//modifying a specified value in a tabular model                }            }        });        Panel.add (Updbutton); FinalJButton Delbutton =NewJButton ("Delete"); Delbutton.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {intSelectedrow = Table.getselectedrow ();//get the index of the selected row                if(Selectedrow! =-1)//determine if a selected row exists//Delete a specified row from a tabular modelTablemodel.removerow (Selectedrow);        }        });    Panel.add (Delbutton); }}
View Code

In fact, the difficulty here is how to display data, according to the code of the textbook, can be roughly imitated to write, and then on the basis of the data displayed in the background is good. In order to make the behavior in the table not editable state, so you define a class, inherit to Defaulttablemode, override the isCellEditable method, set to False. None of the others have changed. If you were to think again, you might rewrite a lot of methods (fortunately, they are done in user management), it will be easier to use, and the code will be less.

Other parts are based on conditional search, the following Jradiobutton and the search box shows the corresponding message should be, before considering the click to add, delete, modify the button, is a new form to pop up, but then tried several times also not (fortunately, the user management inside is such a realization), Only imitate the book above the example to do, in the table below a number of added a row, which contains the song information input box and add, modify, delete buttons, imitation examples to achieve.

First look at the design diagram:

  

Java Experiment--a simple song information management system based on Swing (iv)

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.