NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Java Swing - NetBeans "lookalike" jtree function

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
Vesna



Joined: 30 Jan 2017
Posts: 1

PostPosted: Mon Jan 30, 2017 10:05 am    Post subject: Java Swing - NetBeans "lookalike" jtree function Reply with quote

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 Very Happy

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);
}
});
}
}



pic.PNG
 Description:
 Filesize:  7.05 KB
 Viewed:  1888 Time(s)

pic.PNG


Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo