Hi all. I have this assignment to show a panel to allow users to dynamically alter FlowLayout manager parameters. It doesnt look that pretty, but functionally it is working for the most part. I have most of it up and running, but I simply cannot get the JComboBox working. I have to use this to set the alignment parameter: left, right, or center. How do I go about detecting the selection and then having that alter my settings? Thanks in advance for any help/advice/tips. My code thus far is:

// Exercise28_1: Demonstrate FlowLayout properties
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;

public class Exercise28_1 extends JApplet {
  boolean isStandalone = false;
  JPanel jpComponents = new JPanel();
  JPanel jPanel2 = new JPanel();
  JPanel jpAlignment = new JPanel();
  FlowLayout flowLayout1 = new FlowLayout();
  JLabel jLabel1 = new JLabel();
  JPanel jPanel3 = new JPanel();
  FlowLayout flowLayout2 = new FlowLayout();
  JPanel jPanel4 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JComboBox jtfAlignment = new JComboBox(new Object[]
  {"Left", "Center", "Right"});
  FlowLayout flowLayout3 = new FlowLayout();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JPanel jpGaps = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JTextField jtfHGap = new JTextField();
  JTextField jtfVGap = new JTextField();
  FlowLayout flowLayout4 = new FlowLayout();
  FlowLayout flowLayout5 = new FlowLayout();
  JPanel jPanel5 = new JPanel();
  JPanel jPanel6 = new JPanel();
  FlowLayout flowLayout = new FlowLayout();
  TitledBorder titledBorder1;
  TitledBorder titledBorder2;

  //Construct the applet
  public Exercise28_1() {
    titledBorder1 = new TitledBorder("");
    titledBorder2 = new TitledBorder("");
    this.setSize(new Dimension(400,300));
    jPanel2.setLayout(flowLayout1);
    jLabel1.setText("Alignment");
    jPanel3.setLayout(flowLayout2);
    jpAlignment.setLayout(borderLayout1);
    jPanel4.setLayout(flowLayout3);
    jLabel3.setText("HGap");
    jLabel4.setText("VGap");
    jpGaps.setLayout(borderLayout2);
    jPanel5.setLayout(flowLayout4);
    jPanel6.setLayout(flowLayout5);
    jpComponents.setLayout(flowLayout);

    jtfHGap.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jtfHGap_actionPerformed(e);
      }
    });
    jtfVGap.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jtfVGap_actionPerformed(e);
      }
    });
    jpComponents.setBorder(titledBorder1);
    jPanel2.setBorder(titledBorder2);
    titledBorder1.setTitle("Container of FlowLayout");
    titledBorder2.setTitle("FlowLayout Properties");
    this.getContentPane().add(jpComponents, BorderLayout.CENTER);
    this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
    jPanel2.add(jpAlignment, null);
    jpAlignment.add(jPanel3, BorderLayout.WEST);
    jPanel3.add(jLabel1, null);
    jpAlignment.add(jPanel4, BorderLayout.CENTER);
    jPanel4.add(jtfAlignment, null);
    jPanel2.add(jpGaps, null);
    jpGaps.add(jPanel5, BorderLayout.WEST);
    jPanel5.add(jLabel3, null);
    jPanel5.add(jLabel4, null);
    jpGaps.add(jPanel6, BorderLayout.CENTER);
    jPanel6.add(jtfHGap, null);
    jPanel6.add(jtfVGap, null);

    // Add 15 buttons to jpComponents
    for (int i = 0; i < 15; i++)
      jpComponents.add(new JButton("Component " + i));
  }

  void jtfHGap_actionPerformed(ActionEvent e) {
    int hgap = new Integer(jtfHGap.getText()).intValue();
    flowLayout.setHgap(hgap);
    jpComponents.revalidate();
  }

  void jtfVGap_actionPerformed(ActionEvent e) {
    int vgap = new Integer(jtfVGap.getText()).intValue();
    flowLayout.setVgap(vgap);
    jpComponents.revalidate();
  }

  public static void main(String[] args) {
    Exercise28_1 applet = new Exercise28_1();
    applet.isStandalone = true;
    JFrame frame = new JFrame();
    //EXIT_ON_CLOSE == 3
    frame.setDefaultCloseOperation(3);
    frame.setTitle("Exercise28_1");
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2,
      (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }
}

