Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Build GUI

Status
Not open for further replies.

stoolpigeon

Programmer
Aug 1, 2001
309
US
I am new to Java and I'm not necessarily looking for an answer per se but maybe some help in getting pointed in the right direction.

I would like to build an app that will have buttons across the bottom. I would like to have the program read a file and build the gui on the fly- using the file to determine how many buttons there will be, what the caption will be on each button as well as storing a value that will be sent when the button is clicked.

I am hoping to find examples of creating a gui in some manner similar to what I have described and a way of handling the click event for such a set up.

All the buttons will do the same thing- they will send a value to a server via telnet (I've got some code for that) the only thing that will change is what value the button sends, as determined by the file.

thanks for any suggestions.

Ron
 
A very simple example :

package com.ecc.swing2;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DynamicButtonExample {

private String caption[] = { "B1", "B2" , "B3" };
private String actionCommand[] = { "Cmd1", "Cmd2" , "Cmd3" };
private Controller controller = new Controller();

public DynamicButtonExample() {
}

public static void main(String[] args) {
DynamicButtonExample example = new DynamicButtonExample();
example.doIt();
}

public void doIt() {
JFrame f = new JFrame("Button example");
JPanel buttonPanel = getButtonPanel();
f.getContentPane().add(buttonPanel, "South");
f.pack();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
f.setSize(200,200);
f.setVisible(true);
}

private JPanel getButtonPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
for (int i=0; i<caption.length; i++) {
JButton button = new JButton( caption );
button.addActionListener ( controller );
button.setActionCommand ( actionCommand );
panel.add(button);
}
return panel;
}

private class Controller implements ActionListener {
public void actionPerformed(ActionEvent e) {
String value = ((JButton)(e.getSource())).getActionCommand();
System.out.println(&quot;Sending value &quot; + value + &quot; to the server ...&quot;);
}
}

}
 
Sorry, I forgot to put the code between &quot;code&quot; and &quot;/code&quot;.

Code:
package com.ecc.swing2;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DynamicButtonExample {

  // You should read this data from a file
  private String caption[] = { &quot;B1&quot;, &quot;B2&quot; , &quot;B3&quot; };
  private String actionCommand[] = { &quot;Cmd1&quot;, &quot;Cmd2&quot; , &quot;Cmd3&quot; };
  private Controller controller = new Controller();

  public DynamicButtonExample() {
  }

  public static void main(String[] args) {
    DynamicButtonExample example = new DynamicButtonExample();
    example.doIt();
  }

  public void doIt() {
    JFrame f = new JFrame(&quot;Button example&quot;);
    JPanel buttonPanel = getButtonPanel();
    f.getContentPane().add(buttonPanel, &quot;South&quot;);
    f.pack();
    f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent evt) {
            System.exit(0);
          }
      });
    f.setSize(200,200);
    f.setVisible(true);
  }

  private JPanel getButtonPanel()  {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
    for (int i=0; i<caption.length; i++) {
      JButton button = new JButton( caption[i] );
      button.addActionListener    ( controller     );
      button.setActionCommand     ( actionCommand[i] );
      panel.add(button);
    }
    return panel;
  }

  private class Controller implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String value = ((JButton)(e.getSource())).getActionCommand();
      System.out.println(&quot;Sending value &quot; + value + &quot; to the server ...&quot;);
    }
  }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top