Click to See Complete Forum and Search --> : sending combobox data to another frame
insane86
April 22nd, 2009, 07:12 AM
hey all..i've two different frames, a combo box and a text box..i add some string values to that combo box by using combo.add()
i want to send the selected item of the combo box to another frame..but i'm having trouble while sending the selected item : no matter what i choose from combo box, it always returns the first item ("a")..
here are some code segments i used in my program :
FirstFrame.java
public FirstFrame() {
initComponents();
cmb.addItem("a");
cmb.addItem("b");
cmb.addItem("c");
public String getItem() {
String item=(String)cmb.getSelectedItem();
System.out.println(item);
return(item);
}
------------------------------------------
SecondFrame.java
FirstFrame frm1=new FirstFrame();
public SecondFrame() {
initComponents();
textbox.setText(frm1.getItem());
}
thanx a lot..
keang
April 22nd, 2009, 07:31 AM
but i'm having trouble while sending the selected item : no matter what i choose from combo box, it always returns the first item ("a")..
You haven't shown the code that is supposed to do this, all you've shown is the initialisation code.
Please use code tags when posting code.
insane86
April 23rd, 2009, 04:10 AM
ok then..entire code of my simple program is below..
i've two classes (FirstFrame.java and SecondFrame.java) in seperate files, a button, a combo box and a textbox in my program..thanks..
FirstFrame.java
---------------------------------------
import javax.swing.*;
public class FirstFrame extends javax.swing.JFrame {
public FirstFrame() {
initComponents();
cmb.addItem("a");
cmb.addItem("b");
cmb.addItem("c");
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnsend = new javax.swing.JButton();
cmb = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnsend.setText("Send to other class");
btnsend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsendActionPerformed(evt);
}
});
cmb.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
cmbİtemStateChanged(evt);
}
});
cmb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmbActionPerformed(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(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(list, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(cmb, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnsend)))
.addContainerGap(180, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnsend))
.addGap(38, 38, 38)
.addComponent(list, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(46, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnsendActionPerformed(java.awt.event.ActionEvent evt) {
new SecondFrame().setVisible(true);
}
/**
* @param args the command line arguments
*/
public String getItem() {
String item=(String)cmb.getSelectedItem();
System.out.println(item);
return(item);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FirstFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnsend;
public javax.swing.JComboBox cmb;
// End of variables declaration
}
SecondFrame.java
---------------------------------------
public class SecondFrame extends javax.swing.JFrame {
FirstFrame frm1=new FirstFrame();
public SecondFrame() {
initComponents();
txt.setText(frm1.getItem());
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
txt = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
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(31, 31, 31)
.addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(219, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(246, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SecondFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JTextField txt;
// End of variables declaration
}
dlorde
April 23rd, 2009, 06:29 AM
When posting code, please use CODE tags so the formatting is retained and it stays readable.
Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman
insane86
April 24th, 2009, 02:24 AM
what do you mean by using code tags, how can i put them, i'm a newbie at this forum..the program is so simple that when you copy and paste it into an editor program, it has nothing difficult to understand..
dlorde
April 24th, 2009, 08:02 AM
what do you mean by using code tags, how can i put them, i'm a newbie at this forum..If you read the 'Announcement: Before you post...' link at the top of the topic list, you'll see what is expected of posters to this forum - not least, readable code.
the program is so simple that when you copy and paste it into an editor program, it has nothing difficult to understand..Most here would prefer to read it in-situ first, particularly if they're browsing with a portable device. But hey, if it's too much effort, we can just move on...
Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman
keang
April 24th, 2009, 11:06 AM
it has nothing difficult to understand..mmm so why can't you get it to work then if it's so easy to understand ;)
It always easy to understand code you have written. It's also relatively easy to understand code someone else has written provided that it is properly indented, commented and conforms to Java standards. It's not so easy to understand the mass or text that you have posted. Yes we could, given a bit of time, work out what it does and where the problem is but why should we waste our valuable time when we can spend it helping others/doing something more interesting?
insane86
April 27th, 2009, 02:59 AM
wow..it's impressing to see that there are so many smart-asses in this forum, so it'd be better for me to ask my question to members of another forum who are willing to help..thank you guys, you gave me the lesson of my life.. :P
dlorde
April 27th, 2009, 05:54 AM
<shrug> All we want is readable code. You're the one asking for help, it's your choice.
A prudent question is one-half of wisdom...
F. Bacon
Martin O
April 27th, 2009, 10:41 AM
wow..it's impressing to see that there are so many smart-asses in this forum, so it'd be better for me to ask my question to members of another forum who are willing to help..thank you guys, you gave me the lesson of my life.. :P
If using code tags on a programming forum is too difficult for you....then good luck with Java, you'll need it! :rolleyes:
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.