Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
/**
* @author Darimont
*
*/
public class Test32 extends JFrame {
private JTree tree;
private DefaultTreeModel model;
private JTextField txtSearch;
private JButton btnSearch;
private String strSearch;
public Test32() {
super("Test32");
setDefaultCloseOperation(EXIT_ON_CLOSE);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
populateModel(root);
model = new DefaultTreeModel(root);
tree = new JTree(model);
getContentPane().add(new JScrollPane(tree), BorderLayout.NORTH);
txtSearch = new JTextField(15);
txtSearch.setText("b_3");
btnSearch = new JButton("go");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = txtSearch.getText().trim();
if (str.equals("") || str == null)
return;
if (find(str, tree.getModel())) {
System.out.println("found entry");
}
}
});
getContentPane().add(txtSearch, BorderLayout.WEST);
getContentPane().add(btnSearch, BorderLayout.EAST);
pack();
setVisible(true);
}
protected boolean find(String str, TreeModel model2) {
strSearch = str;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeWalkAndDoAction(
(DefaultMutableTreeNode) model.getRoot(), new ICommand() {
public Object execute(Object arg) {
if (arg.equals(strSearch))
return Boolean.TRUE;
return Boolean.FALSE;
}
});
if (node != null) {
TreePath path = new TreePath(((DefaultTreeModel) tree.getModel())
.getPathToRoot(node));
tree.setSelectionPath(path);
return true;
}
return false;
}
private TreeNode treeWalkAndDoAction(DefaultMutableTreeNode node, ICommand c) {
if (node.getChildCount() > 0) {
Enumeration e = node.children();
while (e.hasMoreElements()) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode) e
.nextElement();
TreeNode o = treeWalkAndDoAction(n, c);
if (o != null)
return o;
}
} else {
if (c.execute(node.getUserObject()).equals(Boolean.TRUE)) {
return node;
}
}
return null;
}
public void populateModel(DefaultMutableTreeNode root) {
DefaultMutableTreeNode node;
root.add(node = new DefaultMutableTreeNode("a"));
node.add(node = new DefaultMutableTreeNode("a_1"));
node.add(node = new DefaultMutableTreeNode("a_2"));
node.add(node = new DefaultMutableTreeNode("a_3"));
root.add(node = new DefaultMutableTreeNode("b"));
node.add(node = new DefaultMutableTreeNode("b_1"));
node.add(node = new DefaultMutableTreeNode("b_2"));
node.add(node = new DefaultMutableTreeNode("b_3"));
root.add(node = new DefaultMutableTreeNode("c"));
node.add(node = new DefaultMutableTreeNode("c_1"));
node.add(node = new DefaultMutableTreeNode("c_2"));
node.add(node = new DefaultMutableTreeNode("c_3"));
}
public static void main(String[] args) {
new Test32();
}
interface ICommand {
public Object execute(Object args);
}
}