User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,918 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,210 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 276 | Replies: 3 | Solved
Reply
Join Date: Jul 2008
Posts: 3
Reputation: jimbobjoe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jimbobjoe jimbobjoe is offline Offline
Newbie Poster

JComboBox problems

  #1  
Jul 24th, 2008
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:

  1. // Exercise28_1: Demonstrate FlowLayout properties
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7.  
  8. public class Exercise28_1 extends JApplet {
  9. boolean isStandalone = false;
  10. JPanel jpComponents = new JPanel();
  11. JPanel jPanel2 = new JPanel();
  12. JPanel jpAlignment = new JPanel();
  13. FlowLayout flowLayout1 = new FlowLayout();
  14. JLabel jLabel1 = new JLabel();
  15. JPanel jPanel3 = new JPanel();
  16. FlowLayout flowLayout2 = new FlowLayout();
  17. JPanel jPanel4 = new JPanel();
  18. BorderLayout borderLayout1 = new BorderLayout();
  19. JComboBox jtfAlignment = new JComboBox(new Object[]
  20. {"Left", "Center", "Right"});
  21. FlowLayout flowLayout3 = new FlowLayout();
  22. JLabel jLabel3 = new JLabel();
  23. JLabel jLabel4 = new JLabel();
  24. JPanel jpGaps = new JPanel();
  25. BorderLayout borderLayout2 = new BorderLayout();
  26. JTextField jtfHGap = new JTextField();
  27. JTextField jtfVGap = new JTextField();
  28. FlowLayout flowLayout4 = new FlowLayout();
  29. FlowLayout flowLayout5 = new FlowLayout();
  30. JPanel jPanel5 = new JPanel();
  31. JPanel jPanel6 = new JPanel();
  32. FlowLayout flowLayout = new FlowLayout();
  33. TitledBorder titledBorder1;
  34. TitledBorder titledBorder2;
  35.  
  36. //Construct the applet
  37. public Exercise28_1() {
  38. titledBorder1 = new TitledBorder("");
  39. titledBorder2 = new TitledBorder("");
  40. this.setSize(new Dimension(400,300));
  41. jPanel2.setLayout(flowLayout1);
  42. jLabel1.setText("Alignment");
  43. jPanel3.setLayout(flowLayout2);
  44. jpAlignment.setLayout(borderLayout1);
  45. jPanel4.setLayout(flowLayout3);
  46. jLabel3.setText("HGap");
  47. jLabel4.setText("VGap");
  48. jpGaps.setLayout(borderLayout2);
  49. jPanel5.setLayout(flowLayout4);
  50. jPanel6.setLayout(flowLayout5);
  51. jpComponents.setLayout(flowLayout);
  52.  
  53. jtfHGap.addActionListener(new java.awt.event.ActionListener() {
  54. public void actionPerformed(ActionEvent e) {
  55. jtfHGap_actionPerformed(e);
  56. }
  57. });
  58. jtfVGap.addActionListener(new java.awt.event.ActionListener() {
  59. public void actionPerformed(ActionEvent e) {
  60. jtfVGap_actionPerformed(e);
  61. }
  62. });
  63. jpComponents.setBorder(titledBorder1);
  64. jPanel2.setBorder(titledBorder2);
  65. titledBorder1.setTitle("Container of FlowLayout");
  66. titledBorder2.setTitle("FlowLayout Properties");
  67. this.getContentPane().add(jpComponents, BorderLayout.CENTER);
  68. this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
  69. jPanel2.add(jpAlignment, null);
  70. jpAlignment.add(jPanel3, BorderLayout.WEST);
  71. jPanel3.add(jLabel1, null);
  72. jpAlignment.add(jPanel4, BorderLayout.CENTER);
  73. jPanel4.add(jtfAlignment, null);
  74. jPanel2.add(jpGaps, null);
  75. jpGaps.add(jPanel5, BorderLayout.WEST);
  76. jPanel5.add(jLabel3, null);
  77. jPanel5.add(jLabel4, null);
  78. jpGaps.add(jPanel6, BorderLayout.CENTER);
  79. jPanel6.add(jtfHGap, null);
  80. jPanel6.add(jtfVGap, null);
  81.  
  82. // Add 15 buttons to jpComponents
  83. for (int i = 0; i < 15; i++)
  84. jpComponents.add(new JButton("Component " + i));
  85. }
  86.  
  87. void jtfHGap_actionPerformed(ActionEvent e) {
  88. int hgap = new Integer(jtfHGap.getText()).intValue();
  89. flowLayout.setHgap(hgap);
  90. jpComponents.revalidate();
  91. }
  92.  
  93. void jtfVGap_actionPerformed(ActionEvent e) {
  94. int vgap = new Integer(jtfVGap.getText()).intValue();
  95. flowLayout.setVgap(vgap);
  96. jpComponents.revalidate();
  97. }
  98.  
  99. public static void main(String[] args) {
  100. Exercise28_1 applet = new Exercise28_1();
  101. applet.isStandalone = true;
  102. JFrame frame = new JFrame();
  103. //EXIT_ON_CLOSE == 3
  104. frame.setDefaultCloseOperation(3);
  105. frame.setTitle("Exercise28_1");
  106. frame.getContentPane().add(applet, BorderLayout.CENTER);
  107. applet.init();
  108. applet.start();
  109. frame.setSize(400,320);
  110. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  111. frame.setLocation((d.width - frame.getSize().width) / 2,
  112. (d.height - frame.getSize().height) / 2);
  113. frame.setVisible(true);
  114. }
  115. }