I have been working on this some. I have gotten my window to look a little more presentable, although my text fields still are amazingly small, and I still haven't figured out how to make the JComboBox work. I have added an action listener, but I don't know how to apply a selection in the box to alter the FlowLayout manager. Here is my updated code:

// Exercise28_1: Demonstrate FlowLayout properties
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;

public class Exercise28_1 extends JApplet {
  boolean isStandalone = false;
  JPanel jpComponents = new JPanel();
  JPanel jPanel2 = new JPanel();
  JPanel jpAlignment = new JPanel();
  FlowLayout flowLayout1 = new FlowLayout();
  JLabel jLabel1 = new JLabel();
  JPanel jPanel3 = new JPanel();
  FlowLayout flowLayout2 = new FlowLayout();
  JPanel jPanel4 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JComboBox jcbAlignment = new JComboBox(new Object[]
  {"Left", "Center", "Right"});
  FlowLayout flowLayout3 = new FlowLayout();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JPanel jpGaps = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JTextField jtfHGap = new JTextField();
  JTextField jtfVGap = new JTextField();
  FlowLayout flowLayout4 = new FlowLayout();
  FlowLayout flowLayout5 = new FlowLayout();
  JPanel jPanel5 = new JPanel();
  JPanel jPanel6 = new JPanel();
  FlowLayout flowLayout = new FlowLayout();
  TitledBorder titledBorder1;
  TitledBorder titledBorder2;

  //Construct the applet
  public Exercise28_1() {
    titledBorder1 = new TitledBorder("");
    titledBorder2 = new TitledBorder("");
    this.setSize(new Dimension(400,300));
    jPanel2.setLayout(flowLayout1);
    jLabel1.setText("Alignment");
    jPanel3.setLayout(flowLayout2);
    jpAlignment.setLayout(borderLayout1);
    jPanel4.setLayout(flowLayout3);
    jLabel3.setText("HGap");
    jLabel4.setText("VGap");
    jpGaps.setLayout(borderLayout2);
    jPanel5.setLayout(flowLayout4);
    jPanel6.setLayout(flowLayout5);
    jpComponents.setLayout(flowLayout);
    
    jcbAlignment.addActionListener(new java.awt.event.ActionListener(){
      public void actionPerformed(ActionEvent e) {
      	jcbAlignment_actionPerformed(e);
      }
    });

    jtfHGap.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jtfHGap_actionPerformed(e);
      }
    });
    jtfVGap.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jtfVGap_actionPerformed(e);
      }
    });
    jpComponents.setBorder(titledBorder1);
    jPanel2.setBorder(titledBorder2);
    titledBorder1.setTitle("Container of FlowLayout");
    titledBorder2.setTitle("FlowLayout Properties");
    this.getContentPane().add(jpComponents, BorderLayout.CENTER);
    this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
    jPanel2.add(jpAlignment, null);
    jpAlignment.add(jPanel3, BorderLayout.WEST);
    jPanel3.add(jLabel1, null);
    jpAlignment.add(jPanel4, BorderLayout.CENTER);
    jPanel4.add(jcbAlignment, null);
    jPanel2.add(jpGaps, null);
    jpGaps.add(jPanel5, BorderLayout.EAST);
    jPanel5.add(jLabel3, null);
    jPanel5.add(jtfHGap, null);
    jpGaps.add(jPanel6, BorderLayout.SOUTH);
    jPanel6.add(jLabel4, null);
    jPanel6.add(jtfVGap, null);

    // Add 15 buttons to jpComponents
    for (int i = 0; i < 15; i++)
      jpComponents.add(new JButton("Component " + i));
  }

  void jcbAlignment_actionPerformed(ActionEvent e) {
  	
  }
  void jtfHGap_actionPerformed(ActionEvent e) {
    int hgap = new Integer(jtfHGap.getText()).intValue();
    flowLayout.setHgap(hgap);
    jpComponents.revalidate();
  }

  void jtfVGap_actionPerformed(ActionEvent e) {
    int vgap = new Integer(jtfVGap.getText()).intValue();
    flowLayout.setVgap(vgap);
    jpComponents.revalidate();
  }

  public static void main(String[] args) {
    Exercise28_1 applet = new Exercise28_1();
    applet.isStandalone = true;
    JFrame frame = new JFrame();
    //EXIT_ON_CLOSE == 3
    frame.setDefaultCloseOperation(3);
    frame.setTitle("Exercise28_1");
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2,
      (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }
}

I figured out how to make the combobox work. Now my only problem is making the text fields bigger. any advice for this?

