-1

I will need to do various test runs with the KDTreeTester.java file attached below. It'd be pretty inefficient to change the values (w, h, n, x) every time I finish a run and recompile the file.

That's when I thought it would be pretty handy to just type the values before the main program opens.

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Main program that starts the KDTree visualization
 */
 public class KDTreeTester extends JFrame{

  int w=400;    //width of the window
  int h=400;    //height of the window
  int n=20;     //number of points
  int x=5;      //number of points to be searched (has to be smaller than n)
  KDTreeVisualization vis;  //the visualization

  public KDTreeTester(){

    //  Initialize the visualization and add it to the main frame. 
    vis = new KDTreeVisualization(w, h, n);
    add(vis);

The code goes on, but I think only that's the relevant part. I need to have a box asking for the four values (width, height, number of points, number of points to be searched). The would have to be parsed from String to int I guess.

What's the best way to implement this?

Lars
  • 11
  • 2

1 Answers1

0

As @XtremeBaumer told you :

you cant do anything before the main method starts, only if you start via cmd

However, if you mean ask for input values before your main frame is displayed, you may use JOptionPane to collect the values.

Here is an example based on Multiple input in JOptionPane.showInputDialog and, to limit the format to integer type, on How to make JFormattedTextField accept integer without decimal point (comma)?

Just call this before instantiating your KDTreeVisualization, and call it with those input parameters once you have them.

I wouldn't call this from the constructor of your JFrame though, but rather before instantiating the JFrame, or even through a custom menu/button/whatever you may add to your frame.

NumberFormat integerFieldFormatter = NumberFormat.getIntegerInstance();

JFormattedTextField nField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField xField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField widthField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField heightField = new JFormattedTextField(integerFieldFormatter);

Object[] message = {

        "Width :", widthField,
        "Height :", heightField,
        "n :", nField,
        "x :", xField
};

int option = JOptionPane.showConfirmDialog(null, message, "Enter values", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {

    try {
        int n = Integer.parseInt(nField.getText());
        int x = Integer.parseInt(xField.getText());
        int width = Integer.parseInt(xField.getText());
        int height = Integer.parseInt(xField.getText());

        // do stuff with the values, like instantitate your object with those parameters
        // new KDTreeVisualization(width, height, n)

    } catch (NumberFormatException e) {

        JOptionPane.showMessageDialog(null, "At least one of the values is invalid", "Error",
                JOptionPane.ERROR_MESSAGE);
    }

} else {
    System.out.println("Input cancelled");
}
Community
  • 1
  • 1
Arnaud
  • 17,229
  • 3
  • 31
  • 44