1

I want to add to a Java JFrame a list of my class's fields names, along with their respective values. Those fields can of course change value during the program's execution and i want this change to be displayed dynamically in my GUI. If for example i have a field private int networkId; in a class BaseStation, how can i make it appear in the frame of a class BaseStationFrame?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
nikos
  • 2,893
  • 11
  • 30
  • 39

4 Answers4

1

I'm not 100% understanding the question, but would the l2fprod PropertySheet do what you need if via JavaBeans?

Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55
1

A common approach would be to access the methods from the component that allow you to manipulate the component values.

Here a simple example using a text area.

private final static String newline = "\n";
private JTextArea textArea;


public void init() {
   //Make sure your components are not null;
   textArea = new JTextArea();  
}   

public void actionPerformed(ActionEvent evt) {
    String text = textArea.getText();
    textArea.append(textArea + newline);
}

//Get set methods

Depending on your needs and variable type, you might need to parse or use special methods. I would recomend you to read the documentation for the javax.swing package.

Also in oracles site there is a great tutorial on javax.swing GUI

javing
  • 12,307
  • 35
  • 138
  • 211
  • I am not sure i completely understood your example above. How can i use this code to display a particular variable, e.g. `int x`? – nikos Dec 07 '11 at 19:46
  • @nikos for example you could have a JLabel and update it every time you type something in the text area. To be honest i dont really understand what do you really want. Is it maybe your problem that you dont see the changes take place? (See this question: http://stackoverflow.com/questions/3179136/jtable-how-to-refresh-table-model-after-insert-delete-or-update-the-data) – javing Dec 08 '11 at 06:26
1

Well...you could store static final Strings that contain the names of your fields, and design some association between the String representation of the name of a field, and the value of that field.

Or, you could use a Map for all of your fields, defining key-value pairs for them. When you want to call the fields, you do a Map.get(), and when you want to print them, you could do something like

Set s = map.keySet()
for(Object o : s) {
    print(o.toString() + " = " + map.get(o).toString())
}

This is, of course, Sudocode to hopefully get the ball rolling for you

Good Luck :)

Cody S
  • 4,744
  • 8
  • 33
  • 64
1

If you want your UI to have access to private fields of your class, you won't have much options

  • Use reflection to access those fields (not recommended)
  • Make those fields public (not recommended either) or package visible and place all code in one package (not recommended either)
  • Add decent getters and setters and fire PropertyChangeEvents when those fields change.

Your UI can then use the getter to obtain the initial value, and use the events to update the UI when the value of a field has been changed. You could even write a fairly generic UI component for this by using reflection to obtain all the getters, but if it is only for a few classes this might be overkill

Robin
  • 36,233
  • 5
  • 47
  • 99