M
M
myrkonianin2018-10-14 20:37:21
Java
myrkonianin, 2018-10-14 20:37:21

Boolean variable not being saved?

I decided to practice, write a notebook. I won't tell the story, because no one is interested.
What is the point: in the Page class, the boolean variable does not change. this is the first time I meet ... Help!

class Page: (where the error is)

import javax.swing.*;
import java.io.*;
public class Page{
  boolean isSaved;
  private String path = "";
  private File file;
  
  public Page(){
    isSaved = false ;
  }
  
  public void saveFile(JTabbedPane tabs){
       
    if(!isSaved){
      isSaved = true;
      JFileChooser fc = new JFileChooser();
      fc.showSaveDialog(null);
      file = fc.getSelectedFile();
      path = file.getAbsolutePath();
      Scroll s = (Scroll)tabs.getSelectedComponent();
      try{
        FileWriter fw = new FileWriter(file);
        fw.write(s.getText()); 
        fw.close();
      }catch(IOException ex){ex.printStackTrace();}
      isSaved = true;
      
    }else{
      Scroll s = (Scroll)tabs.getSelectedComponent();
      try{
        FileWriter fw = new FileWriter(file);
        fw.write(s.getText()); 
        fw.close();
      }catch(IOException ex){ex.printStackTrace();}
    }
  }
  
  public void clearSavePath(){
    isSaved = false;
    path = "";
  }
}


gui class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
class gui{
  private final JTabbedPane tabs;
  public JFrame win;
  public gui(){
   win = new JFrame("jmpad");
   win.setSize(800,600);
   win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   win.setLayout(new BorderLayout());
   
   JMenuBar menu = new JMenuBar();
   JMenu file = new JMenu("File...");
   JMenu tabsSettings = new JMenu("Tabs...");
   JMenuItem open = new JMenuItem("Open file...");
   JMenuItem save = new JMenuItem("Save file...");
   JMenuItem closeTab = new JMenuItem("Close tab");
   JMenuItem newTab = new JMenuItem("New tab");
   tabs = new JTabbedPane();
   
   file.add(open);
   file.add(save);
   tabsSettings.add(newTab);
   tabsSettings.add(closeTab);
   menu.add(file);
   menu.add(tabsSettings);
   win.setJMenuBar(menu);
   //menu events
   newTab.addActionListener(new ActionListener(){
     @Override
     public void actionPerformed(ActionEvent ae){
      JTextArea text = new JTextArea();
      Scroll scrl = new Scroll(text);
      tabs.addTab("new tab",scrl);
     }
   });
   
   open.addActionListener(new ActionListener(){
     @Override
     public void actionPerformed(ActionEvent ae){
      JFileChooser fc = new JFileChooser();
      fc.showOpenDialog(null);
      File file = fc.getSelectedFile();
      try{
        String input = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
        JTextArea text = new JTextArea(input);
        Scroll scrl = new Scroll(text);
        tabs.addTab(file.getName(),scrl);
      }catch(IOException ex){ex.printStackTrace();}
     }
   });
   
   save.addActionListener(new ActionListener(){
     @Override
     public void actionPerformed(ActionEvent ae){
       Page page = new Page();
       page.saveFile(tabs);
     }
   });
   //
   
   newTab();
   
   win.add(tabs);
  }
  
  public void newTab(){
    JTextArea text = new JTextArea();
    Scroll scrl = new Scroll(text);
    tabs.addTab("new tab",scrl);
  }
}


Well, I won't give the launcher class.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Iloveski, 2018-10-14
@myrkonianin

Maybe I'm stupid, but in my opinion the problem is that for each button click a new Page object is created

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question