Last edited by jimbobjoe : Jul 24th, 2008 at 9:57 pm. Reason: syntax highlighting
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: jimbobjoe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jimbobjoe jimbobjoe is offline Offline
Newbie Poster

Re: JComboBox problems

  #2  
Jul 24th, 2008
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:

  1. // Exercise28_1: Demonstrate FlowLayout properties
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7.  
  8. public class Exercise28_1 extends JApplet {
  9. boolean isStandalone = false;
  10. JPanel jpComponents = new JPanel();
  11. JPanel jPanel2 = new JPanel();
  12. JPanel jpAlignment = new JPanel();
  13. FlowLayout flowLayout1 = new FlowLayout();
  14. JLabel jLabel1 = new JLabel();
  15. JPanel jPanel3 = new JPanel();
  16. FlowLayout flowLayout2 = new FlowLayout();
  17. JPanel jPanel4 = new JPanel();
  18. BorderLayout borderLayout1 = new BorderLayout();
  19. JComboBox jcbAlignment = new JComboBox(new Object[]
  20. {"Left", "Center", "Right"});
  21. FlowLayout flowLayout3 = new FlowLayout();
  22. JLabel jLabel3 = new JLabel();
  23. JLabel jLabel4 = new JLabel();
  24. JPanel jpGaps = new JPanel();
  25. BorderLayout borderLayout2 = new BorderLayout();
  26. JTextField jtfHGap = new JTextField();
  27. JTextField jtfVGap = new JTextField();
  28. FlowLayout flowLayout4 = new FlowLayout();
  29. FlowLayout flowLayout5 = new FlowLayout();
  30. JPanel jPanel5 = new JPanel();
  31. JPanel jPanel6 = new JPanel();
  32. FlowLayout flowLayout = new FlowLayout();
  33. TitledBorder titledBorder1;
  34. TitledBorder titledBorder2;
  35.  
  36. //Construct the applet
  37. public Exercise28_1() {
  38. titledBorder1 = new TitledBorder("");
  39. titledBorder2 = new TitledBorder("");
  40. this.setSize(new Dimension(400,300));
  41. jPanel2.setLayout(flowLayout1);
  42. jLabel1.setText("Alignment");
  43. jPanel3.setLayout(flowLayout2);
  44. jpAlignment.setLayout(borderLayout1);
  45. jPanel4.setLayout(flowLayout3);
  46. jLabel3.setText("HGap");
  47. jLabel4.setText("VGap");
  48. jpGaps.setLayout(borderLayout2);
  49. jPanel5.setLayout(flowLayout4);
  50. jPanel6.setLayout(flowLayout5);
  51. jpComponents.setLayout(flowLayout);
  52.  
  53. jcbAlignment.addActionListener(new java.awt.event.ActionListener(){
  54. public void actionPerformed(ActionEvent e) {
  55. jcbAlignment_actionPerformed(e);
  56. }
  57. });
  58.  
  59. jtfHGap.addActionListener(new java.awt.event.ActionListener() {
  60. public void actionPerformed(ActionEvent e) {
  61. jtfHGap_actionPerformed(e);
  62. }
  63. });
  64. jtfVGap.addActionListener(new java.awt.event.ActionListener() {
  65. public void actionPerformed(ActionEvent e) {
  66. jtfVGap_actionPerformed(e);
  67. }
  68. });
  69. jpComponents.setBorder(titledBorder1);
  70. jPanel2.setBorder(titledBorder2);
  71. titledBorder1.setTitle("Container of FlowLayout");
  72. titledBorder2.setTitle("FlowLayout Properties");
  73. this.getContentPane().add(jpComponents, BorderLayout.CENTER);
  74. this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
  75. jPanel2.add(jpAlignment, null);
  76. jpAlignment.add(jPanel3, BorderLayout.WEST);
  77. jPanel3.add(jLabel1, null);
  78. jpAlignment.add(jPanel4, BorderLayout.CENTER);
  79. jPanel4.add(jcbAlignment, null);
  80. jPanel2.add(jpGaps, null);
  81. jpGaps.add(jPanel5, BorderLayout.EAST);
  82. jPanel5.add(jLabel3, null);
  83. jPanel5.add(jtfHGap, null);
  84. jpGaps.add(jPanel6, BorderLayout.SOUTH);
  85. jPanel6.add(jLabel4, null);
  86. jPanel6.add(jtfVGap, null);
  87.  
  88. // Add 15 buttons to jpComponents
  89. for (int i = 0; i < 15; i++)
  90. jpComponents.add(new JButton("Component " + i));
  91. }
  92.  
  93. void jcbAlignment_actionPerformed(ActionEvent e) {
  94.  
  95. }
  96. void jtfHGap_actionPerformed(ActionEvent e) {
  97. int hgap = new Integer(jtfHGap.getText()).intValue();
  98. flowLayout.setHgap(hgap);
  99. jpComponents.revalidate();
  100. }
  101.  
  102. void jtfVGap_actionPerformed(ActionEvent e) {
  103. int vgap = new Integer(jtfVGap.getText()).intValue();
  104. flowLayout.setVgap(vgap);
  105. jpComponents.revalidate();
  106. }
  107.  
  108. public static void main(String[] args) {
  109. Exercise28_1 applet = new Exercise28_1();
  110. applet.isStandalone = true;
  111. JFrame frame = new JFrame();
  112. //EXIT_ON_CLOSE == 3
  113. frame.setDefaultCloseOperation(3);
  114. frame.setTitle("Exercise28_1");
  115. frame.getContentPane().add(applet, BorderLayout.CENTER);
  116. applet.init();
  117. applet.start();
  118. frame.setSize(400,320);
  119. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  120. frame.setLocation((d.width - frame.getSize().width) / 2,
  121. (d.height - frame.getSize().height) / 2);
  122. frame.setVisible(true);
  123. }
  124. }
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: jimbobjoe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jimbobjoe jimbobjoe is offline Offline
Newbie Poster
  #3  
