Review JAVA WinForm Practices (4)

Source: Internet
Author: User

Today, this article mainly implements the double-click modification function. It is said that the java winform program is really broken, and I am confused about various problems.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZV604-0.png "title =" QQ20131125170812.png "alt =" 170828503.png"/>


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZS919-1.png "title =" qq201311251639.png "alt =" 164558682.png"/>

This function is to double-click a row in Jtable, and then the modification page is displayed. After the modification, the data is refreshed to Jtable. At the same time, this modification interface also supports page number changes, that is, you can click the previous page, next page, and other buttons to capture the data of the corresponding row in the Jtable.

Double-click event first. in C #, The DataGridView double-click event exists, but JTable does not. What should we do? I have read another article. Silverlight MVVM is close to the double-click Implementation of the main interface. Today, java double-click is similar to that one, the principle is to double-click monitoring when the two clicks are completed within the specified time range.

TimerTask timTsk;    Timer tim = new Timer();    Boolean isTimerRuning = true;    String ename=\"\";    private void TimTaskInit() {        timTsk = new TimerTask() {            public void run() {                isTimerRuning = false;            }        };        tim.schedule(timTsk, 200, 200);    }    private void ShowModifyForm() throws IllegalArgumentException,            IllegalAccessException {        if (isTimerRuning) {            isTimerRuning = false;            FrmCodeModify frmCodeModify = new FrmCodeModify(table,this);            frmCodeModify.setDefaultCloseOperation(DISPOSE_ON_CLOSE);            StartMain.SetMidScreen(frmCodeModify);            frmCodeModify.setVisible(true);        } else {            TimerScheduleReset(timTsk);            isTimerRuning = true;            tim.schedule(timTsk, 200, 200);        }    }    private void TimerScheduleReset(TimerTask timTsk) {        Field field;        try {            field = TimerTask.class.getDeclaredField(\"state\");            field.setAccessible(true);            try {                field.set(timTsk, 0);            } catch (IllegalArgumentException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } catch (IllegalAccessException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        } catch (NoSuchFieldException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SecurityException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

Call ShowModifyForm () when clicking Jtable ()

