Hi everyone, I've got a small (I hope) yet interesting problem. I have got an assignment today at my university which causes me a headache by now. The job is to design a swing app which uses reflection to create an instance of a class called "Pravokutnik" (never mind the class name) which is a subclass of JPanel. You write the parameters into a JTextField by this order: "className,Color,width,height"
If I want a red "square" (pravokutnik=square) which is 100x100, the input should be "Pravokutnik,red,100,100"
A click of the button bellow should gather info about the class, color and dimensions and create an instance of Pravokutnik and
add it into the jPanelMain using FlowLayout. Every new one functions the same way etc.
My problem does not occure until the reflection-raped (:D) square is to be added to the jPanelMain. What happens is that i do not see a new Pravokutnik added to the GUI. EDIT: Tried to play with invalidate() functions called on the jPanelMain, and on the whole JFrame itself. No progress what so ever...
My code:
import java.awt.Color;
import java.lang.reflect.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
public class Zad1_JavaII extends javax.swing.JFrame {
private Color c;
public Zad1_JavaII() {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jPanelMain = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
jLabel1.setText("Unesite parametre oblika:");
jTextField1.setToolTipText("Oblik unosa parametara: Pravokutnik,boja,xVel,yVel");
jTextField1.setName("jTextFieldParam");
jButton1.setText("Stvori oblik");
jButton1.setName("jButtonNew");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
java.awt.FlowLayout flowLayout1 = new java.awt.FlowLayout();
flowLayout1.setAlignOnBaseline(true);
jPanelMain.setLayout(flowLayout1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanelMain, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String parametriString = jTextField1.getText();
String[] parametri = parametriString.split(",");
Class boja = Color.class;
Field f;
try {
f = boja.getDeclaredField(parametri[1].toUpperCase());
c = (Color)f.get(null);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException |
SecurityException ex) {
ex.printStackTrace();
}
Class pravokutnik = null;
try {
pravokutnik = Class.forName("packageName." + parametri[0]);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
Constructor ctr;
try {
try {
ctr = pravokutnik.getDeclaredConstructor(Class.forName("java.awt.Color"));
Pravokutnik p = null;
try {
p = (Pravokutnik) ctr.newInstance(c);
p.setSize(getWidthRefl(parametri[2]), getHeightRefl(parametri[3]));
} catch (InstantiationException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException ex) {
System.out.println(ex.getMessage());
}
jPanelMain.add(p);
System.out.println("Panel should be added");
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
} catch (NoSuchMethodException | SecurityException ex) {
System.out.println(ex.getMessage());
}
}
private int getWidthRefl(String s) {
int width;
width = Integer.parseInt(s);
return width;
}
private int getHeightRefl(String s) {
int height;
height = Integer.parseInt(s);
return height;
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Zad1_JavaII.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Zad1_JavaII.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Zad1_JavaII.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Zad1_JavaII.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Zad1_JavaII().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanelMain;
private javax.swing.JTextField jTextField1;
}
P.S.
Sorry for the long post, I added pretty much all of the code so you can try it out yourself.