Here is my almost final code:

// Exercise28_1: Demonstrate FlowLayout properties
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;

public class Exercise28_1 extends JApplet {
  boolean isStandalone = false;
  JPanel jpComponents = new JPanel();
  JPanel jPanel2 = new JPanel();
  JPanel jpAlignment = new JPanel();
  FlowLayout flowLayout1 = new FlowLayout();
  JLabel jLabel1 = new JLabel();
  JPanel jPanel3 = new JPanel();
  FlowLayout flowLayout2 = new FlowLayout();
  JPanel jPanel4 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JComboBox jcbAlignment = new JComboBox(new Object[]
  {"Left", "Center", "Right"});
  FlowLayout flowLayout3 = new FlowLayout();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JPanel jpGaps = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JTextField jtfHGap = new JTextField();
  JTextField jtfVGap = new JTextField();
  FlowLayout flowLayout4 = new FlowLayout();
  FlowLayout flowLayout5 = new FlowLayout();
  JPanel jPanel5 = new JPanel();
  JPanel jPanel6 = new JPanel();
  FlowLayout flowLayout = new FlowLayout();
  TitledBorder titledBorder1;
  TitledBorder titledBorder2;

  //Construct the applet
  public Exercise28_1() {
    titledBorder1 = new TitledBorder("");
    titledBorder2 = new TitledBorder("");
    this.setSize(new Dimension(400,300));
    jPanel2.setLayout(flowLayout1);
    jLabel1.setText("Alignment");
    jPanel3.setLayout(flowLayout2);
    jpAlignment.setLayout(borderLayout1);
    jPanel4.setLayout(flowLayout3);
    jLabel3.setText("HGap");
    jLabel4.setText("VGap");
    jpGaps.setLayout(borderLayout2);
    jPanel5.setLayout(flowLayout4);
    jPanel6.setLayout(flowLayout5);
    jpComponents.setLayout(flowLayout);
    
    jcbAlignment.addActionListener(new java.awt.event.ActionListener(){
      public void actionPerformed(ActionEvent e) {
      	jcbAlignment_actionPerformed(e);
      }
    });

    jtfHGap.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jtfHGap_actionPerformed(e);
      }
    });
    jtfVGap.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jtfVGap_actionPerformed(e);
      }
    });
    jpComponents.setBorder(titledBorder1);
    jPanel2.setBorder(titledBorder2);
    titledBorder1.setTitle("Container of FlowLayout");
    titledBorder2.setTitle("FlowLayout Properties");
    this.getContentPane().add(jpComponents, BorderLayout.CENTER);
    this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
    jPanel2.add(jpAlignment, null);
    jpAlignment.add(jPanel3, BorderLayout.WEST);
    jPanel3.add(jLabel1, null);
    jpAlignment.add(jPanel4, BorderLayout.CENTER);
    jPanel4.add(jcbAlignment, null);
    jPanel2.add(jpGaps, null);
    jpGaps.add(jPanel5, BorderLayout.EAST);
    jPanel5.add(jLabel3, null);
    jPanel5.add(jtfHGap, null);
    jpGaps.add(jPanel6, BorderLayout.SOUTH);
    jPanel6.add(jLabel4, null);
    jPanel6.add(jtfVGap, null);

    // Add 15 buttons to jpComponents
    for (int i = 0; i < 15; i++)
      jpComponents.add(new JButton("Component " + i));
  }

  void jcbAlignment_actionPerformed(ActionEvent e) {
  	int alignment = new Integer(jcbAlignment.getSelectedIndex()).intValue();
  	flowLayout.setAlignment(alignment);
  	jpComponents.revalidate();
  }
  void jtfHGap_actionPerformed(ActionEvent e) {
    int hgap = new Integer(jtfHGap.getText()).intValue();
    flowLayout.setHgap(hgap);
    jpComponents.revalidate();
  }

  void jtfVGap_actionPerformed(ActionEvent e) {
    int vgap = new Integer(jtfVGap.getText()).intValue();
    flowLayout.setVgap(vgap);
    jpComponents.revalidate();
  }

  public static void main(String[] args) {
    Exercise28_1 applet = new Exercise28_1();
    applet.isStandalone = true;
    JFrame frame = new JFrame();
    //EXIT_ON_CLOSE == 3
    frame.setDefaultCloseOperation(3);
    frame.setTitle("Exercise28_1");
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2,
      (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }
}

JTextField jtfVGap = new JTextField(40)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.