Java Swing - NetBeans "lookalike" jtree function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Java Swing - NetBeans "lookalike" jtree function

Vesna
Hey,
I'm quite new at programming and I'm creating a programme with Java Swing. I have splitpane with a jtree on the left side and a tabbed pane on the right side.

The idea is that a user can create a project with default 3 empty txt files and place it to a computer directory. At the same time or when he clicks open file - it would display the project directory and the 3 txt files in a jtree (like NetBeans) if not already open. And again - the save button would save the project back in a chosen cumputer directory.

How can I do that?

Right now I have a default jtree with nodes I manually set and open and save method that are not connected to the jtree. There's a picture attached.

Thanks for any help :D

Here's the shrunk code:

public class Uiue extends javax.swing.JFrame {
   
    JTextField textField;
    JPanel nodeTab;
   
    public Uiue() {
        initComponents();
    }                    
 
    private void open(java.awt.event.ActionEvent evt) {                      
        JFileChooser jfc = new JFileChooser();
        int option = jfc.showOpenDialog(this);
        if (option == JFileChooser.ERROR_OPTION)
            Status.setText("Error opening file.");
        if (option == JFileChooser.CANCEL_OPTION)
            Status.setText("No file selected.");
        if (option == JFileChooser.APPROVE_OPTION) {
            File f = jfc.getSelectedFile();
            try {
//
//            } catch (IOException ex) {
//                
            } finally {Status.setText("Error reading file.");}
        }            
    }                    

    private void saveAs(java.awt.event.ActionEvent evt) {                        
        JFileChooser jfc = new JFileChooser();
        int option = jfc.showSaveDialog(this);
        if (option == JFileChooser.ERROR_OPTION)
            Status.setText("Error selecting file.");
        if (option == JFileChooser.CANCEL_OPTION)
            Status.setText("No file selected.");
        if (option == JFileChooser.APPROVE_OPTION) {
            File f = jfc.getSelectedFile();
            Status.setText("File " + f.getName() + " saved.");
            try {
//
//            } catch (IOException ex) {
//
            } finally {Status.setText("Error reading file.");}
        }
    }                                    

    private void newProjectClicked(java.awt.event.ActionEvent evt) {                                  
        String newProject = JOptionPane.showInputDialog(null, "Enter Projects Name:", "New Project", JOptionPane.OK_CANCEL_OPTION);
        DefaultTreeModel model = (DefaultTreeModel) Tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode newP = new DefaultMutableTreeNode(newProject);
        if (newProject.length() > 0){
            root.add(newP);
            DefaultMutableTreeNode newNodeH = new DefaultMutableTreeNode("FirstTxt");
            model.insertNodeInto(newNodeH, newP, newP.getChildCount());
            DefaultMutableTreeNode newNodeU = new DefaultMutableTreeNode("SecondTxt");
            model.insertNodeInto(newNodeU, newP, newP.getChildCount());
            DefaultMutableTreeNode newNodeN = new DefaultMutableTreeNode("ThirdTxt");
            model.insertNodeInto(newNodeN, newP, newP.getChildCount());
            model.reload();
            Status.setText("New Project successfully added.");
        } else {
            Status.setText("Action canceled.");
        }
    }                                  
   
    private void TreeMouseClicked(java.awt.event.MouseEvent evt) {       //opens a new tab in tabbedpane                  
        if(evt.getClickCount() == 2 && !evt.isConsumed()) {
            evt.consume();
            Status.setText("Clicks: " + evt.getClickCount());
            DefaultTreeModel model = (DefaultTreeModel) Tree.getModel();
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) Tree.getLastSelectedPathComponent();
            String nodeName = selectedNode.toString();
            DefaultMutableTreeNode previousNode = (DefaultMutableTreeNode) selectedNode.getParent();
            String projectName = previousNode.toString();
            if(previousNode != root){ //Klik na posamezno datoteko odpre samo to datoteko
                JTextField textField = new JTextField();
                Tabs.addTab(projectName + ": " + nodeName, textField);
                String content = "content of txt file"; //vsebina txt datoteke
                textField.setText(content);
            } else { //Klik na projekt odpre vse 3 datoteke
                return;
            }
        }    
    }                                
   
    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Uiue().setVisible(true);
            }
        });
    }
}




Attachments:
http://forums.netbeans.org//files/pic_123.png