henterji's blog

  • 2006-09-29

    Java swing tutorial Website


    Tag:Java GUI
    henterji 发表于12:21:00 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-07-28

    Swing and Roundabouts 4: Grid Bag Grease


    Tag:Java GUI
    henterji 发表于09:41:21 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-07-11

    JCheckBoxMenuItem & JRadioButtonMenuItem


    Not all menu items are boring. Some have a check mark to indicatethat the menu item has been selected...

    ...while others have a small black dot that performs the same function as the check mark:

    The check mark is provided by the JCheckBoxMenuItem class, while the dot is there thanks to the JRadioButtonMenuItem class.

    To add JCheckBoxMenuItems and JRadioButtonMenuItems to NetBeansmodules or to applications based on the NetBeans Platform, implementthe CallableSystemAction class (via the New Action wizard) and then override the getMenuPresenter()method. The cool thing is not so much the visual feedback via the checkmark or dot, but the fact that you can make things happen depending onthe state of the menu item (i.e., either selected or unselected).

     

    For example, heres my class that extends CallableSystemAction, implemented for JCheckBoxMenuItem, but with comments explaining how to change the imlementation to make it work for JRadioButtonMenuItem:

    public class MyAction extends CallableSystemAction {

    private static JCheckBoxMenuItem abc;
    //private static JRadioButtonMenuItem abc;
    private static final String ICON_PATH = "org/netbeans/mdoules/checkboxitemsample/HOMER16.png";
    private ImageIcon ICON = new ImageIcon(Utilities.loadImage(MyAction.ICON_PATH, true));

    public JMenuItem getMenuPresenter() {

    //Use the following line for JCheckBoxMenuItem:
    abc = new JCheckBoxMenuItem("Open Homer Window", null);

    //Use the following line for JRadioButtonMenuItem:
    //abc = new JRadioButtonMenuItem("Open Homer Window", null);

    //Use the following line for JCheckBoxMenuItem:
    abc.setState(true);

    //Use the following line for JRadioButtonMenuItem:
    //abc.setSelected(true);

    abc.setIcon(ICON);

    //Here we listen and then act:
    abc.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    TopComponent tc = MyTopComponent.findInstance().getDefault();
    if(abc.isSelected()) {
    if (!tc.isOpened()) {
    tc.open();
    }
    } else {
    if (tc.isOpened()) {
    tc.close();
    }
    tc.requestActive();
    }
    }
    });
    return abc;
    }

    //Required by CallableSystemAction:
    public void performAction() {
    }

    //Required by CallableSystemAction:
    public String getName() {
    return null;
    }

    //Required by CallableSystemAction:
    public HelpCtx getHelpCtx() {
    return null;
    }

    }

    And thats it. The important thing to note is that all of the CallableSystemAction methods are meaningless to us; everything has to be defined within the getMenuPresenter()method that overrides the default way that a menu item is implementedin NetBeans IDE (i.e., as a boring menu item). Now, when the menu itemis selected, the window (which extends the TopComponentclass, which can also be generated from a wizard in NetBeans IDE) opensand when it is unselected, the window closes. Could come in prettyhandy sometimes.

     

    Instead of overriding getMenuPresenter(), you could also override getPopupPresenter() or getToolbarPresenter(). If youre interested in the latter, see the NetBeans Google Toolbar Module Tutorial.

    In the comments at the end of yesterdays blog entry, Sandipreferred to the Radio Buttons section in the Java Look and Feel Design Guidelines (which I didnt know about but am finding very useful). Sandips point was that "the JRadioButtonMenuItem or JRadioButtonare meant to be used in cases where it represents an exclusive choicewithin a set of related choices", while my example yesterday didntrepresent this kind of choice (there was only one choice, which isntreally a choice). "So," I thought, "How would I go about implementingan exclusive choice (to justify not using Sandips approach)?" Thething to use is a ButtonGroup,which is simple to implement in Matisse—just drag and drop one ontoyour form. But what about when were not designing graphically, butdoing everything in the code? And, secondly, wed need two or more menuitems, all subclassing CallableSystemAction. How are the JRadioButtonMenuItems in these separate classes going to share the same ButtonGroup? So, I now have this simple helper class:
    public class ButtonGroupHelper {

    public static ButtonGroup bg = new ButtonGroup();

    /** Creates a new instance of ButtonGroupHelper */
    public ButtonGroupHelper() {
    }

    /** Returns a ButtonGroup */
    public static ButtonGroup returnGroup() {
    return bg;
    }

    }

    And, within the first of the two classes that subclass CallableSystemAction, I have this bit of code...

    comedyChoice = new JRadioButtonMenuItem("Comedy", null);
    ButtonGroup local = ButtonGroupHelper.returnGroup();
    local.add(comedyChoice);

    ...while the second (and any subsequent classes) have the same:

    romanceChoice = new JRadioButtonMenuItem("Romance", null);
    ButtonGroup local = ButtonGroupHelper.returnGroup();
    local.add(romanceChoice);

    This lets me retrieve the same button group for each JRadioButtonMenuItem.And, this lets me implement "an exclusive choice within a set ofrelated choices". When I select "Comedy" below, "Romance" isautomatically unselected, and vice versa:

    Ive set it up so that when a choice is made, a related windowis opened and, if the other window is open at the same time, it isclosed. This means either the "Comedy" or the "Romance" window is open,but never both at the same time.

    Tag:Java GUI
    henterji 发表于00:00:00 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-06-29

    Make JComboBox popup wide enough


    Tag:Java GUI
    henterji 发表于11:47:32 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-06-13

    TIP: Wrap a Swing JComponent in a background image


    Tag:Java GUI
    henterji 发表于09:33:11 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-05-26

    Dynamic interface design with Swing


    Tag:GUI Java
    henterji 发表于12:17:56 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-05-25

    Ease of Swing Development - Beans Binding


    Tag:Java GUI
    henterji 发表于09:34:57 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-05-19

    Do's and Dont's for Swing Applications


    Tag:GUI Java
    henterji 发表于09:20:08 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-05-16

    java实现圆角带标题的Border


    Tag:Java GUI
    henterji 发表于14:53:31 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-05-15

    使JSplitPane一开始就只显示一半,而另一半不显示


    Tag:Java GUI
    henterji 发表于13:17:19 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-05-10

    一个比较完善的透明窗口


    Tag:Java GUI
    henterji 发表于12:04:54 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-05-10

    透明窗口


    Tag:Java GUI
    henterji 发表于11:07:19 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-05-08

    Using JPopupMenu in TrayIcon


    Tag:GUI Java
    henterji 发表于09:46:26 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-04-29

    Swing 中设置模态窗体和启动位置


    Tag:Java GUI
    henterji 发表于09:42:05 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-04-24

    DNA Waiting Dialog (Swing Demo)


    Tag:Java GUI
    henterji 发表于20:04:57 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-04-21

    java写的一个font chooser


    Tag:GUI Java
    henterji 发表于17:36:12 | 阅读全文 | 评论 1 | 编辑 | 分享 0
  • 2006-04-20

    Bringing Swing to the Web


    Tag:Java GUI
    henterji 发表于19:42:09 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-04-20

    解决JComboBox重复元素的getSelectIndex()的bug


    Tag:Java GUI
    henterji 发表于15:20:04 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-04-18

    Stuff to Play With: the EnumComboBoxModel


    Tag:GUI Java
    henterji 发表于09:27:54 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-04-17

    JTextArea的一个bug


    Tag:GUI Java
    henterji 发表于16:23:32 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-03-29

    多行的menu


    Tag:GUI Java
    henterji 发表于16:17:59 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-03-27

    Style Swing components using CSS


    Tag:GUI Java
    henterji 发表于09:59:25 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-03-24

    MultiSplitPane: Splitting Without Nesting


    Tag:Java GUI
    henterji 发表于13:03:00 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-03-22

    Text类控件中带有链接,并启动事件处理


    Tag:Java GUI
    henterji 发表于09:57:26 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-03-20

    SWT: Tray Icons and Tooltips


    Tag:Java GUI eclipse
    henterji 发表于09:30:20 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-03-08

    Synth: How to skin a JScrollBar


    Tag:Java GUI
    henterji 发表于17:08:23 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-03-01

    分组表头的实现


    Tag:Java GUI
    henterji 发表于17:56:53 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-03-01

    [原创]基于java的toolbar按钮旁附带一个下拉菜单


    Tag:Java GUI
    henterji 发表于17:07:19 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-02-28

    A Reusable BuddyList Component


    Tag:GUI Java
    henterji 发表于17:10:55 | 阅读全文 | 评论 0 | 编辑 | 分享 0
  • 2006-02-20

    java中鼠标拖动没有title的窗口


    Tag:Java GUI
    henterji 发表于13:14:52 | 阅读全文 | 评论 0 | 编辑 | 分享 0