Table. addMouseListener (new MouseAdapter () {@ Override public void mouseClicked (MouseEvent e) {try {if (table. getSelectedRow () =-1) {MessageHelper. showMessage (\ "double-click the row to be modified! \ "); Return;} ShowModifyForm ();} catch (IllegalArgumentException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (IllegalAccessException e1) {// TODO Auto-generated catch block e1.printStackTrace ();}}});

When calling the pop-up interface, define a timer. Its task is executed once every Ms. After each interval, the variable isTimerRuning is set to false. When you click it for the first time, set it to true. When you click it for the second time to determine whether it is true, the two clicks are completed within Ms, this simulates the double-click event.

OK. After the double-click event is simulated, the modification interface is opened. Here I directly passed the Jtable and the main window. In fact, only one main window can be passed. As for why, let's talk about it later.

FrmCodeModify frmCodeModify = new FrmCodeModify(table,this);

Okay. After the interface is opened

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZQ459-2.png "title =" QQ20131125164944.png "alt =" 165002813.png"/>

We found that this field is more remarks than the previous article, so we added a field

String sql = "SELECT 0 as bit,data,ename,cname,display_content,remark  FROM dbo.Codes WHERE ename='"

But this column is not used to display in jtable, So hide it

CommonFunctions.hiddenColumn(5, table);public class CommonFunctions {    public static void hiddenColumn(int columnIndex,JTable table){         TableColumnModel tcm=table.getColumnModel();         TableColumn tc=tcm.getColumn(columnIndex);         tc.setWidth(0);         tc.setPreferredWidth(0);         tc.setMaxWidth(0);         tc.setMinWidth(0);         table.getTableHeader().getColumnModel().getColumn(columnIndex)              .setMaxWidth(0);         table.getTableHeader().getColumnModel().getColumn(columnIndex)             .setMinWidth(0);     }}

Okay. We can load the data in the jtable on the home page through the pages above. First, initialize the code.

public FrmCodeModify(JTable tb,JFrame jf) {        InitForm();        table = tb;        this.jf=jf;        Init();    }    /**     * Create the frame.     */    public FrmCodeModify() {        addWindowListener(new WindowAdapter() {            @Override            public void windowClosed(WindowEvent e) {                ((FrmSysCodeMng)jf).BindTable();            }        });        InitForm();    }

During initialization, the passed jtable and jf values are attached. Call Init (). Before using Init, I would like to explain why java does not provide a solution for calling a constructor without parameters. In C #, it is implemented by the parameter: this. Okay. Let's take a look at Init.

private void Init() {        rowIndex = table.getSelectedRow();        this.RowIndexChanged(rowIndex);        this.RefreshPagerState();        RegisterEventListener();    }    private void RefreshPagerState() {        labFirst.setEnabled(true);        labPrevious.setEnabled(true);        labNext.setEnabled(true);        labLast.setEnabled(true);        if (rowIndex == 0) {            labFirst.setEnabled(false);            labPrevious.setEnabled(false);        }        if (rowIndex == table.getRowCount() - 1) {            labNext.setEnabled(false);            labLast.setEnabled(false);        }    }    private void RowIndexChanged(int rowIndex) {        String data = table.getValueAt(rowIndex, 1).toString();        String ename = table.getValueAt(rowIndex, 2).toString();        String cname = table.getValueAt(rowIndex, 3).toString();        String display = table.getValueAt(rowIndex, 4).toString();        String remark = table.getValueAt(rowIndex, 5).toString();        this.txtDisplay.setText(display);        this.labCName.setText(cname);        this.labEname.setText(ename);        this.labData.setText(data);        this.txtRemark.setText(remark);    }    private void GetRowIndex(PagerType pt) {        switch (pt) {        case FIRST:            rowIndex = 0;            break;        case PREVIOUS:            rowIndex--;            break;        case NEXT:            rowIndex++;            break;        case LAST:            rowIndex = table.getRowCount() - 1;            break;        }    }    private void RegisterEventListener() {        JLabel[] labels = new JLabel[] { labFirst, labPrevious, labNext,                labLast };        for (JLabel lb : labels) {            lb.addMouseListener(new MouseAdapter() {                @Override                public void mouseClicked(MouseEvent e) {                    PagerType pt;                    String labName = ((JLabel)e.getComponent()).getName();                    MessageHelper.ShowMessage(labName);                    switch (labName) {                    case "labFirst":                        pt = PagerType.FIRST;                        break;                    case "labPrevious":                        pt = PagerType.PREVIOUS;                        break;                    case "labNext":                        pt = PagerType.NEXT;                        break;                    case "labLast":                        pt = PagerType.LAST;                        break;                    default:                        pt = PagerType.FIRST;                        break;                    }                    GetRowIndex(pt);                    RowIndexChanged(rowIndex);                    RefreshPagerState();                }            });        }    }    enum PagerType {        FIRST, PREVIOUS, NEXT, LAST    }

The heap of code is used to initialize data. First, obtain the row number of the jtable double-click row on the parent page. Place the value on the modification page. Refresh the page button status and register a unified event handler for the page button. Display by page. Last modify data, save

Private void UpdateCodeInformation () {String display = txtDisplay. getText (). trim (); String remark = txtRemark. getText (). trim (); String ename = labEname. getText (); String data = labData. getText (); String SQL = "Update TOP (1) dbo. codes SET display_content = '"+ display + "', remark = '"+ remark +" 'where ename =' "+ ename +" 'AND data =' "+ data +" '"; JDBCSqlHelper. update (SQL); MessageHelper. showMessa Ge ("saved successfully! "); This. dispose ();}

In the end, the passed this is to refresh the jtable after the modification page is closed.

public FrmCodeModify() {        addWindowListener(new WindowAdapter() {            @Override            public void windowClosed(WindowEvent e) {                ((FrmSysCodeMng)jf).BindTable();            }        });        InitForm();    }

Well, wash and sleep. I rely on it. It's only noon. It's just dinner. Let's say this java compilation is going to run everywhere and come up on linux. First, export the generated jar package to the FTP service folder of Windows 7. View in linux

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZU543-3.png "title =" QQ20131125185800.png "alt =" 195035507.png"/>

Okay. Copy the GRLZ. JAR and JBDC4 drivers to/opt/GRLZ /.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZU0O-4.png "title =" 2.png" alt = "195156251.png"/>

Set JDBC Environment Variables

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZTL2-5.png "title =" QQ20131125195246.png "alt =" 195302311.png"/>

OK. Run it now. Open the terminal and enter the following command to open the interface.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZSB9-6.png "title =" QQ20131125195347.png "alt =" 195408655.png"/>

But I cannot enter my password.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZS0U-7.png "title =" QQ20131125195510.png "alt =" 195521527.png"/>

As a result, I directly click the login button to bring up a prompt

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZR3a-8.png "title =" QQ20131125195607.png "alt =" 195618395.png"/>

Since this prompt is displayed, I can enter the password. I depend on it. At this time, I would like to ask the interviewers who love to ask others about the principle. What is the principle and why is it like this.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/122ZV636-9.png "title =" QQ20131125195708.png "alt =" 195717926.png"/>

I reported that I couldn't find the JDBC driver, and my husband was depressed. I haven't done a good job for a long time. Give up. If anyone comes in, give me some advice.

This article is from the "technology creates value" blog, please be sure to keep this source http://leelei.blog.51cto.com/856755/1331230

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.