10. december 2002 - 23:05Der er
6 kommentarer og 1 løsning
Hvordan tilføjer man en actionListener på en JTree
Hej med jer... Hvordan kan man indsætte en actionListener på en menupunkt på en JTree? Det er endnu et enkelt eksempel på en JTree, men skal senere udvikles så man kan skifte mellem forskellige JPanels ved at trykke på menupunkterne i JTree'et!
Håber én kan hjælpe mig!
import java.awt.*; import javax.swing.*; import javax.swing.tree.*; public class TreeFrame {
public TreeFrame() { JFrame frame = new JFrame("Tree example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MutableTreeNode top = createTree(); JTree tree = new JTree(top); tree.setRootVisible(true); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
top = new DefaultMutableTreeNode("Forum"); treeModel = new DefaultTreeModel(top); //treeModel.addTreeModelListener(new MyTreeModelListener());
tree = new JTree(treeModel); //JTreeUpdater updater = new JTreeUpdater(tree); tree.setEditable(false); tree.setRootVisible(true); tree.setShowsRootHandles(true); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); renderer.setOpenIcon(null); renderer.setClosedIcon(null); renderer.setLeafIcon(null); tree.setCellRenderer(renderer);
nyTraad = new JButton("Ny Tråd"); nyTraad.setBounds(100,5,110,20); nyTraad.addActionListener(this); panel.add(nyTraad);
svarTraad = new JButton("Svar på Tråd"); svarTraad.setBounds(325,5,110,20); svarTraad.addActionListener(this); panel.add(svarTraad);
scroller = new JScrollPane(tree); //scroller.setOpaque(true); //tree.setBackground(new Color(217,219,226)); scroller.setBounds(10,30,530,200); panel.add(scroller);
felt = new JTextPane(); felt.setEditable(false); feltScroller = new JScrollPane(felt); feltScroller.setBounds(10,240,530,155); // felt.setBackground(new Color(217,219,226)); panel.add(feltScroller);
public void actionPerformed(ActionEvent e) { if(e.getSource() == nyTraad) { traadText = new JTextArea(5,5); feltScroller = new JScrollPane(traadText,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); navn = new JTextField(); email = new JTextField(); emne = new JTextField();
traadText = new JTextArea(5,5); feltScroller = new JScrollPane(traadText,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); navn = new JTextField(); email = new JTextField(); emne = new JTextField();
int i = 0; while(row.next()) { int id = row.getInt("id"); int overId = row.getInt("parentID"); java.sql.Date tid = row.getDate("tid"); String navn = row.getString("name"); String email = row.getString("email"); String ip = row.getString("ip"); String traad = row.getString("subject"); String besked = row.getString("message");
DefaultMutableTreeNode svar = new DefaultMutableTreeNode(traad);
LOL Aslan, jeg tror ikke du overholder min javalærers påbud om at en javaklasse max må fylde en A4 side, hehe.... Godt at se man ikke er den eneste der laver lange klasser :-)
public class TreeDemo extends JFrame { private JEditorPane htmlPane; private static boolean DEBUG = false; private URL helpURL;
//Optionally play with line styles. Possible values are //"Angled", "Horizontal", and "None" (the default). private boolean playWithLineStyle = false; private String lineStyle = "Angled";
public TreeDemo() { super("TreeDemo");
//Create the nodes. DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series"); createNodes(top);
//Create a tree that allows one selection at a time. final JTree tree = new JTree(top); tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION);
//Listen for when the selection changes. tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node == null) return;
Object nodeInfo = node.getUserObject(); if (node.isLeaf()) { BookInfo book = (BookInfo)nodeInfo; displayURL(book.bookURL); if (DEBUG) { System.out.print(book.bookURL + ": \n "); } } else { displayURL(helpURL); } if (DEBUG) { System.out.println(nodeInfo.toString()); } } });
if (playWithLineStyle) { tree.putClientProperty("JTree.lineStyle", lineStyle); }
//Create the scroll pane and add the tree to it. JScrollPane treeView = new JScrollPane(tree);
//Create the HTML viewing pane. htmlPane = new JEditorPane(); htmlPane.setEditable(false); initHelp(); JScrollPane htmlView = new JScrollPane(htmlPane);
//Add the scroll panes to a split pane. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setTopComponent(treeView); splitPane.setBottomComponent(htmlView);
Dimension minimumSize = new Dimension(100, 50); htmlView.setMinimumSize(minimumSize); treeView.setMinimumSize(minimumSize); splitPane.setDividerLocation(100);
category = new DefaultMutableTreeNode("Books for Java Programmers"); top.add(category);
//original Tutorial book = new DefaultMutableTreeNode(new BookInfo ("The Java Tutorial: Object-Oriented Programming for the Internet", "tutorial.html")); category.add(book);
//Tutorial Continued book = new DefaultMutableTreeNode(new BookInfo ("The Java Tutorial Continued: The Rest of the JDK", "tutorialcont.html")); category.add(book);
//JFC Swing Tutorial book = new DefaultMutableTreeNode(new BookInfo ("The JFC Swing Tutorial: A Guide to Constructing GUIs", "swingtutorial.html")); category.add(book);
//Arnold/Gosling book = new DefaultMutableTreeNode(new BookInfo ("The Java Programming Language", "arnold.html")); category.add(book);
//FAQ book = new DefaultMutableTreeNode(new BookInfo( "The Java FAQ", "faq.html")); category.add(book);
//Chan/Lee book = new DefaultMutableTreeNode(new BookInfo ("The Java Class Libraries: An Annotated Reference", "chanlee.html")); category.add(book);
//Threads book = new DefaultMutableTreeNode(new BookInfo ("Concurrent Programming in Java: Design Principles and Patterns", "thread.html")); category.add(book);
category = new DefaultMutableTreeNode("Books for Java Implementers"); top.add(category);
//VM book = new DefaultMutableTreeNode(new BookInfo ("The Java Virtual Machine Specification", "vm.html")); category.add(book);
//Language Spec book = new DefaultMutableTreeNode(new BookInfo ("The Java Language Specification", "jls.html")); category.add(book); }
public static void main(String[] args) { JFrame frame = new TreeDemo();
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.