Hello Sir, I have a dropdown menu and i have to show textfields and submit button on the basis of the selection of option from the dropdown menu .acc. to my requirement i have to show the textfields and submit button in the same panel of the jframe window .
Below is my code which is displaying textfields in the other jframe. KIndly help me sir
Plz sir have a look of my code to sort out problem.
will always be gratefull.Thank you Sir.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.xml.soap.Detail;
public class ComboBox{
public static void main(String[] args) {
ComboBox b=new ComboBox();
}
public ComboBox(){
final JComboBox combo;
JTextField txt;
JButton b=new JButton("GO");
String course[] = {"Detail Report","Summary Report"};
final JFrame frame = new JFrame("Creating a JComboBox Component");
final JPanel panel = new JPanel();
combo = new JComboBox(course);
combo.setBackground(Color.white);
combo.setForeground(Color.red);
panel.add(combo);
panel.add(b);
frame.add(panel);
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
String str = (String)combo.getSelectedItem();
}
});
combo.setBounds(10,10,100,20);
b.setBounds(120,40,80,20);
panel.setBounds(10,70,480,170);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel p=new JPanel();
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXITONCLOSE);
f.setSize(450,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
String str3 = (String)combo.getSelectedItem();
String str2="Detail Report";
System.out.println(str3);
if(str3.equals(str2)){
JLabel l1=new JLabel("Date1:");
JTextField txt1=new JTextField(12);
p.add(l1);
p.add(txt1);
f.add(p);
f.setVisible(true);
}
else{
JLabel l2=new JLabel("Date2:");
JTextField txt2=new JTextField(12);
p.add(l2);
p.add(txt2);
f.add(p);
f.setVisible(true);
}
}
});
frame.setDefaultCloseOperation(JFrame.EXITONCLOSE);
frame.setSize(450,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Here is a code that open textfields in the same jframe by selcting from dropdown menu option.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.xml.soap.Detail;
public class GenerateReport{
public static void main(String[] args) {
GenerateReport b=new GenerateReport();
}
public GenerateReport(){
final JComboBox combo;
JTextField txt;
JButton b=new JButton("GO");
String course[] = {"Detail Report","Summary Report"};
JFrame frame = new JFrame("Creating a JComboBox Component");
frame.setLayout(null);
combo = new JComboBox(course);
combo.setBackground(Color.white);
combo.setForeground(Color.red);
final JLabel l1=new JLabel("Date1:");
final JTextField txt1=new JTextField(12);
final JLabel l2=new JLabel("Date2:");
final JTextField txt2=new JTextField(12);
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
String str = (String)combo.getSelectedItem();
}
});
combo.setBounds(10,10,100,20);
b.setBounds(10,40,80,20);
l1.setBounds(10,70,100,20);
txt1.setBounds(120,70,100,20);
l2.setBounds(10,110,100,20);
txt2.setBounds(120,110,100,20);
frame.add(combo);
frame.add(b);
frame.add(l1);
frame.add(txt1);
frame.add(l2);
frame.add(txt2);
l1.setVisible(false);
txt1.setVisible(false);
l2.setVisible(false);
txt2.setVisible(false);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str3 = (String)combo.getSelectedItem();
String str2="Detail Report";
System.out.println(str3);
if(str3.equals(str2)){
l1.setVisible(true);
txt1.setVisible(true);
l2.setVisible(false);
txt2.setVisible(false);
}
else{
l1.setVisible(false);
txt1.setVisible(false);
l2.setVisible(true);
txt2.setVisible(true);
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Thank you deepak .Thank you very much......