Using xml to build menus in Swing

Source: Internet
Author: User


Generally, a menu bar is generated based on your needs. You can add the required menu items in each menu as needed. This process is clear, however, the Code looks messy and complicated.


Xml can well manage these entries. If you load the data in xml, convert the data from xml and load the data on the menu bar, it can also be easily modified and maintained in the future. In addition, we can easily find that the menus we need to build are similar to the xml format. Therefore, today I want to implement the idea of using xml to complete menu entries.


First, construct an xml file for a simple menu bar, as shown below:


<? Xml version = "1.0" encoding = "UTF-8"?>

<MenuBar>

<Menu name = "file" value = "file">

<MenuItem name = "new" value = "new">

</MenuItem>

</Menu>

<Menu name = "edit" value = "edit">

<MenuItem name = "delete" value = "delete">

</MenuItem>

</Menu>

</MenuBar>


In this xml file, there is a root node called MenuBar. Then there are two subnodes, file and edit, respectively. They both have the corresponding value of this name, these two are the menus to be added.


Under each menu, for example, under file, there are also menu items whose names are new and whose values are new, that is, the menu items to be loaded on the file menu.


Now the structure is clear, so the question is how should we implement these things using JMenuBar, JMenu, and JMenuItem?



First, let's analyze:


1. For each node, we find that there are two attributes: name and value, so we can consider using a ing to implement this structure;

2. For the relationship between the child node and the parent node of each node, and the number of child nodes in each Menu is different, you can use ArrayList to install the data.


Here, I simplified the data of each node so that the xml file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>

<MenuBar>

<Menu name = "file">

<MenuItem name = "new">

</MenuItem>

</Menu>

<Menu name = "edit">

<MenuItem name = "delete">

</MenuItem>

</Menu>

</MenuBar>


Each node uses a String to represent the data.


Each Menu and Its MenuItems are installed with an ArrayList, and the index of this ArrayList is 0 to store the Menu, and the rest stores its MenuItems in order.


At the same time, the number of menus is also unknown, so all menus are installed with an ArrayLisy.



The following describes the specific code implementation:


A. xml Loading



// Use map to record the attribute values of each node

// Use ArrayList to install each Menu and Its subnodes

// Use an array to load all arraylists.

Private File file;

Private SAXReader saxReader;

Private ArrayList <String> node;

Private ArrayList <String> nodes;

Public void read (String filePath)

{

File = new File (filePath );

SaxReader = new SAXReader (); // instantiate

Nodes = new ArrayList <String> ();

Try {

Document document = saxReader. read (file );

Element root = document. getRootElement ();

For (Iterator <Element> I = root. elementIterator (); I. hasNext ();)

{

Node = new ArrayList <String> ();

Element menu = I. next ();

Node. add (menu. attribute ("name"). getValue ());

For (Iterator <Element> j = menu. elementIterator (); j. hasNext ();)

{

Element menuItem = j. next ();

Node. add (menuItem. attribute ("name"). getValue ());

}

Nodes. add (node );

}

} Catch (incluentexception e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}


All xml data is stored in nodes in order.


B. Generation of MenuItem


For (ArrayList <String> node: nodes)

{

Menu = new JMenu (node. get (0 ));

For (int I = 1; I <node. size (); I ++)

{

System. out. println (node. get (I ));

MenuItem = new JMenuItem (node. get (I ));

Menu. add (menuItem );

}

JMenuBar. add (menu );

}



In this way, the remaining steps are similar, and this method has been implemented. If you have any problems with implementation, you can think carefully and wish you success.

This article is from the "12345" blog, please be sure to keep this source http://nicedone.blog.51cto.com/4955195/1290188

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.