Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Jcombobox And File Handling !

combobox

  • Please log in to reply
32 replies to this topic

#25 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 21 April 2012 - 10:44 PM

Another problem I have now!
I am using two Jframes one that has the combobox and the textarea which is used to display the texts of the corresponding combobox item and another frame which is used to add the new item to the combobox which has a textarea and the file/item name... the second frame creates a file by the given name... but the problem is how do i update the new file in the combobox of the first frame?
  • 0

#26 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 22 April 2012 - 03:08 AM

how do i update the new file in the combobox of the first frame

Is your problem getting a reference to the instance of the new class in the first class?

second frame creates a file

What do you mean by a "file"? Can you post the code that shows how the code is creating a file?
  • 0

#27 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 22 April 2012 - 03:22 AM

Frame 1:



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication7;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javaapplication7.JFt;
import javax.swing.JOptionPane;
/**
*
* @author number 1
*/
public class NewJFrame extends javax.swing.JFrame {

JFt jf = new JFt();
public void setEx(String fn){
FileReader fr = null;
try {
File f= new File("D:/jaydeep/"+fn+".txt");
fr = new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String temp="";
while(br.readLine()!=null)
temp=temp+"\n"+br.readLine();
jTextArea1.setText(temp);
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fr.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
public NewJFrame() {
initComponents();
}

public void addToList(String str){
jComboBox1.add(this, str);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();

jButton1.setText("jButton1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton2.setText("Add");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(81, 81, 81)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 395, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(101, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(182, 182, 182)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(73, 73, 73))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(61, 61, 61)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addGap(47, 47, 47)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(32, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
String a= (String) jComboBox1.getSelectedItem();
setEx(a);


}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

new JFt().setVisible(true);


}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.orac...dfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration


}



Frame 2:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication7;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javaapplication7.NewJFrame;
import javax.swing.JFrame;
/**
*
* @author number 1
*/
public class JFt extends javax.swing.JFrame {

public String getName(){
String x=jTextField1.getText();

return x;

}


public JFt() {
initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jInternalFrame1 = new javax.swing.JInternalFrame();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jInternalFrame1.setVisible(true);

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton1.setText("Add");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTextField1.setText("jTextField1");

jLabel1.setText("Test Name");

javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1))
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(82, 82, 82)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton1))
.addGap(0, 171, Short.MAX_VALUE)))
.addContainerGap())
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(32, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String filename= jTextField1.getText();
File f=new File("D:/jaydeep/"+ filename + ".txt");
FileWriter fw=new FileWriter(f);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(jTextArea1.getText());
System.out.println(jTextArea1.getText());
bw.close();
} catch (IOException ex) {
Logger.getLogger(JFt.class.getName()).log(Level.SEVERE, null, ex);
}

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.orac...dfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new JFt().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
  • 0

#28 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 22 April 2012 - 03:32 AM

Please edit your post and wrap the code in code tags to preserve its formatting. Unformatted code is hard to read and work with.
The <> icon above the input box will create the tags:
[code=auto:0]
<Your code here>
[/CODE ]
Without the space.
  • 0

#29 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 22 April 2012 - 04:10 AM

Frame 1:


[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]package javaapplication7;[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]import java.io.*;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]import java.util.logging.Level;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]import java.util.logging.Logger;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]import javax.swing.JFrame;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]import javaapplication7.JFt;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]import javax.swing.JOptionPane;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]/**[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]*[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]* @author number 1[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]*/[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]public class NewJFrame extends javax.swing.JFrame {[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]JFt jf = new JFt();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]public void setEx(String fn){[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]FileReader fr = null;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]try {[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]File f= new File("D:/jaydeep/"+fn+".txt");[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]fr = new FileReader(f);[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]BufferedReader br=new BufferedReader(fr);[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]String temp="";[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]while(br.readLine()!=null)[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]temp=temp+"\n"+br.readLine();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]jTextArea1.setText(temp);[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]} catch (IOException ex) {[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]} finally {[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]try {[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]fr.close();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]} catch (IOException ex) {[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]}[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]}[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]}[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]public NewJFrame() {[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]initComponents();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]}[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]public void addToList(String str){[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]jComboBox1.add(this, str);[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]}[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]@SuppressWarnings("unchecked")[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]// <editor-fold defaultstate="collapsed" desc="Generated Code"> [/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]private void initComponents() {[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]jButton1 = new javax.swing.JButton();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]jComboBox1 = new javax.swing.JComboBox();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]jScrollPane1 = new javax.swing.JScrollPane();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]jTextArea1 = new javax.swing.JTextArea();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]jButton2 = new javax.swing.JButton();[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]jButton1.setText("jButton1");[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) { [/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]String a= (String) jComboBox1.getSelectedItem();[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]setEx(a);[/size][/font][/color]


