2

So I am reading a file and I get the amount of lines in that file. Based on that I generate my interface. Now I need to have the ability to edit valus trougth UI . Rows is the variable that has how lines the input document has . Of course the code below doesnt work . I want to write new values to the array from which i read .

for(int i=0;i<Rows;i++)
{
    //System.out.println("!"+Symbol[1]+"!");
    //if(Symbol[i]!=""&&Symbol[i]!=null)
    // {
    JTextField symbol = new JTextField(6);
    symbol.setText(Symbol[i]);
    symbol.setBounds(10,25*i+10 , 75, 20);
    symbol.setEditable(false);
    frame.add(symbol);
    JTextField buyf = new JTextField(4);
    buyf.setText(String.valueOf(buy[i]));
    buyf.setBounds(95, 25*i+10, 50, 20);
    buyf.setEditable(true);
    buyf.addActionListener(new java.awt.event.ActionListener() { 
         public void actionPerformed(ActionEvent ae) {  
              buy[i]=Integer.parseInt(buyf.getText());
         }
    });                      
    frame.add(buyf);
} 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1633277
  • 510
  • 2
  • 7
  • 14
  • 1
    I have no idea what you want.. but maybe the title of the question is your answer? Don't make them local variables.. – Lews Therin Sep 28 '12 at 11:34
  • I want to read a file generate UI based on that , and be able to use JTextFields for input – user1633277 Sep 28 '12 at 11:41
  • The description of your problem is not clear... Anyway, the 'buyf' variable has to be declared as final if you want to use it in you anonymous inner class. – Yanflea Sep 28 '12 at 11:41
  • I want to generate a certain number of JTextFields at runtime based on input file and then write values from those JTextFields into an Array where each JTextField is connected to one and only one cell in that array . That array is public . – user1633277 Sep 28 '12 at 11:54
  • Please respect the java coding conventions. It looks like you have a variable named `symbol` and one named `Symbol` – Robin Sep 28 '12 at 13:17

1 Answers1

3
  1. don't use AbsoluteLayout e.g. symbol.setBounds(10,25*i+10 , 75, 20); use proper LayoutManager, maybe GridLayout is best for your ...

  2. use DocumentListener for listening of changes in JTextComponents

  3. use JFormattedTextField with Number formatter, rather than plain JTextField, then you can remove everything about parseWhatever

  4. you can to use plain JTextField but with DocumentFilter (remove non numberic chars)

  5. ActionListener could be correct Listener an alternative is DocumentListener in point second

  6. for better help sooner post an SSCCE, but I think that DocumentListener can solve that

mKorbel
  • 109,525
  • 20
  • 134
  • 319