Array assignment issue

[nobr]Hi ,

Am trying to assign the array values in side a method , but its betting error

publicfinal String listData[] =new String[7];

- its initialized and assigning in side keyevent like listData[7] =// Here am getting error . is it possible to assiagn values here ?

{

"Organization",

"Meeting",

"Team",

"Project",

"General",

"Task",

"Call",

"Client"

};

Please see my Code

package swing;

import java.awt.GridBagLayout;

import javax.swing.JPanel;

import javax.swing.JTextField;

import java.awt.Rectangle;

import javax.swing.JComboBox;

import javax.swing.BorderFactory;

import javax.swing.border.EtchedBorder;

import javax.sql.*;

import java.sql.*;

import java.lang.*;

publicclass Key2extends JPanel{

privatestaticfinallong serialVersionUID = 1;

private JTextField jTextField =null;

private JComboBox jComboBox =null;

publicstatic String aa;

publicfinal String listData[] =new String[7];

public String name;

Connection dbconn;// @jve:decl-index=0:

ResultSet results;

PreparedStatement sql;

/**

* This is the default constructor

*/

public Key2(){

super();

initialize();

}

/**

* This method initializes this

*

* @return void

*/

privatevoid initialize(){

this.setSize(300, 200);

this.setLayout(null);

this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

this.add(getJTextField(),null);

//this.add(getJComboBox(), null);

try{

Class.forName("com.mysql.jdbc.Driver");

try

{

dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyNewDatabase","root","nbuser");

}

catch (SQLException s)

{

System.out.println("SQL Error<br>");

}

}catch (ClassNotFoundException err)

{

System.out.println("Class loading error");

}

}

/**

* This method initializes jTextField

*

* @return javax.swing.JTextField

*/

private JTextField getJTextField(){

if (jTextField ==null){

jTextField =new JTextField();

jTextField.setBounds(new Rectangle(90, 25, 130, 39));

jTextField.addKeyListener(new java.awt.event.KeyAdapter(){

publicvoid keyPressed(java.awt.event.KeyEvent e){

String ba = jTextField.getText();

System.out.println("keyPressed()"+ba);// TODO Auto-generated Event stub keyPressed()

System.out.println("Length of the string >"+ba.length());

try{

sql = dbconn.prepareStatement("SELECT * FROM Log");

results = sql.executeQuery();

String[] u_id=new String [10];

while(results.next())

{

int ii = 0;

u_id[ii]=results.getString(2);

System.out.println("passed");

System.out.println("My id is "+u_id[ii]);

//

////////

name ="rayees";

System.out.println("name-->"+name);

///////

listData[7] =// Here am getting error . is it possible to assiagn values here ?

{

"Organization",

"Meeting",

"Team",

"Project",

"General",

"Task",

"Call",

"Client"

};

////////

}

}catch (SQLException ss)

{

System.out.println("In side the querry execution error ");

}

jTextField.add(getJComboBox(),null);

}

});

}

return jTextField;

}

/**

* This method initializes jComboBox

*

* @return javax.swing.JComboBox

*/

// @jve:decl-index=0:

public JComboBox getJComboBox(){

if (jComboBox ==null){

jComboBox =new JComboBox(listData);

jComboBox.setBounds(new Rectangle(94, 95, 94, 19));

}

return jComboBox;

}

}

[/nobr]

[8147 byte] By [rayees1234a] at [2007-11-27 10:53:39]
# 1

listData[7] = // Here am getting error . is it possible to assiagn values here ?

{

"Organization",

"Meeting",

"Team",

"Project",

"General",

"Task",

"Call",

"Client"

};

When you get an error you should include that with your post. I imagine you are getting an error becuase you are

trying to add all those Strings into the 7th slot of your array. Two problems with that:

1. You can only add a single String to a single slot

2. There is no position 7. If your array is 7 elements long, they are numbered 0 - 6.

floundera at 2007-7-29 11:45:26 > top of Java-index,Java Essentials,Java Programming...
# 2

