So I tried to make a simple java swing program, i didn't finish the program but I wanted to see how it looks before doing the functionality part, this is my code:
import layouts.SpringUtilities;
import javax.swing.*;
import layouts.SpringUtilities;
public class FactorialCalculatorFrame extends JFrame {
public FactorialCalculatorFrame(){
JPanel panel = new JPanel();
panel.setLayout(new SpringLayout());
JTextField brojText = new JTextField();
brojText.setColumns(10);
panel.add(new JLabel("Broj:", SwingConstants.RIGHT));
panel.add(brojText);
JButton start = new JButton("Start");
panel.add(new JLabel("Pokreni izracun:",SwingConstants.RIGHT));
panel.add(start);
JProgressBar napredakProgressBar = new JProgressBar();
add(new JLabel("Napredak:", SwingConstants.RIGHT));
add(napredakProgressBar);
JLabel rezultat = new JLabel("Rezultat:");
JLabel ispisiRez = new JLabel("");
add(new JLabel("Rezultat:", SwingConstants.RIGHT));
add(ispisiRez);
start.addActionListener((e)->{
try {
int number = Integer.parseInt(brojText.getText());
//reset GUI components
napredakProgressBar.setValue(0);
start.setEnabled(false);
ispisiRez.setText("");
//schedule for execution on one of working threads
new primeNumberJavaSwingApp().execute();
} catch (Exception ex) {
ex.printStackTrace();
}
});
SpringUtilities.makeCompactGrid(panel,4, 2, 0, 0, 5, 5);
add(panel);
}
public static void main(String[] args){
FactorialCalculatorFrame frame = new FactorialCalculatorFrame();
frame.setVisible(true);
}
public class primeNumberJavaSwingApp extends SwingWorker<Long, Integer> {
@Override
protected Long doInBackground() throws Exception {
Long l = (long)3.2;
return l;}}}
the Exception that occurs is this Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: No such child: 4
at layouts.SpringUtilities.makeCompactGrid(SpringUtilities.java:190)
at FactorialCalculatorFrame.<init>(FactorialCalculatorFrame.java:55)
Why does this occurs, what did I do wrong?
I also use SpringUtilities class that I copied from Oracle. Any help is greatly appriciated!
I downloaded source code for class SpringUtilities
from here.
Your problem is that you are not adding all the components to the panel
. You are calling add()
when you should be calling panel.add()
. Hence you are only adding 4 components to panel
rather than 8. Hence the error message.
I also added a call to method pack()
, of class JFrame
, in order to make the JFrame
big enough to display all the components it contains.
Here is your code with my fixes:
import layout.SpringUtilities; // in your code this import is added twice
import javax.swing.*;
public class FactorialCalculatorFrame extends JFrame {
public FactorialCalculatorFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE); // I added this line
JPanel panel = new JPanel();
panel.setLayout(new SpringLayout());
JTextField brojText = new JTextField();
brojText.setColumns(10);
panel.add(new JLabel("Broj:", SwingConstants.RIGHT));
panel.add(brojText);
JButton start = new JButton("Start");
panel.add(new JLabel("Pokreni izracun:",SwingConstants.RIGHT));
panel.add(start);
JProgressBar napredakProgressBar = new JProgressBar();
panel.add(new JLabel("Napredak:", SwingConstants.RIGHT)); // change here
panel.add(napredakProgressBar); // change here
JLabel rezultat = new JLabel("Rezultat:");
JLabel ispisiRez = new JLabel("");
panel.add(new JLabel("Rezultat:", SwingConstants.RIGHT));
panel.add(ispisiRez);
start.addActionListener((e)->{
try {
int number = Integer.parseInt(brojText.getText());
//reset GUI components
napredakProgressBar.setValue(0);
start.setEnabled(false);
ispisiRez.setText("");
//schedule for execution on one of working threads
new primeNumberJavaSwingApp().execute();
} catch (Exception ex) {
ex.printStackTrace();
}
});
SpringUtilities.makeCompactGrid(panel, 4, 2, 0, 0, 5, 5);
add(panel);
}
public static void main(String[] args){
FactorialCalculatorFrame frame = new FactorialCalculatorFrame();
frame.pack(); // I added this line
frame.setLocationRelativeTo(null); // I added this line
frame.setVisible(true);
}
public class primeNumberJavaSwingApp extends SwingWorker<Long, Integer> {
@Override
protected Long doInBackground() throws Exception {
Long l = (long) 3.2;
return l;
}
}
}
Here is a screen capture of the JFrame
when running the above code.