JCombo Box with Keypressed
Hi ,
Here i have pasted my code, Is it possible to Create and a combo box by pressing the keypressed event
package swing;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import javax.swing.JTextField;
publicclass Key3extends JPanel{
privatestaticfinallong serialVersionUID = 1;
private JPanel jPanel =null;
private JTextField jTextField =null;
/**
* This is the default constructor
*/
public Key3(){
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
privatevoid initialize(){
this.setSize(300, 200);
this.setLayout(null);
this.add(getJPanel(),null);
this.add(getJTextField(),null);
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel(){
if (jPanel ==null){
jPanel =new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(149, 99, 1, 1));
}
return jPanel;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField(){
if (jTextField ==null){
jTextField =new JTextField();
jTextField.setBounds(new Rectangle(56, 34, 170, 42));
jTextField.addKeyListener(new java.awt.event.KeyAdapter(){
publicvoid keyPressed(java.awt.event.KeyEvent e){
System.out.println("keyPressed()");// TODO Auto-generated Event stub keyPressed()
[b]// Here i want to display the combobox [/b]
}
});
}
return jTextField;
}
}
# 1
instantiate a new jcombobox and add it to the container.
# 2
I tried like that, but am facing issue while creating the Jcombo box by keypressed even which i have marked separately , any one can please help me
package swing;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JComboBox;
public class Key3 extends JPanel {
private static final long serialVersionUID = 1;
private JPanel jPanel = null;
private JTextField jTextField = null;
//private JComboBox jComboBox = null;
/**
* This is the default constructor
*/
public Key3() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setLayout(null);
this.add(getJPanel(), null);
this.add(getJTextField(), null);
//this.add(getJComboBox(), null);
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(149, 99, 1, 1));
}
return jPanel;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(56, 34, 170, 42));
jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
System.out.println("keyPressed()"); // TODO Auto-generated Event stub keyPressed()
//////////////// //////////////// //////////////// //////////////// //////////////// //////////////// ////////////////
JComboBox jComboBox = null;
add(getJComboBox(), null);
JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
jComboBox.setBounds(new Rectangle(83, 119, 149, 34));
}
return jComboBox;
}
//////////////// //////////////// //////////////// //////////////// //////////////// //////////////// ////////////////
}
});
}
return jTextField;
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
}
# 3
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(56, 34, 170, 42));
jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
System.out.println("keyPressed()"); // TODO Auto-generated Event stub keyPressed()
JComboBox jComboBox = null;
add(getJComboBox(), null);
JComboBox getJComboBox() { // you are adding a method inside a method
if (jComboBox == null) {// it would generate an error
jComboBox = new JComboBox();
jComboBox.setBounds(new Rectangle(83, 119, 149, 34));
}
return jComboBox;
}
}
});
}
return jTextField;
}
Message was edited by:
Yannix
# 4
How can i Solve this issue ?
# 5
@OP: copy and paste, have a nice day
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class sample1 extends JPanel {
private static final long serialVersionUID = 1;
private JTextField jTextField = null;
private JComboBox jCombo;
private int y = 100;
public sample1() {
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setLayout(null);
this.add(getJTextField());
setBackground(Color.YELLOW);
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(56, 34, 170, 42));
jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
System.out.println("keyPressed()");
addComboBox();
}
});
}
return jTextField;
}
private void addComboBox(){
jCombo = new JComboBox();
jCombo.addItem("Help Pls.");
jCombo.setBounds(10, y, 100, 20);
add(jCombo);
revalidate();
repaint();
y += 20;
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(400,400);
frame.add(new sample1(),BorderLayout.CENTER);
frame.setVisible(true);
}
}
Message was edited by:
Yannix