Use this

final String listData[] ={"Organization","Meeting","Team","Project","General","Task","Call"};

AmitChalwade123456a at 2007-7-29 11:45:26 > top of Java-index,Java Essentials,Java Programming...
# 3

But i want to Declare it globally and assign the vales in side the keystroke

rayees1234a at 2007-7-29 11:45:26 > top of Java-index,Java Essentials,Java Programming...
# 4

[nobr]Even though Single entry is not working

package swing;

import java.awt.GridBagLayout;

import javax.swing.JPanel;

import javax.swing.JTextField;

import java.awt.Rectangle;

import javax.swing.JComboBox;

import javax.swing.BorderFactory;

import javax.swing.border.EtchedBorder;

import javax.sql.*;

import java.sql.*;

import java.lang.*;

public class Key2 extends JPanel {

private static final long serialVersionUID = 1;

private JTextField jTextField = null;

private JComboBox jComboBox = null;

public static String aa;

public final String listData[] = new String[0];

public String name;

Connection dbconn; // @jve:decl-index=0:

ResultSet results;

PreparedStatement sql;

/**

* This is the default constructor

*/

public Key2() {

super();

initialize();

}

/**

* This method initializes this

*

* @return void

*/

private void initialize() {

this.setSize(300, 200);

this.setLayout(null);

this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

this.add(getJTextField(), null);

//this.add(getJComboBox(), null);

try{

Class.forName("com.mysql.jdbc.Driver");

try

{

dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyNewDatabase","root","nbuser");

}

catch (SQLException s)

{

System.out.println("SQL Error<br>");

}

}catch (ClassNotFoundException err)

{

System.out.println("Class loading error");

}

}

/**

* This method initializes jTextField

*

* @return javax.swing.JTextField

*/

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField();

jTextField.setBounds(new Rectangle(90, 25, 130, 39));

jTextField.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(java.awt.event.KeyEvent e) {

String ba = jTextField.getText();

System.out.println("keyPressed()"+ba);// TODO Auto-generated Event stub keyPressed()

System.out.println("Length of the string >"+ba.length());

try{

sql = dbconn.prepareStatement("SELECT * FROM Log");

results = sql.executeQuery();

String[] u_id=new String [10];

while(results.next())

{

int ii = 0;

u_id[ii]=results.getString(2);

System.out.println("passed");

System.out.println("My id is "+u_id[ii]);

//

////////

name = "rayees";

System.out.println("name-->"+name);

///////

// Here am getting error . is it possible to assiagn values here ?

listData ={"Organization"};

////////

}

}catch (SQLException ss)

{

System.out.println("In side the querry execution error ");

}

jTextField.add(getJComboBox(), null);

}

});

}

return jTextField;

}

/**

* This method initializes jComboBox

*

* @return javax.swing.JComboBox

*/

// @jve:decl-index=0:

public JComboBox getJComboBox() {

if (jComboBox == null) {

jComboBox = new JComboBox(listData);

jComboBox.setBounds(new Rectangle(94, 95, 94, 19));

}

return jComboBox;

}

}

[/nobr]

rayees1234a at 2007-7-29 11:45:26 > top of Java-index,Java Essentials,Java Programming...
# 5

public final String[] listData = {"Organization",

"Meeting",

"Team",

"Project",

"General",

"Task",

"Call",

"Client"

};

Message was edited by:

Yannix

Yannixa at 2007-7-29 11:45:26 > top of Java-index,Java Essentials,Java Programming...
# 6

Dear i need to declare in globally .....

rayees1234a at 2007-7-29 11:45:26 > top of Java-index,Java Essentials,Java Programming...
# 7

Hi all,

Need to declare globally, and from the method one need to assign the values and the same value i need to access from another method

rayees1234a at 2007-7-29 11:45:26 > top of Java-index,Java Essentials,Java Programming...
# 8

list = new String[] {"one", "two", "three"};

floundera at 2007-7-29 11:45:26 > top of Java-index,Java Essentials,Java Programming...