how to create TextFields dynamically in j2me? - java

we are developing Mobile application in j2me.In my application, we are using TextField and some other controls in Form.Here, my problem is i want to dynamically create TextField based on User's Credentials.For Example, If Manager is entered,then i want to create certain TextField(based on Manager Selection) for getting input from the Manager.Otherwise,i just want to create TextField that are less than the Manager TextField.
How to Create TextFields Dynamically...
For example like this...
int userSelection=10;
for(int i=0;i<userSelection;i++)
TextField text=new TextField("Some Name",null);
here, our problem is,
I want to create TextField With Different Name...
Please guide me to get out of this issue...

Create the TextField array and refer from array index.
TextField[] textFields = new TextField[10];
for (int i = 0; i < textFields.length; i++) {
textFields[0] = new TextField(label, text, maxSize, constraint);
}

after you use correct parameters to construct TextField, code might look like
import javax.microedition.lcdui.TextField;
import java.util.Vector;
// ...
Vector newTextFields(int userSelection) {
// neither List nor generics in midp sorry
final int MAX_SIZE = 42;
final Vector list = new Vector();
for(int i=0; i < userSelection; i++) {
list.addElement(new TextField("Name #" + i, null,
MAX_SIZE, TextField.ANY);
}
return list;
}
// ...

Related

Add value to a selected jlist model

So I am trying to make a program that adds a subject to a jList from a textfield and after that, I want to add the grade for that subject from a textfield. Is it possible to store (in an array) that value to the item selected on the jList? so that I can access it for getting the average of all the grades of the subjects entered.
int x[] = jList1.getSelectedIndices();
for(int i = 0; i < jList1.getModel().getSize(); i++){
grade[x[i]] = Double.parseDouble(jTextField2.getText());
jList1.getSelectedValue();
}
You are supposed to manipulate data in the model that jList is based on.
listModel = new DefaultListModel();
listModel.addElement("Jane Doe");
listModel.addElement("John Smith");
listModel.addElement("Kathy Green");
list = new JList(listModel);
https://docs.oracle.com/javase/tutorial/uiswing/components/list.html#creating
Jlist is a (view) component that displays a list of objects and allows the user to select one or more items. A separate model, ListModel, maintains the contents of the list.
https://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html
What is the exact problem with your implementation?
EDIT
Double grade = Double.parseDouble(jTextField2.getText());
for(int i = 0; i < jList1.getModel().getSize(); i++){
String before = jList1.getModel().getElementAt(i);
String after=before+"_"+String.valueOf(grade);
jList1.getModel().setElementAt(after,i);
}
howto change a value of jlist element in jpane dialog

Minesweeper, how can i work on cells

I would like to create a minesweeper game. Firstly, this will work on buttons. I think I will work on two dimensional array, and there will be like boolean array that will present where are bombs, for example if booleanArray[0][4] is true, there is a bomb.
Now, how can I implement this in my buttons? I can set Names on these buttons, and then if I click some button, then I will get the name from this div. For example when i click first button, i will get "00" name, then i will get first letter "0" and second letter "0" and parse it to int. And this will be the indexes from my previous booleanArray, in this case it will be booleanArray[0][0].
So, can I do this another, better way, instead of that?
This is the way I will be creating the buttons:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
JButton button = new JButton("");
button.setPreferredSize(new Dimension(50, 50));
button.setName(Integer.toString(i) + Integer.toString(j));
}
}
EDIT
I will have a two dimensional Array, that will reflect my buttons:
and now, how can I check if I hit the bomb after I click for example in the first button?
Just hold a two dimensional array of buttons,
JButton[][] myButtons = new JButton[10][10];
which you use to draw them and they all call the same method with their value
for (int x=0; x<10; x++){
for (int y=0; y<10; y++){
myButtons[x][y] = new JButton("0");
//add to page, or do it elsewhere
myButtons[x][y].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectionButtonPressed(x,y);
}
});
}
}
Then a call to yout method selectionButtonPressed() will tell you what button has been pressed, you can take action and even make changes to the button with myButtons[x][y].whatever()
That is one way but you should create the String of the name by using something like:
button.setName(i + ":" j);
This will make it easier to parse out the two values as you can just use the String.split(...) method.
Another option might be to create a HashTable with the JButton as the key and then use a Point object (representing the row/column) as the value Object of the HashTable. Then you just use the get(...) method of the HashMap to retrieve the Point for the clicked button.
Another option is to extend JButton and add two parameters (row, column) when creating the button. Then you also add getRow() and getColumn() methods.
Either of this approaches will keep the logic simple and you only need to create a single ActionListener to be used by all the buttons.

