1

I set up a small program that can create rows and columns at run time, its for a math class implementing sets, I already got the program to create the rows and columns but what I wanna do next is be able to get the values inside and then load another form of the same structure and get that forms values then add them and show them on a final form. Fairly new to the whole java thing, so if anyone could help it'd mean the world. Here's what I have so far..

import java.awt.*;
import java.awt.event.ActionEvent;   
import javax.swing.*;

public class Matrice extends JFrame {

protected static final int PREF_W = 200;
protected static final int PREF_H = 200;
JPanel mainPanel = new JPanel() {
  @Override
  public Dimension getPreferredSize() {
     return new Dimension(PREF_W, PREF_H);
  }
 };

 public JComponent getMainComponent() {
  return mainPanel;
   }

public Matrice(){
int rows, columns, end;

String RI, CI;

RI = JOptionPane.showInputDialog(null, "How many rows: ");
rows = Integer.parseInt(RI);
CI = JOptionPane.showInputDialog(null, "How many columns: ");
columns = Integer.parseInt(CI);
end = rows * columns;

JPanel holder = new JPanel(new GridLayout(rows, columns));
    holder.setVisible(true);
for(int i = 0; i < end; i ++){
holder.add(new JTextField("output"));
}
JPanel set = new JPanel(new GridLayout(1, 0));
JTextField r = new JTextField("rows");
JTextField c = new JTextField("columns");
set.add(r);
set.add(c);
set.add(new JButton("Set"));
mainPanel.add(set, BorderLayout.SOUTH);
mainPanel.add(holder, BorderLayout.NORTH);
}

 private static void createAndShowGui() {
   Matrice test = new Matrice();
   JFrame frame = new JFrame("Matrice");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.getContentPane().add(test.getMainComponent());
   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
    }

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        createAndShowGui();
     }
    });
}

    }

never mind the buttons and the pre-set TextField

LeMarc
  • 25
  • 1
  • 1
  • 4
  • If you're laying out a matrix for the UI why not just use that same matrix concept to hold your text fields? Just follow through with the concept that you initiated with the way you built your grid layout. Hint: JTextField[][]. – MarsAtomic Mar 16 '14 at 02:24
  • 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) Instead of using a `JTextField`, use a `JSpinner` as seen [here](http://stackoverflow.com/a/10021773/418556). – Andrew Thompson Mar 16 '14 at 02:35
  • You could use a JTable instead of rows/columns of text fields – MadProgrammer Mar 16 '14 at 04:45

2 Answers2

1

Simple answer to your question is that you need to define a array of TextField after getting the size:

TextField[][] inputFields = new TextField[rows][columns];

now add the TextFields

getContentPane().setLayout(new GridLayout(rows, columns));
for(int i = 0; i < rows; i++){
    for(int j = 0; j < columns; j++){
        inputFields[i][j] = new TextField("output");
        holder.add(inputFields[i][j]);
    }
}

There might be syntax errors above as I am directly typing it here.

Sourabh Bhat
  • 1,793
  • 16
  • 21
0

To get the text out of a JTextField call .getText() on the object. so r.getText();

An elegant way to get a number out of the field is to use Integer.parseInt(String)

so your call would be Integer.parseInt(r.getText()) Note that this may throw

NumberFormatException so you will have to catch the exception and maybe reprompt the user to enter a valid number.

t3dodson
  • 3,949
  • 2
  • 29
  • 40