Jul 24th, 2008
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:

  1. // Exercise28_1: Demonstrate FlowLayout properties
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7.  
  8. public class Exercise28_1 extends JApplet {
  9. boolean isStandalone = false;
  10. JPanel jpComponents = new JPanel();
  11. JPanel jPanel2 = new JPanel();
  12. JPanel jpAlignment = new JPanel();
  13. FlowLayout flowLayout1 = new FlowLayout();
  14. JLabel jLabel1 = new JLabel();
  15. JPanel jPanel3 = new JPanel();
  16. FlowLayout flowLayout2 = new FlowLayout();
  17. JPanel jPanel4 = new JPanel();
  18. BorderLayout borderLayout1 = new BorderLayout();
  19. JComboBox jcbAlignment = new JComboBox(new Object[]
  20. {"Left", "Center", "Right"});
  21. FlowLayout flowLayout3 = new FlowLayout();
  22. JLabel jLabel3 = new JLabel();
  23. JLabel jLabel4 = new JLabel();
  24. JPanel jpGaps = new JPanel();
  25. BorderLayout borderLayout2 = new BorderLayout();
  26. JTextField jtfHGap = new JTextField();
  27. JTextField jtfVGap = new JTextField();
  28. FlowLayout flowLayout4 = new FlowLayout();
  29. FlowLayout flowLayout5 = new FlowLayout();
  30. JPanel jPanel5 = new JPanel();
  31. JPanel jPanel6 = new JPanel();
  32. FlowLayout flowLayout = new FlowLayout();
  33. TitledBorder titledBorder1;
  34. TitledBorder titledBorder2;
  35.  
  36. //Construct the applet
  37. public Exercise28_1() {
  38. titledBorder1 = new TitledBorder("");
  39. titledBorder2 = new TitledBorder("");
  40. this.setSize(new Dimension(400,300));
  41. jPanel2.setLayout(flowLayout1);
  42. jLabel1.setText("Alignment");
  43. jPanel3.setLayout(flowLayout2);
  44. jpAlignment.setLayout(borderLayout1);
  45. jPanel4.setLayout(flowLayout3);
  46. jLabel3.setText("HGap");
  47. jLabel4.setText("VGap");
  48. jpGaps.setLayout(borderLayout2);
  49. jPanel5.setLayout(flowLayout4);
  50. jPanel6.setLayout(flowLayout5);
  51. jpComponents.setLayout(flowLayout);
  52.  
  53. jcbAlignment.addActionListener(new java.awt.event.ActionListener(){
  54. public void actionPerformed(ActionEvent e) {
  55. jcbAlignment_actionPerformed(e);
  56. }
  57. });
  58.  
  59. jtfHGap.addActionListener(new java.awt.event.ActionListener() {
  60. public void actionPerformed(ActionEvent e) {
  61. jtfHGap_actionPerformed(e);
  62. }
  63. });
  64. jtfVGap.addActionListener(new java.awt.event.ActionListener() {
  65. public void actionPerformed(ActionEvent e) {
  66. jtfVGap_actionPerformed(e);
  67. }
  68. });
  69. jpComponents.setBorder(titledBorder1);
  70. jPanel2.setBorder(titledBorder2);
  71. titledBorder1.setTitle("Container of FlowLayout");
  72. titledBorder2.setTitle("FlowLayout Properties");
  73. this.getContentPane().add(jpComponents, BorderLayout.CENTER);
  74. this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
  75. jPanel2.add(jpAlignment, null);
  76. jpAlignment.add(jPanel3, BorderLayout.WEST);
  77. jPanel3.add(jLabel1, null);
  78. jpAlignment.add(jPanel4, BorderLayout.CENTER);
  79. jPanel4.add(jcbAlignment, null);
  80. jPanel2.add(jpGaps, null);
  81. jpGaps.add(jPanel5, BorderLayout.EAST);
  82. jPanel5.add(jLabel3, null);
  83. jPanel5.add(jtfHGap, null);
  84. jpGaps.add(jPanel6, BorderLayout.SOUTH);
  85. jPanel6.add(jLabel4, null);
  86. jPanel6.add(jtfVGap, null);
  87.  
  88. // Add 15 buttons to jpComponents
  89. for (int i = 0; i < 15; i++)
  90. jpComponents.add(new JButton("Component " + i));
  91. }
  92.  
  93. void jcbAlignment_actionPerformed(ActionEvent e) {
  94. int alignment = new Integer(jcbAlignment.getSelectedIndex()).intValue();
  95. flowLayout.setAlignment(alignment);
  96. jpComponents.revalidate();
  97. }
  98. void jtfHGap_actionPerformed(ActionEvent e) {
  99. int hgap = new Integer(jtfHGap.getText()).intValue();
  100. flowLayout.setHgap(hgap);
  101. jpComponents.revalidate();
  102. }
  103.  
  104. void jtfVGap_actionPerformed(ActionEvent e) {
  105. int vgap = new Integer(jtfVGap.getText()).intValue();
  106. flowLayout.setVgap(vgap);
  107. jpComponents.revalidate();
  108. }
  109.  
  110. public static void main(String[] args) {
  111. Exercise28_1 applet = new Exercise28_1();
  112. applet.isStandalone = true;
  113. JFrame frame = new JFrame();
  114. //EXIT_ON_CLOSE == 3
  115. frame.setDefaultCloseOperation(3);
  116. frame.setTitle("Exercise28_1");
  117. frame.getContentPane().add(applet, BorderLayout.CENTER);
  118. applet.init();
  119. applet.start();
  120. frame.setSize(400,320);
  121. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  122. frame.setLocation((d.width - frame.getSize().width) / 2,
  123. (d.height - frame.getSize().height) / 2);
  124. frame.setVisible(true);
  125. }
  126. }
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 476
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 49
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro in Training

Re: JComboBox problems

  #4  
Jul 25th, 2008
JTextField jtfVGap = new JTextField(40)
I AM the 12th CYLON
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:10 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC