Maybe you can help me out here :)
I want to "simulate" like say 10 machine-stations. In my JFrame
/Container
(I tried both) I put these 10 maschines ( = 10 JPanels
containing x buttons, textfields, whatever of my desired design), and I want to have different informations on each one and change them for my needs.
I tried to change the value of a JTextField
with an JButton
(like setting the priority of the machine + 1. But I cannot distinguish between the 10 "priority up" buttons :(
How you do that? My idea was to speak somehow to the JPanel
it came from but I can´t.
Container wizardFrame = new JFrame();
wizardFrame.setLayout(new GridLayout(2,5));
String Name;
for(int i = 1; i < 11; i++){
Name = "Maschine " + i;
fillWizardFrame(wizardFrame, Name, i);
}
wizardFrame.setVisible(true);
}
public void fillWizardFrame(Container wizardFrame, String Name, int i) {
JPanel MaschineId = new JPanel();
MaschineId.setLayout(new BorderLayout());
JTextField maschineName = new JTextField(Name ,10);
MaschineId.add(maschineName, BorderLayout.WEST);
maschinePrioritaet = new JTextField("20",10);
MaschineId.add(maschinePrioritaet,BorderLayout.CENTER);
JButton Higher = new JButton("Higher " + i); Higher.addActionListener(this);
MaschineId.add(Higher, BorderLayout.NORTH);
wizardFrame.add(MaschineId);
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getActionCommand().contains("Higher")){
System.out.println("Higher pressed " + event.getActionCommand());
}
// i tried .getID , .getSource etc... :/
}
I want to change the value of maschinePrioritaet
with my "higher" button, but I can´t... This thing took me hours of searching and trying but wasn´t able to find something.
Thank you so much for your help!
Best, Andrea
You have to work more with objects and especially Views (like in MVC). A View represents an object, in this case it looks like some machine (which is a name, an id and a priority). So you need to create a machine panel that is attached to that model.
Here is something closer to that (but there are still many improvements to do):
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Wizard {
public Wizard() {
JFrame wizardFrame = new JFrame();
wizardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wizardFrame.setLayout(new GridLayout(2, 5));
String name;
for (int i = 1; i < 11; i++) {
name = "Maschine " + i;
MashinePanel mashinePanel = new MashinePanel(name, i);
wizardFrame.add(mashinePanel.getPanel());
}
wizardFrame.pack();
wizardFrame.setVisible(true);
}
public static class MashinePanel implements ActionListener {
private final String name;
private final int id;
private JTextField maschineNameTF;
private JFormattedTextField maschinePrioritaetTF;
private JButton higher;
private JPanel machinePanel;
public MashinePanel(String name, int i) {
super();
this.name = name;
this.id = i;
machinePanel = new JPanel();
machinePanel.setLayout(new BorderLayout());
maschineNameTF = new JTextField(name, 10);
machinePanel.add(maschineNameTF, BorderLayout.WEST);
maschinePrioritaetTF = new JFormattedTextField(20);
maschinePrioritaetTF.setColumns(10);
machinePanel.add(maschinePrioritaetTF, BorderLayout.CENTER);
higher = new JButton("Higher " + i);
higher.addActionListener(this);
machinePanel.add(higher, BorderLayout.NORTH);
}
public JPanel getPanel() {
return machinePanel;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
@Override
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().contains("Higher")) {
Object value = maschinePrioritaetTF.getValue();
int priority = 20;
if (value instanceof Integer) {
priority = (Integer) value;
}
maschinePrioritaetTF.setValue(priority + 1);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Wizard();
}
});
}
}