I need to make a reference to a JLabel based off a string

So I have numbered JLabels declared like so (In the class)
OP_1
OP_2
OP_3
etc..
And I have a number and a string.
What I want is that when, for example, the number is 2. I want to change the label text to the content of the string. This is part of a method that is supposed to take a string, put it into the last available JLabel, and then increment the number.
I am very confused, and help would be appreciated.
Here I created an array of JLabels and a method, updateNextLabel(String), which will update the next JLabel with whatever you enter for str.
public class Example {
static int count = 0; //Create a count
static JLabel[] array = new JLabel[3]; //Create an array to hold all three JLabels
public static void main(String[] args) {
//Set the default text for each JLabel
array[0] = new JLabel("This is OP1");
array[1] = new JLabel("This is OP2");
array[2] = new JLabel("This is OP3");
//Here is an example if you wanted to use a for-loop to update the JLabels
for (int x = 0; x < array.length; x++) {
updateNextLabel("This is the new text for OP" + (count + 1));
System.out.println(array[x].getText());
}
}
public static void updateNextLabel(String str) {
array[count].setText(str);
count++;
}
}
Instead of / additional to naming your labels by specific names you can later match them on, I'd think a Map of JLabels with Strings or Integers as keys might be a better approach:
Map<String,JLabel> labelMap = new HashMap<String,JLabel>();
labelMap.put("1", OP_1);
labelMap.put("2", OP_2);
This will allow later access of "The label for key 2" as well as "list me all that labels and find the one with text 2" as well

GUI in Java.. generating a run time component

I have an User Interface where the user inputs a number (10 for example) in a textfeild
then if the user presses enter I want 10 text feilds to be generated in the same User Interface.
how can I do that?
Something like that:
// Assuming myOrigTextField is your original JTextField
int howMany = Integer.parseInt(myOrigTextField.getText());
JTextField[] jtfs = new JTextField[howMany];
for (int i = 0; i < jtfs.length; ++i) {
jtfs[i] = new JTextField();
myPanelToAddThem.add(jtfs[i]);
}
Create the text field objects,
add them to your container.
Use a loop to do this with an arbitrary number.
Post some code and your specific problems for more help.
Roughly assuming some thing about layout manager you're using I'd say this:
public List<JTextField> addComponents( int number ) {
List<JTextField> fields = new ArrayList<JTextField>( number );
for( int i = 0; i < number; i++ ) {
fields.add( new JTextField() );
panelToAddComponentsTo.add( fields.get( i ) );
}
return fields;
}
if the user presses enter I want ? text feilds to be generated in the same User Interface
You add an ActionListener to the text field. The ActionListener will be invoked when the Enter key is pressed.
In the ActionListener code you need to parse the number entered and then loop to create and add the text fields to your panel:
for (...)
{
panel.add( new JTextField(...) );
}
panel.revalidate(); // needed when dynamically adding/removing components
panel.repaint(); // sometimes needed

JOptionPane in Java

Does anyone know why tab (\t) does not work with JOptionPane.showMessageDialog?
My code is as follows:
String addText = "NAME\t\tADDRESS\t\tTEL.No\tEMAIL\n";
for (int i = 0; i < addressBookSize; i++) {
addText = addText+entry[i].viewAllInfo();
}
System.out.print(addText);
JOptionPane.showMessageDialog(null, addText);
Are there other ways to align text in JOptionPane?
Put your tabbed text into JTextArea
String addText = "NAME\t\tADDRESS\t\tTEL.No\tEMAIL\n";
for (int i = 0; i < addressBookSize; i++) {
addText = addText+entry[i].viewAllInfo();
}
System.out.print(addText);
JOptionPane.showMessageDialog(null, new JTextArea(addText));
Looking at your data again, I'd probably display it in a JTable, and then if desired, would display this in a JOptionPane or in a GUI. If you need simpler, then display it in a JTextArea whose font has been set to monospaced, and use String.format(...) or something similar to allow your Strings to be displayed in a table.

Categories

Resources