共3页 1 2 3 下一页 最后一页

个人资料

henterji

日历

搜索

管理

  • 进入后台 写新日志
  • 文章管理 评论管理
  • 更换模板 访问统计

文章分类

    Tag

    • Java[143]
    • GUI[70]
    • eclipse[10]
    • Windows[10]
    • ENGLISH[7]
    More..

    最新文章

    • Java swing tutorial Website
    • 在线Web IM 工具列表
    • 如何获得全文文献
    • Ubuntu安装后root的密码是什么?
    • Building cheat sheets in Eclipse V3.2
    • How to encode Enums?
    • 计算Java日期
    • Remove Duplicates from a List
    • 测试blog价值(娱乐)
    • Swing and Roundabouts 4: Grid Bag Grease
    全部日志>>

    最新评论

    • 兜兜:这个程序最后的结果是不是动画显示的,不是的话!能进一步做...
    • 梁达劲:我看得不是很明白,请问root的密码的多少??...
    • a dating info:Nice world <a href="...
    • j dating info:Nice world <a href="...
    • i dating info:Nice world <a href="...
    • h dating info:My favorites <a href=&quo...
    • kekee:佩服
    • pop:very good!!
    • ksdmaj kmsghwoxc:cldrp zgrb evmxh lnfvk mwpck...
    • xsqkyjg zmveuwa:qgnpeoyiu rtqnscmp qnxayh ur...

    链接

      存档

      • 2006 [181]
      • 2005 [190]
      自定义HTML代码区域
      • 访问统计:
      • RSS 什么是RSS?
        用IM提醒我内容更新
        订阅到QQ邮箱
        订阅到鲜果阅读器
        订阅到Google阅读器
        订阅到抓虾阅读器
      • 《城客》第四期:创意之城
        博客大巴
        博客大巴使用指南
        博客大巴模板中心
        免费注册博客大巴
        一键博客搬家工具
        中文互动杂志城客
      Copyright © 2002-2009 BlogBus.com, All Rights Reserved. 博客大巴 版权所有
      博客大巴模板设计:新空气之旅 | 作者:enid | 素材来源:香必飘新空气之旅