[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]} [/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { [/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]new JFt().setVisible(true);[/size][/font][/color]


[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]} [/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]/**[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]* @param args the command line arguments[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]*/[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]java.awt.EventQueue.invokeLater(new Runnable() {[/size][/font][/color]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]public void run() {[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]new NewJFrame().setVisible(true);[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]}[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]});[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]}[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]// Variables declaration - do not modify [/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]private javax.swing.JButton jButton1;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]private javax.swing.JButton jButton2;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]private javax.swing.JComboBox jComboBox1;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]private javax.swing.JScrollPane jScrollPane1;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]private javax.swing.JTextArea jTextArea1;[/size][/font][/color]
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]// End of variables declaration [/size][/font][/color]


[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3]}[/size][/font][/color]


Frame 2:

[code=auto:0]
package javaapplication7;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javaapplication7.NewJFrame;
import javax.swing.JFrame;
/**
*
* @author number 1
*/
public class JFt extends javax.swing.JFrame {

public String getName(){
String x=jTextField1.getText();

return x;

}


public JFt() {
initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jInternalFrame1 = new javax.swing.JInternalFrame();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jInternalFrame1.setVisible(true);

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton1.setText("Add");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});






private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String filename= jTextField1.getText();
File f=new File("D:/jaydeep/"+ filename + ".txt");
FileWriter fw=new FileWriter(f);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(jTextArea1.getText());
System.out.println(jTextArea1.getText());
bw.close();
} catch (IOException ex) {
Logger.getLogger(JFt.class.getName()).log(Level.SEVERE, null, ex);
}

}


java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new JFt().setVisible(true);
}
});
}

} [color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3][/CODE ]

  • 0

#30 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 22 April 2012 - 04:16 AM

I don't know what you did, but the posted code is not usable.
  • 0

#31 JayDeep Hora

JayDeep Hora

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 22 April 2012 - 04:20 AM

Frame 1:
package javaapplication7;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javaapplication7.JFt;
import javax.swing.JOptionPane;
/**
*
* @author number 1
*/
public class NewJFrame extends javax.swing.JFrame {

JFt jf = new JFt();
public void setEx(String fn){
FileReader fr = null;
try {
File f= new File("D:/jaydeep/"+fn+".txt");
fr = new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String temp="";
while(br.readLine()!=null)
temp=temp+"\n"+br.readLine();
jTextArea1.setText(temp);
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fr.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
public NewJFrame() {
initComponents();
}

public void addToList(String str){
jComboBox1.add(this, str);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jComboBox1 = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();

jButton1.setText("jButton1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton2.setText("Add");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});


pack();
}// </editor-fold>

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
String a= (String) jComboBox1.getSelectedItem();
setEx(a);


}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

new JFt().setVisible(true);


}


//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame().setVisible(true);
}
});
}




}




Frame 2:

[code=auto:0]
package javaapplication7;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javaapplication7.NewJFrame;
import javax.swing.JFrame;
/**
*
* @author number 1
*/
public class JFt extends javax.swing.JFrame {

public String getName(){
String x=jTextField1.getText();

return x;

}


public JFt() {
initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jInternalFrame1 = new javax.swing.JInternalFrame();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jInternalFrame1.setVisible(true);

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton1.setText("Add");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});






private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String filename= jTextField1.getText();
File f=new File("D:/jaydeep/"+ filename + ".txt");
FileWriter fw=new FileWriter(f);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(jTextArea1.getText());
System.out.println(jTextArea1.getText());
bw.close();
} catch (IOException ex) {
Logger.getLogger(JFt.class.getName()).log(Level.SEVERE, null, ex);
}

}


java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new JFt().setVisible(true);
}
});
}

} [color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3][/CODE ]


this is a rough code!
  • 0

#32 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 22 April 2012 - 04:22 AM

Your code tags are wrong. There should not be a space in the ending tag between the E and the ]
  • 0

#33 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 22 April 2012 - 07:54 AM

You might have better luck posting your code to http://pastebin.com/
The [code=auto:0] tag on the new IPB is pretty useless sometimes.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download