Re: Cannot clean up Evaluator.java code - errors involving javax.tools.*
phillip.s.powell@gmail.com wrote:
Once again I am going to illustrate how my
"language" and your "language" are so not related. I need to do in
Java what can easily be done in PHP like this:
If you want to learn Java, it may be best to stop thinking in PHP.
Telling Java programmers how expressive PHP is doesn't help those Java
programmers to help you. Nor is it particularly motivating (for me at
least).
Since I don't know PHP, quoting PHP code at me doesn't illuminate your
problem. There must be some reason why you are using Java not PHP. I
suggest we just accept that and drop all mention of PHP.
I know Java has no eval(), but I unfortunately have to come up with
something like that to build a particular application with a dynamic
amount of JButtons, in short, you could have 20, 30, 200, 2 billion [ok
a bit user-unfriendly, but..]
This sounds to me like an X-Y problem. I wish to accomplish X, In PHP
I'd achieve this using Y. Therefore I will ask how to do Y in Java
(without describing X).
I'm not sure which X your Y would solve in some other language. Here's
one solution to one X that might help. If your X is different, please
try to describe it (without reference to languages other than Java).
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
public class DynamicButtons extends JPanel implements ActionListener {
SpinnerNumberModel model;
JSpinner numberSpinner;
JButton addButton = new JButton("Go");
DynamicButtons() {
add(new JLabel("How many buttons do you want?"));
model = new SpinnerNumberModel(5, 1, 100, 1);
numberSpinner = new JSpinner(model);
add(numberSpinner);
addButton.addActionListener(this);
add(addButton);
}
public void actionPerformed(ActionEvent arg0) {
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(
new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
int n = model.getNumber().intValue();
for (int i = 0; i < n; i++) {
buttonPanel.add(
new JButton("Button " + Integer.toString(i)));
}
JOptionPane.showMessageDialog(this, buttonPanel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("Lotsa Buttons");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new DynamicButtons());
f.pack();
f.setVisible(true);
}
});
}
}