Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have jFrame with someone Textfield who need to be cheked,and that information which you entered in JTextfield,need to save on someone txt.file when you press button,and when you press other button need to show that information whick you entered in JTF.I need y to pick up Jtextfield,save on txt.file and show that.

Java
import java.awt.BorderLayout;
import java.awt.Desktop.Action;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Prisustvo extends JFrame{
        JPanel prisustvo = new JPanel (new GridLayout(6, 7));
     JButton sacuvaj = new JButton("Upisi");
      JButton prikazi= new JButton("Prikazi");
      JButton predhodni = new JButton("Predhodni");
      JButton sledeci =new JButton("Sledeci");
      JTextField ime = new JTextField();
        JTextField prezime = new JTextField();
          JTextField indeks = new JTextField();
          String[] seminari = {"Microsoft","MAC","Java"};
JComboBox cek = new JComboBox(seminari);
    
    public Prisustvo(){
        prisustvo.add(new JLabel("Ime studenta:"));
         prisustvo.add(ime);
         prisustvo.add(new JLabel("Prezime studenta:"));
         prisustvo.add(prezime);
         prisustvo.add(new JLabel("Broj indeksa:"));
         prisustvo.add(indeks);
         prisustvo.add(cek);
        cek.addItemListener(new Cekbox());
         prisustvo.add(sacuvaj);
         sacuvaj.addActionListener(new Osluskivaci());
         prisustvo.add(prikazi);
         prikazi.addActionListener(new Osluskivaci());
        prisustvo.add(predhodni);
         predhodni.addActionListener(new Osluskivaci());
         prisustvo.add(sledeci);
         sledeci.addActionListener(new Osluskivaci());
       
        
               add(prisustvo,BorderLayout.CENTER);   
         }
    
    
  
         
         
         
        
    
    class Osluskivaci implements ActionListener{
        
 
        @Override
        public void actionPerformed(ActionEvent ae) {
           
        }
    }
    class Cekbox implements ItemListener{

        @Override
        public void itemStateChanged(ItemEvent ie) {
 
        }
        
    }
    
    

  
    public static void main(String[] args) {
       JFrame prisustvo = new Prisustvo();
       prisustvo.setSize(500, 500);
       prisustvo.setTitle("Prisustvo studenata seminarima");
       prisustvo.setLocationRelativeTo(null);
       prisustvo.setDefaultCloseOperation(EXIT_ON_CLOSE);
     prisustvo.pack();
     prisustvo.setVisible(true);

       
       
        
    }
    
}
Posted
Updated 2-Jun-14 1:15am
v2
Comments
[no name] 1-Jun-14 10:47am    
I think that you have mistaken the purpose of this site.
MarkoN90 1-Jun-14 11:35am    
i don'tno,but i realy need help.
[no name] 1-Jun-14 12:33pm    
You need to tell us a specific part you need help with. As it stands, this reads like "do my homework assignment for me". Especially when you have not presented any code that attempts to do what you say you want to do.

Looks good so far!

YOu added the actionlistener to all the components - was it supposed to be that way?

You can figure the actioncommand in the actionPerformed() and react to that.

How to Write an Action Listener[^]

Reading, Writing, and Creating Files[^]

Have fun!
 
Share this answer
 
To get Text from a JTextField, you can use the getText() method, and to write a file, you can use the BufferedWriter.

In the actionPerformed method,

we need to check which of the buttons have triggered the ActionEvent and to find that we could use the getSource method, after getting the button which triggered the event, we can use conditional statements to do appropriate work

So in general , the code in actionPerformed looks like this,

Java
if(ae.getSource() == button1)
{
    // code to write to file
}
else if(ae.getSource() == button2)
{
     // display contents of file in JTextField.
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900