943,732 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4425
  • Java RSS
Oct 29th, 2008
0

Using user input to change background color of JPanel

Expand Post »
How do you to take user input for red, green and blue(a number between 0 and 255) and make the background of the center JPanel that color.

Heres the code on what ive done so far:
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5.  
  6. /**
  7.  * Write a description of class ColorTestApplet here.
  8.  *
  9.  * @author (your name)
  10.  * @version (a version number or a date)
  11.  */
  12. public class ColorTestApplet extends JApplet
  13. {
  14. public void init()
  15. {
  16. this.setLayout(new BorderLayout());
  17.  
  18. //Declaring panels
  19. centerPanel = new JPanel();
  20. northPanel = new JPanel();
  21. southPanel = new JPanel();
  22.  
  23. //centerPanel.setLayout(new GridLayout());
  24. this.add(centerPanel, BorderLayout.CENTER);
  25. this.add(northPanel, BorderLayout.NORTH);
  26. this.add(southPanel, BorderLayout.SOUTH);
  27.  
  28. JLabel red = new JLabel("Red:");
  29. JLabel green = new JLabel("Green:");
  30. JLabel blue = new JLabel("Blue:");
  31.  
  32. redText = new JTextField("0", 5);
  33. greenText = new JTextField("0", 5);
  34. blueText = new JTextField("0", 5);
  35. redText.setHorizontalAlignment(redText.RIGHT);
  36. greenText.setHorizontalAlignment(redText.RIGHT);
  37. blueText.setHorizontalAlignment(redText.RIGHT);
  38.  
  39. JButton show = new JButton("Show");
  40.  
  41. JLabel name = new JLabel("Kimberlie Davis");
  42.  
  43. northPanel.add(red);
  44. northPanel.add(redText);
  45. northPanel.add(green);
  46. northPanel.add(greenText);
  47. northPanel.add(blue);
  48. northPanel.add(blueText);
  49. southPanel.add(show);
  50. centerPanel.add(name);
  51.  
  52. ShowAction showAction = new ShowAction();
  53. show.addActionListener(showAction);
  54. }
  55.  
  56. class ShowAction implements ActionListener
  57. {
  58. public void actionPerformed(ActionEvent e)
  59. {
  60. int red, green, blue;
  61. String color;
  62. try
  63. {
  64. red = Integer.parseInt(redText.getText());
  65. green = Integer.parseInt(greenText.getText());
  66. blue = Integer.parseInt(blueText.getText());
  67. if(red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0)
  68. {
  69. JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 255!");
  70. redText.setText("0");
  71. blueText.setText("0");
  72. greenText.setText("0");
  73. }
  74. else
  75. {
  76. color = "" + red + green + blue;
  77. JOptionPane.showMessageDialog(null, color);
  78. //centerPanel.setBackground(red, blue, green);
  79. }
  80. //centerpanel.setBackground(Color.color);
  81. }
  82. catch(NumberFormatException exception)
  83. {
  84. JOptionPane.showMessageDialog(null, "Please enter a number!");
  85. redText.setText("0");
  86. blueText.setText("0");
  87. greenText.setText("0");
  88. }
  89. }
  90. }
  91.  
  92. JPanel centerPanel;
  93. JPanel northPanel;
  94. JPanel southPanel;
  95.  
  96. JTextField redText;
  97. JTextField greenText;
  98. JTextField blueText;
  99. }
Similar Threads
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Oct 29th, 2008
0

Re: Using user input to change background color of JPanel

Do you have to do it that way? I think a much more interesting way would be to use ColorChooser
Last edited by jasimp; Oct 29th, 2008 at 9:21 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Oct 29th, 2008
0

Re: Using user input to change background color of JPanel

I do have to have the user input numbers for red green and blue. Its for a school assignment.
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Oct 29th, 2008
1

Re: Using user input to change background color of JPanel

Just create a new color using your values. I added the line of code into your try statement.

java Syntax (Toggle Plain Text)
  1. try
  2. {
  3. red = Integer.parseInt(redText.getText());
  4. green = Integer.parseInt(greenText.getText());
  5. blue = Integer.parseInt(blueText.getText());
  6. if(red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0)
  7. {
  8. JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 255!");
  9. redText.setText("0");
  10. blueText.setText("0");
  11. greenText.setText("0");
  12. }
  13. else
  14. {
  15. //creates a new color using the values inputed by the user
  16. Color colorTest = new Color(red,green,blue);
  17. color = "" + red + green + blue;
  18. JOptionPane.showMessageDialog(null, color);
  19. //sets the color to the one created from user inputted values
  20. centerPanel.setBackground(colorTest);
  21.  
  22. }
  23.  
  24. }
Last edited by jasimp; Oct 29th, 2008 at 9:24 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Oct 29th, 2008
1

Re: Using user input to change background color of JPanel

ok thank you very much!
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: AWT or Swing?
Next Thread in Java Forum Timeline: Run Time Stack





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC