tab与list配合使用
生活随笔
收集整理的這篇文章主要介紹了
tab与list配合使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
tab與list配合使用會(huì)使效果增添不少,可是在之前沒有接觸過此框架的要做到很好卻是有點(diǎn)小難度,以下就按我在開發(fā)過程中遇到的問題,一一羅列出來,并附上解決的方法。
如圖:
?? ? ? ? ??
?
問題一:首先我從登陸窗體進(jìn)入時(shí),我不希望每個(gè)Tab選項(xiàng)卡中的List中都去服務(wù)器取數(shù)據(jù),這樣會(huì)導(dǎo)致加載時(shí)間過長(zhǎng)導(dǎo)致用戶等待過久,因?yàn)門ab是在窗體初始化的時(shí)候就要把List中的數(shù)據(jù)添加進(jìn)去,如果要分別加載數(shù)據(jù)則在tab事件中必須重新加載之前的數(shù)據(jù)。
解決方法:此問題的關(guān)鍵是Container的使用,我們可以為每個(gè)選項(xiàng)卡添加一個(gè)Container,然后觸發(fā)tab事件時(shí)我們改變的只是Container中的數(shù)據(jù)及控件,所以這樣最完美了解決了存在的問題,創(chuàng)建list的代碼就不重復(fù)了,需要請(qǐng)看這里。
?
?? ? ? ?//?個(gè)人郵件
?? ? ? ?String[][]?mailPersonal?=?GetMailPersonal();
?? ? ? ?MailInfo[]?mailInfoPersonal?=?GetMainInfoArrary(mailPersonal,?true);
?? ? ? ?containerPersonal?=?new?Container();
?? ? ? ?containerPersonal.setLayout(new?BoxLayout(BoxLayout.Y_AXIS));
?? ? ? ?if?(mailInfoPersonal?!=?null)
?? ? ? ? ? ?containerPersonal.addComponent(createList(mailInfoPersonal,
?? ? ? ? ? ? ? ? ? ?List.VERTICAL,?new?ContactsRenderer()));
?? ? ? ?else?{
?? ? ? ? ? ?Label?label?=?new?Label("您的個(gè)人郵件沒有數(shù)據(jù)!");
?? ? ? ? ? ?containerPersonal.getStyle().setMargin(Component.TOP,?60);
?? ? ? ? ? ?containerPersonal.setLayout(new?FlowLayout(Component.CENTER));
?? ? ? ? ? ?containerPersonal.addComponent(label);
?? ? ? ?}
?? ? ? ?tab.addTab("個(gè)人郵件",?getRes().getImage("SJX.gif"),?containerPersonal);
?? ? ? ?//?群發(fā)郵件,未加載數(shù)據(jù)賦空數(shù)值
?? ? ? ?String[][]?mailGroup?=?{?{?"正在加載",?"",?"",?"",?"",?"",?""?}?};
?? ? ? ?MailInfo[]?mailInfoGroup?=?GetMainInfoArrary(mailGroup,?false);
?? ? ? ?final?Container?ContainerGroup?=?new?Container();
?? ? ? ?ContainerGroup.setLayout(new?BoxLayout(BoxLayout.Y_AXIS));
?? ? ? ?ContainerGroup.addComponent(createList(mailInfoGroup,?List.VERTICAL,
?? ? ? ? ? ? ? ?new?ContactsRenderer()));
?? ? ? ?tab.addTab("群發(fā)郵件",?getRes().getImage("SJX.gif"),?ContainerGroup);
?? ? ? ?//?保留郵件,未加載數(shù)據(jù)賦空數(shù)值
?? ? ? ?String[][]?mailSave?=?{?{?"正在加載",?"",?"",?"",?"",?"",?""?}?};
?? ? ? ?MailInfo[]?mailInfoSave?=?GetMainInfoArrary(mailSave,?true);
?? ? ? ?final?Container?ContainerSave?=?new?Container();
?? ? ? ?ContainerSave.setLayout(new?BoxLayout(BoxLayout.Y_AXIS));
?? ? ? ?ContainerSave.addComponent(createList(mailInfoSave,?List.VERTICAL,
?? ? ? ? ? ? ? ?new?ContactsRenderer()));
?? ? ? ?tab.addTab("保留郵件",?getRes().getImage("SJX.gif"),?ContainerSave);
?? ? ? ?//?發(fā)件箱,未加載數(shù)據(jù)賦空數(shù)值
?? ? ? ?String[][]?mailSend?=?{?{?"正在加載",?"",?"",?"",?"",?"",?""?}?};
?? ? ? ?MailInfo[]?mailInfoSend?=?GetMainInfoArrary(mailSend,?true);
?? ? ? ?final?Container?ContainerSend?=?new?Container();
?? ? ? ?ContainerSend.setLayout(new?BoxLayout(BoxLayout.Y_AXIS));
?? ? ? ?ContainerSend.addComponent(createList(mailInfoSend,?List.VERTICAL,
?? ? ? ? ? ? ? ?new?ContactsRenderer()));
?? ? ? ?tab.addTab("發(fā)件箱",?getRes().getImage("SJX.gif"),?ContainerSend);
?? ? ? ?tab.setTabPlacement(TabbedPane.TOP);
?? ? ? ?addComponent(BorderLayout.CENTER,?tab);
?? ? ? ?//?tab事件,觸發(fā)時(shí)請(qǐng)求數(shù)據(jù)并向Container添加已經(jīng)填充好數(shù)據(jù)的list
?? ? ? ?tab.addTabsListener(new?SelectionListener()?{
?? ? ? ? ? ?public?void?selectionChanged(int?oldSelected,?int?newSelected)?{
?? ? ? ? ? ? ? ?int?indexvalue?=?tab.getSelectedIndex();
?? ? ? ? ? ? ? ?if?(indexvalue?==?1?&&?flagGroup?==?0)?{
?? ? ? ? ? ? ? ? ? ?Global.GetWaitCursor("正在轉(zhuǎn)到群發(fā)郵件,請(qǐng)稍候",?FormFactory
?? ? ? ? ? ? ? ? ? ? ? ? ? ?.getInstance().getMailForm());
?? ? ? ? ? ? ? ? ? ?new?Thread()?{
?? ? ? ? ? ? ? ? ? ? ? ?public?void?run()?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ?String[][]?mailGroup?=?GetMailGroup();
?? ? ? ? ? ? ? ? ? ? ? ? ? ?flagGroup?=?1;
?? ? ? ? ? ? ? ? ? ? ? ? ? ?LoadDataAgain(ContainerGroup,?"您的群發(fā)郵件沒有數(shù)據(jù)!",
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mailGroup,?false);
?? ? ? ? ? ? ? ? ? ? ? ? ? ?FormFactory.getInstance().getMailForm().show();
?? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ?}.start();
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?if?(indexvalue?==?2?&&?flagSave?==?0)?{
?? ? ? ? ? ? ? ? ? ?Global.GetWaitCursor("正在轉(zhuǎn)到保留郵件,請(qǐng)稍候",?FormFactory
?? ? ? ? ? ? ? ? ? ? ? ? ? ?.getInstance().getMailForm());
?? ? ? ? ? ? ? ? ? ?new?Thread()?{
?? ? ? ? ? ? ? ? ? ? ? ?public?void?run()?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ?String[][]?mailSave?=?GetMailSave();
?? ? ? ? ? ? ? ? ? ? ? ? ? ?flagSave?=?1;
?? ? ? ? ? ? ? ? ? ? ? ? ? ?LoadDataAgain(ContainerSave,?"您的保留郵件沒有任何數(shù)據(jù)!",
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mailSave,?true);
?? ? ? ? ? ? ? ? ? ? ? ? ? ?FormFactory.getInstance().getMailForm().show();
?? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ?}.start();
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?if?(indexvalue?==?3?&&?flagSend?==?0)?{
?? ? ? ? ? ? ? ? ? ?Global.GetWaitCursor("正在轉(zhuǎn)到發(fā)件箱,請(qǐng)稍候",?FormFactory
?? ? ? ? ? ? ? ? ? ? ? ? ? ?.getInstance().getMailForm());
?? ? ? ? ? ? ? ? ? ?new?Thread()?{
?? ? ? ? ? ? ? ? ? ? ? ?public?void?run()?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ?String[][]?mailSend?=?GetMailSend();
?? ? ? ? ? ? ? ? ? ? ? ? ? ?flagSend?=?1;
?? ? ? ? ? ? ? ? ? ? ? ? ? ?LoadDataAgain(ContainerSend,?"您的發(fā)件箱沒有任何數(shù)據(jù)!",
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mailSend,?true);
?? ? ? ? ? ? ? ? ? ? ? ? ? ?FormFactory.getInstance().getMailForm().show();
?? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ?}.start();
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ?}
?? ? ? ?});
?? ? ? ?//將指定的Container添加上List
?? ?private?void?LoadDataAgain(Container?containner,?String?Labelstring,
?? ? ? ? ? ?String[][]?mail,?boolean?bolvalue)?{
?? ? ? ?MailInfo[]?mailInfo?=?GetMainInfoArrary(mail,?bolvalue);
?? ? ? ?containner.removeAll();
?? ? ? ?if?(mailInfo?!=?null)
?? ? ? ? ? ?containner.addComponent(createList(mailInfo,?List.VERTICAL,
?? ? ? ? ? ? ? ? ? ?new?ContactsRenderer()));
?? ? ? ?else?{
?? ? ? ? ? ?Label?label?=?new?Label(Labelstring);
?? ? ? ? ? ?containner.getStyle().setMargin(Component.TOP,?60);
?? ? ? ? ? ?containner.setLayout(new?FlowLayout(Component.CENTER));
?? ? ? ? ? ?containner.addComponent(label);
?? ? ? ?}
?? ?}
問題二:獲取選中的List列中的數(shù)據(jù),這是普遍會(huì)遇到的難題,如果你單單是用手機(jī)中間鍵觸發(fā)的話,或許比較簡(jiǎn)單,但是如果在菜單按鈕上也要增加此獲取list列表的數(shù)據(jù),就會(huì)比較麻煩,不過麻煩都過去了。
解決辦法:通過list的獲取焦點(diǎn)失去焦點(diǎn)還有選擇焦點(diǎn),設(shè)置全局變量來獲取list中綁定的屬性值。
?
?
?? ? ? ?//?list列表選擇焦點(diǎn)監(jiān)聽事件
?? ? ? ? ? ? ? ?list.addSelectionListener(new?SelectionListener()?{
?? ? ? ? ? ?public?void?selectionChanged(int?oldSelected,?int?newSelected)?{
?? ? ? ? ? ? ? ?globalStrMailGuid?=?MainInfos[newSelected].fid;
?? ? ? ? ? ?}
?? ? ? ?});
?? ? ? ?list.addFocusListener(new?FocusListener()?{
?? ? ? ? ? ?//?list獲得焦點(diǎn)
?? ? ? ? ? ?public?void?focusGained(Component?cmp)?{
?? ? ? ? ? ? ? ?globalStrMailGuid?=?MainInfos[list.getSelectedIndex()].fid;
?? ? ? ? ? ?}
?? ? ? ? ? ?//?list失去焦點(diǎn)
?? ? ? ? ? ?public?void?focusLost(Component?cmp)?{
?? ? ? ? ? ? ? ?globalStrMailGuid?=?"";
?? ? ? ? ? ?}
?? ? ? ?});
list.addActionListener(new?ActionListener()?{
?? ? ? ? ? ?public?void?actionPerformed(final?ActionEvent?evt)?{
?? ? ? ? ? ? ? ?//觸發(fā)列表中的列的時(shí)候你所要做的事情。。。
?? ? ? ? ? ?}
?? ? ? ?});
問題三:選中狀態(tài)的調(diào)整,如果沒有調(diào)整的話,你會(huì)發(fā)現(xiàn)list放到Tab后,選中狀態(tài)跟綁定的數(shù)據(jù)會(huì)慢了一拍,也就是你選在第二條而數(shù)據(jù)卻是第一條的。
解決辦法:設(shè)置一全局變量,getListFocusComponent做下更改即可,代碼如下。
?
?? ? ? ? ? ?//創(chuàng)建list中的getListFocusComponent中添加此if語句。 ? ?
?? ? ? ? ? ? ? ?public?Component?getListFocusComponent(final?List?list)?{
?? ? ? ? ? ?if?(globalselect?==?1)
?? ? ? ? ? ? ? ?return?focus;
?? ? ? ? ? ?else
?? ? ? ? ? ? ? ?return?null;
?? ? ? ?}
?? ? ? ? ? ? ? ?//也就是list獲得焦點(diǎn)的時(shí)候才返回focus選中狀態(tài),否則返回null
?? ? ? ?list.addFocusListener(new?FocusListener()?{
?? ? ? ? ? ?//?list獲得焦點(diǎn)
?? ? ? ? ? ?public?void?focusGained(Component?cmp)?{
?? ? ? ? ? ? ? ?globalselect?=?1;
?? ? ? ? ? ?}
?? ? ? ? ? ?//?list失去焦點(diǎn)
?? ? ? ? ? ?public?void?focusLost(Component?cmp)?{
?? ? ? ? ? ? ? ?globalselect?=?0;
?? ? ? ? ? ?}
?? ? ? ?});
這三點(diǎn)是比較大的問題所在,其他的還有一些可能會(huì)比較好解決就不列出來了。
如圖:
?? ? ? ? ??
?
問題一:首先我從登陸窗體進(jìn)入時(shí),我不希望每個(gè)Tab選項(xiàng)卡中的List中都去服務(wù)器取數(shù)據(jù),這樣會(huì)導(dǎo)致加載時(shí)間過長(zhǎng)導(dǎo)致用戶等待過久,因?yàn)門ab是在窗體初始化的時(shí)候就要把List中的數(shù)據(jù)添加進(jìn)去,如果要分別加載數(shù)據(jù)則在tab事件中必須重新加載之前的數(shù)據(jù)。
解決方法:此問題的關(guān)鍵是Container的使用,我們可以為每個(gè)選項(xiàng)卡添加一個(gè)Container,然后觸發(fā)tab事件時(shí)我們改變的只是Container中的數(shù)據(jù)及控件,所以這樣最完美了解決了存在的問題,創(chuàng)建list的代碼就不重復(fù)了,需要請(qǐng)看這里。
?
?? ? ? ?//?個(gè)人郵件
?? ? ? ?String[][]?mailPersonal?=?GetMailPersonal();
?? ? ? ?MailInfo[]?mailInfoPersonal?=?GetMainInfoArrary(mailPersonal,?true);
?? ? ? ?containerPersonal?=?new?Container();
?? ? ? ?containerPersonal.setLayout(new?BoxLayout(BoxLayout.Y_AXIS));
?? ? ? ?if?(mailInfoPersonal?!=?null)
?? ? ? ? ? ?containerPersonal.addComponent(createList(mailInfoPersonal,
?? ? ? ? ? ? ? ? ? ?List.VERTICAL,?new?ContactsRenderer()));
?? ? ? ?else?{
?? ? ? ? ? ?Label?label?=?new?Label("您的個(gè)人郵件沒有數(shù)據(jù)!");
?? ? ? ? ? ?containerPersonal.getStyle().setMargin(Component.TOP,?60);
?? ? ? ? ? ?containerPersonal.setLayout(new?FlowLayout(Component.CENTER));
?? ? ? ? ? ?containerPersonal.addComponent(label);
?? ? ? ?}
?? ? ? ?tab.addTab("個(gè)人郵件",?getRes().getImage("SJX.gif"),?containerPersonal);
?? ? ? ?//?群發(fā)郵件,未加載數(shù)據(jù)賦空數(shù)值
?? ? ? ?String[][]?mailGroup?=?{?{?"正在加載",?"",?"",?"",?"",?"",?""?}?};
?? ? ? ?MailInfo[]?mailInfoGroup?=?GetMainInfoArrary(mailGroup,?false);
?? ? ? ?final?Container?ContainerGroup?=?new?Container();
?? ? ? ?ContainerGroup.setLayout(new?BoxLayout(BoxLayout.Y_AXIS));
?? ? ? ?ContainerGroup.addComponent(createList(mailInfoGroup,?List.VERTICAL,
?? ? ? ? ? ? ? ?new?ContactsRenderer()));
?? ? ? ?tab.addTab("群發(fā)郵件",?getRes().getImage("SJX.gif"),?ContainerGroup);
?? ? ? ?//?保留郵件,未加載數(shù)據(jù)賦空數(shù)值
?? ? ? ?String[][]?mailSave?=?{?{?"正在加載",?"",?"",?"",?"",?"",?""?}?};
?? ? ? ?MailInfo[]?mailInfoSave?=?GetMainInfoArrary(mailSave,?true);
?? ? ? ?final?Container?ContainerSave?=?new?Container();
?? ? ? ?ContainerSave.setLayout(new?BoxLayout(BoxLayout.Y_AXIS));
?? ? ? ?ContainerSave.addComponent(createList(mailInfoSave,?List.VERTICAL,
?? ? ? ? ? ? ? ?new?ContactsRenderer()));
?? ? ? ?tab.addTab("保留郵件",?getRes().getImage("SJX.gif"),?ContainerSave);
?? ? ? ?//?發(fā)件箱,未加載數(shù)據(jù)賦空數(shù)值
?? ? ? ?String[][]?mailSend?=?{?{?"正在加載",?"",?"",?"",?"",?"",?""?}?};
?? ? ? ?MailInfo[]?mailInfoSend?=?GetMainInfoArrary(mailSend,?true);
?? ? ? ?final?Container?ContainerSend?=?new?Container();
?? ? ? ?ContainerSend.setLayout(new?BoxLayout(BoxLayout.Y_AXIS));
?? ? ? ?ContainerSend.addComponent(createList(mailInfoSend,?List.VERTICAL,
?? ? ? ? ? ? ? ?new?ContactsRenderer()));
?? ? ? ?tab.addTab("發(fā)件箱",?getRes().getImage("SJX.gif"),?ContainerSend);
?? ? ? ?tab.setTabPlacement(TabbedPane.TOP);
?? ? ? ?addComponent(BorderLayout.CENTER,?tab);
?? ? ? ?//?tab事件,觸發(fā)時(shí)請(qǐng)求數(shù)據(jù)并向Container添加已經(jīng)填充好數(shù)據(jù)的list
?? ? ? ?tab.addTabsListener(new?SelectionListener()?{
?? ? ? ? ? ?public?void?selectionChanged(int?oldSelected,?int?newSelected)?{
?? ? ? ? ? ? ? ?int?indexvalue?=?tab.getSelectedIndex();
?? ? ? ? ? ? ? ?if?(indexvalue?==?1?&&?flagGroup?==?0)?{
?? ? ? ? ? ? ? ? ? ?Global.GetWaitCursor("正在轉(zhuǎn)到群發(fā)郵件,請(qǐng)稍候",?FormFactory
?? ? ? ? ? ? ? ? ? ? ? ? ? ?.getInstance().getMailForm());
?? ? ? ? ? ? ? ? ? ?new?Thread()?{
?? ? ? ? ? ? ? ? ? ? ? ?public?void?run()?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ?String[][]?mailGroup?=?GetMailGroup();
?? ? ? ? ? ? ? ? ? ? ? ? ? ?flagGroup?=?1;
?? ? ? ? ? ? ? ? ? ? ? ? ? ?LoadDataAgain(ContainerGroup,?"您的群發(fā)郵件沒有數(shù)據(jù)!",
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mailGroup,?false);
?? ? ? ? ? ? ? ? ? ? ? ? ? ?FormFactory.getInstance().getMailForm().show();
?? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ?}.start();
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?if?(indexvalue?==?2?&&?flagSave?==?0)?{
?? ? ? ? ? ? ? ? ? ?Global.GetWaitCursor("正在轉(zhuǎn)到保留郵件,請(qǐng)稍候",?FormFactory
?? ? ? ? ? ? ? ? ? ? ? ? ? ?.getInstance().getMailForm());
?? ? ? ? ? ? ? ? ? ?new?Thread()?{
?? ? ? ? ? ? ? ? ? ? ? ?public?void?run()?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ?String[][]?mailSave?=?GetMailSave();
?? ? ? ? ? ? ? ? ? ? ? ? ? ?flagSave?=?1;
?? ? ? ? ? ? ? ? ? ? ? ? ? ?LoadDataAgain(ContainerSave,?"您的保留郵件沒有任何數(shù)據(jù)!",
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mailSave,?true);
?? ? ? ? ? ? ? ? ? ? ? ? ? ?FormFactory.getInstance().getMailForm().show();
?? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ?}.start();
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ?if?(indexvalue?==?3?&&?flagSend?==?0)?{
?? ? ? ? ? ? ? ? ? ?Global.GetWaitCursor("正在轉(zhuǎn)到發(fā)件箱,請(qǐng)稍候",?FormFactory
?? ? ? ? ? ? ? ? ? ? ? ? ? ?.getInstance().getMailForm());
?? ? ? ? ? ? ? ? ? ?new?Thread()?{
?? ? ? ? ? ? ? ? ? ? ? ?public?void?run()?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ?String[][]?mailSend?=?GetMailSend();
?? ? ? ? ? ? ? ? ? ? ? ? ? ?flagSend?=?1;
?? ? ? ? ? ? ? ? ? ? ? ? ? ?LoadDataAgain(ContainerSend,?"您的發(fā)件箱沒有任何數(shù)據(jù)!",
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mailSend,?true);
?? ? ? ? ? ? ? ? ? ? ? ? ? ?FormFactory.getInstance().getMailForm().show();
?? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ?}.start();
?? ? ? ? ? ? ? ?}
?? ? ? ? ? ?}
?? ? ? ?});
?? ? ? ?//將指定的Container添加上List
?? ?private?void?LoadDataAgain(Container?containner,?String?Labelstring,
?? ? ? ? ? ?String[][]?mail,?boolean?bolvalue)?{
?? ? ? ?MailInfo[]?mailInfo?=?GetMainInfoArrary(mail,?bolvalue);
?? ? ? ?containner.removeAll();
?? ? ? ?if?(mailInfo?!=?null)
?? ? ? ? ? ?containner.addComponent(createList(mailInfo,?List.VERTICAL,
?? ? ? ? ? ? ? ? ? ?new?ContactsRenderer()));
?? ? ? ?else?{
?? ? ? ? ? ?Label?label?=?new?Label(Labelstring);
?? ? ? ? ? ?containner.getStyle().setMargin(Component.TOP,?60);
?? ? ? ? ? ?containner.setLayout(new?FlowLayout(Component.CENTER));
?? ? ? ? ? ?containner.addComponent(label);
?? ? ? ?}
?? ?}
問題二:獲取選中的List列中的數(shù)據(jù),這是普遍會(huì)遇到的難題,如果你單單是用手機(jī)中間鍵觸發(fā)的話,或許比較簡(jiǎn)單,但是如果在菜單按鈕上也要增加此獲取list列表的數(shù)據(jù),就會(huì)比較麻煩,不過麻煩都過去了。
解決辦法:通過list的獲取焦點(diǎn)失去焦點(diǎn)還有選擇焦點(diǎn),設(shè)置全局變量來獲取list中綁定的屬性值。
?
?
?? ? ? ?//?list列表選擇焦點(diǎn)監(jiān)聽事件
?? ? ? ? ? ? ? ?list.addSelectionListener(new?SelectionListener()?{
?? ? ? ? ? ?public?void?selectionChanged(int?oldSelected,?int?newSelected)?{
?? ? ? ? ? ? ? ?globalStrMailGuid?=?MainInfos[newSelected].fid;
?? ? ? ? ? ?}
?? ? ? ?});
?? ? ? ?list.addFocusListener(new?FocusListener()?{
?? ? ? ? ? ?//?list獲得焦點(diǎn)
?? ? ? ? ? ?public?void?focusGained(Component?cmp)?{
?? ? ? ? ? ? ? ?globalStrMailGuid?=?MainInfos[list.getSelectedIndex()].fid;
?? ? ? ? ? ?}
?? ? ? ? ? ?//?list失去焦點(diǎn)
?? ? ? ? ? ?public?void?focusLost(Component?cmp)?{
?? ? ? ? ? ? ? ?globalStrMailGuid?=?"";
?? ? ? ? ? ?}
?? ? ? ?});
list.addActionListener(new?ActionListener()?{
?? ? ? ? ? ?public?void?actionPerformed(final?ActionEvent?evt)?{
?? ? ? ? ? ? ? ?//觸發(fā)列表中的列的時(shí)候你所要做的事情。。。
?? ? ? ? ? ?}
?? ? ? ?});
問題三:選中狀態(tài)的調(diào)整,如果沒有調(diào)整的話,你會(huì)發(fā)現(xiàn)list放到Tab后,選中狀態(tài)跟綁定的數(shù)據(jù)會(huì)慢了一拍,也就是你選在第二條而數(shù)據(jù)卻是第一條的。
解決辦法:設(shè)置一全局變量,getListFocusComponent做下更改即可,代碼如下。
?
?? ? ? ? ? ?//創(chuàng)建list中的getListFocusComponent中添加此if語句。 ? ?
?? ? ? ? ? ? ? ?public?Component?getListFocusComponent(final?List?list)?{
?? ? ? ? ? ?if?(globalselect?==?1)
?? ? ? ? ? ? ? ?return?focus;
?? ? ? ? ? ?else
?? ? ? ? ? ? ? ?return?null;
?? ? ? ?}
?? ? ? ? ? ? ? ?//也就是list獲得焦點(diǎn)的時(shí)候才返回focus選中狀態(tài),否則返回null
?? ? ? ?list.addFocusListener(new?FocusListener()?{
?? ? ? ? ? ?//?list獲得焦點(diǎn)
?? ? ? ? ? ?public?void?focusGained(Component?cmp)?{
?? ? ? ? ? ? ? ?globalselect?=?1;
?? ? ? ? ? ?}
?? ? ? ? ? ?//?list失去焦點(diǎn)
?? ? ? ? ? ?public?void?focusLost(Component?cmp)?{
?? ? ? ? ? ? ? ?globalselect?=?0;
?? ? ? ? ? ?}
?? ? ? ?});
這三點(diǎn)是比較大的問題所在,其他的還有一些可能會(huì)比較好解決就不列出來了。
轉(zhuǎn)載于:https://blog.51cto.com/zhaohaiyang/435619
總結(jié)
以上是生活随笔為你收集整理的tab与list配合使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站性能优化之应用程序缓存-初篇
- 下一篇: Events are a bad ide