Erasing and redrawing swing components

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 31
Reputation: kinger29 is an unknown quantity at this point 
Solved Threads: 1
kinger29 kinger29 is offline Offline
Light Poster

Erasing and redrawing swing components

 
0
  #1
Oct 26th, 2009
I have created a JApplet and I want to be able to clear the screen and draw new components depending on what button I click on. Right now the code below will clear the screen but it doesn't redraw the JTextFields (label_1 & label_2). However I did notice whenever I resize the applet(when it pops up in eclipse) the JTextFields suddenly reappear. Is there some type of refresh or restart method I need to call to get the components to draw correctly. Any tips or help in the right direction would be greatly appreciated. Here is my code so far.


  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6.  
  7. public class Main_app extends JApplet
  8. {
  9. //Called when this is loaded into the browser.
  10. public static Container content;
  11. //public static JFrame frame;
  12.  
  13.  
  14. public void init() {
  15. //Create the frame and content manager
  16. //frame = new JFrame("Cryptography Algorithms");
  17. content = getContentPane();
  18. setSize(600,600);
  19. content.setBackground(Color.white);
  20. //content.setSize(new Dimension(600,600));
  21.  
  22. //Set the flow for the content
  23. FlowLayout mgr = new FlowLayout();
  24. mgr.setHgap(10);
  25. mgr.setVgap(10);
  26. mgr.setAlignment(FlowLayout.LEFT);
  27. content.setLayout(mgr);
  28.  
  29. //Add buttons to First Page
  30.  
  31. //Vigenere button
  32. JButton vigenere_b = new JButton("Vigenere Cipher");
  33. vigenere_b.addActionListener(new ActionListener()
  34. {
  35. public void actionPerformed(ActionEvent e)
  36. {
  37. vigenere_gui();
  38. }
  39. });
  40. content.add(vigenere_b);
  41.  
  42. //Inverse Matrix
  43. JButton hill_b = new JButton("Hill Cipher");
  44. hill_b.addActionListener(new ActionListener()
  45. {
  46. public void actionPerformed(ActionEvent e)
  47. {
  48. hill_gui();
  49. }
  50. });
  51. content.add(hill_b);
  52. //Reset Button
  53. JButton reset_b = new JButton("Reset");
  54. reset_b.addActionListener(new ActionListener()
  55. {
  56. public void actionPerformed(ActionEvent e)
  57. {
  58. reset_gui();
  59. }
  60. });
  61. content.add(reset_b);
  62. }
  63.  
  64. //
  65. // Clears the applet display
  66. //
  67. public static void reset_gui()
  68. {
  69. content.removeAll();
  70. content.repaint();
  71.  
  72. }
  73.  
  74. //
  75. // Set up display for vigenere cipher
  76. //
  77. public static void vigenere_gui()
  78. {
  79. reset_gui();
  80.  
  81. }
  82.  
  83. //
  84. // Set up display for hill cipher
  85. //
  86. // public static Container hill_container;
  87. public void hill_gui()
  88. {
  89. reset_gui();
  90. content = getContentPane();
  91. JTextField label_1 = new JTextField(70);
  92. label_1.setBorder(BorderFactory.createEmptyBorder());
  93. label_1.setText("Hill Cipher");
  94. content.add(label_1);
  95. JTextField label_2 = new JTextField(70);
  96. label_2.setBorder(BorderFactory.createEmptyBorder());
  97. label_2.setText("Enter the encryption matrix to get the decryption matrix");
  98. content.add(label_2);
  99. content.paint(content.getGraphics());
  100. //content.setVisible(true);
  101.  
  102. }
  103. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #2
Oct 26th, 2009
call validate and then repaint (on the entire JApplet, i.e. just like you're calling getContentPane), rather than that content.paint call.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
1
  #3
Oct 26th, 2009
First init all components
Used BorderLayout and CardLayout
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6.  
  7. public class Main_app extends JApplet {
  8.  
  9. private Container content;
  10. private JPanel centerPanel;
  11. private CardLayout card;
  12.  
  13. public void init() {
  14. content = getContentPane();
  15. setSize(800, 600);
  16. content.setBackground(Color.GREEN);
  17. content.setLayout(new BorderLayout());
  18. JPanel northPanel = new JPanel();
  19. JButton vigenere_b = new JButton("Vigenere Cipher");
  20. vigenere_b.addActionListener(new ActionListener() {
  21.  
  22. public void actionPerformed(ActionEvent e) {
  23. vigenere_gui();
  24. }
  25. });
  26. northPanel.add(vigenere_b);
  27.  
  28. //Inverse Matrix
  29. JButton hill_b = new JButton("Hill Cipher");
  30. hill_b.addActionListener(new ActionListener() {
  31.  
  32. public void actionPerformed(ActionEvent e) {
  33. hill_gui();
  34. }
  35. });
  36. northPanel.add(hill_b);
  37.  
  38. //Reset Button
  39. JButton reset_b = new JButton("Reset");
  40. reset_b.addActionListener(new ActionListener() {
  41.  
  42. public void actionPerformed(ActionEvent e) {
  43. reset_gui();
  44. }
  45. });
  46. northPanel.add(reset_b);
  47. //
  48. content.add(northPanel, BorderLayout.NORTH);
  49. //
  50. centerPanel = new JPanel();
  51. card = new CardLayout();
  52. centerPanel.setLayout(card);
  53. //
  54. JPanel hillPanel = new JPanel();
  55. hillPanel.setBackground(Color.RED);
  56. JTextField label_1 = new JTextField(70);
  57. label_1.setBorder(BorderFactory.createEmptyBorder());
  58. label_1.setText("Hill Cipher");
  59. hillPanel.add(label_1);
  60. JTextField label_2 = new JTextField(70);
  61. label_2.setBorder(BorderFactory.createEmptyBorder());
  62. label_2.setText("Enter the encryption matrix to get the decryption matrix");
  63. hillPanel.add(label_2);
  64.  
  65. // add hillPanel known as "hill"
  66. centerPanel.add("hill", hillPanel);
  67. //
  68. JPanel vigenerePanel = new JPanel();
  69. vigenerePanel.setBackground(Color.BLUE);
  70. vigenerePanel.add(new JButton("button vigenere"));
  71.  
  72. // add vigenerePanel known as "vigenere"
  73. centerPanel.add("vigenere", vigenerePanel);
  74. //
  75. JPanel emptyPanel = new JPanel();
  76. emptyPanel.setBackground(Color.WHITE);
  77.  
  78. // add emptyPanel known as "empty"
  79. centerPanel.add("empty", emptyPanel);
  80.  
  81. content.add(centerPanel, BorderLayout.CENTER);
  82. }
  83.  
  84. public void vigenere_gui() {
  85. card.show(centerPanel, "vigenere");
  86. }
  87.  
  88. public void hill_gui() {
  89. card.show(centerPanel, "hill");
  90. }
  91.  
  92. public void reset_gui() {
  93. card.show(centerPanel, "empty");
  94. }
  95. }
now implement your code
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #4
Oct 26th, 2009
Yes, CardLayout is a good option, but doing other's work for them is not. Simply suggest CardLayout and link to the tutorial for it, or give a "generic" example of it.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #5
Oct 26th, 2009
Masijade, I appreciate your posts and welcome your comments towards me.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC