| | |
Erasing and redrawing swing components
![]() |
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 1
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.
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Main_app extends JApplet { //Called when this is loaded into the browser. public static Container content; //public static JFrame frame; public void init() { //Create the frame and content manager //frame = new JFrame("Cryptography Algorithms"); content = getContentPane(); setSize(600,600); content.setBackground(Color.white); //content.setSize(new Dimension(600,600)); //Set the flow for the content FlowLayout mgr = new FlowLayout(); mgr.setHgap(10); mgr.setVgap(10); mgr.setAlignment(FlowLayout.LEFT); content.setLayout(mgr); //Add buttons to First Page //Vigenere button JButton vigenere_b = new JButton("Vigenere Cipher"); vigenere_b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { vigenere_gui(); } }); content.add(vigenere_b); //Inverse Matrix JButton hill_b = new JButton("Hill Cipher"); hill_b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { hill_gui(); } }); content.add(hill_b); //Reset Button JButton reset_b = new JButton("Reset"); reset_b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reset_gui(); } }); content.add(reset_b); } // // Clears the applet display // public static void reset_gui() { content.removeAll(); content.repaint(); } // // Set up display for vigenere cipher // public static void vigenere_gui() { reset_gui(); } // // Set up display for hill cipher // // public static Container hill_container; public void hill_gui() { reset_gui(); content = getContentPane(); JTextField label_1 = new JTextField(70); label_1.setBorder(BorderFactory.createEmptyBorder()); label_1.setText("Hill Cipher"); content.add(label_1); JTextField label_2 = new JTextField(70); label_2.setBorder(BorderFactory.createEmptyBorder()); label_2.setText("Enter the encryption matrix to get the decryption matrix"); content.add(label_2); content.paint(content.getGraphics()); //content.setVisible(true); } }
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
----------------------------------------------
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
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
1
#3 Oct 26th, 2009
First init all components
Used BorderLayout and CardLayout
now implement your code
Used BorderLayout and CardLayout
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Main_app extends JApplet { private Container content; private JPanel centerPanel; private CardLayout card; public void init() { content = getContentPane(); setSize(800, 600); content.setBackground(Color.GREEN); content.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); JButton vigenere_b = new JButton("Vigenere Cipher"); vigenere_b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { vigenere_gui(); } }); northPanel.add(vigenere_b); //Inverse Matrix JButton hill_b = new JButton("Hill Cipher"); hill_b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { hill_gui(); } }); northPanel.add(hill_b); //Reset Button JButton reset_b = new JButton("Reset"); reset_b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reset_gui(); } }); northPanel.add(reset_b); // content.add(northPanel, BorderLayout.NORTH); // centerPanel = new JPanel(); card = new CardLayout(); centerPanel.setLayout(card); // JPanel hillPanel = new JPanel(); hillPanel.setBackground(Color.RED); JTextField label_1 = new JTextField(70); label_1.setBorder(BorderFactory.createEmptyBorder()); label_1.setText("Hill Cipher"); hillPanel.add(label_1); JTextField label_2 = new JTextField(70); label_2.setBorder(BorderFactory.createEmptyBorder()); label_2.setText("Enter the encryption matrix to get the decryption matrix"); hillPanel.add(label_2); // add hillPanel known as "hill" centerPanel.add("hill", hillPanel); // JPanel vigenerePanel = new JPanel(); vigenerePanel.setBackground(Color.BLUE); vigenerePanel.add(new JButton("button vigenere")); // add vigenerePanel known as "vigenere" centerPanel.add("vigenere", vigenerePanel); // JPanel emptyPanel = new JPanel(); emptyPanel.setBackground(Color.WHITE); // add emptyPanel known as "empty" centerPanel.add("empty", emptyPanel); content.add(centerPanel, BorderLayout.CENTER); } public void vigenere_gui() { card.show(centerPanel, "vigenere"); } public void hill_gui() { card.show(centerPanel, "hill"); } public void reset_gui() { card.show(centerPanel, "empty"); } }
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
----------------------------------------------
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
![]() |
Similar Threads
- Swing and threads (Java)
- how to relocate swing component automatically??? (Java)
- displaying HTML Text into the jEditorPane in swing (Java)
- Swing Components (Java)
- java calendar SWING (Java)
- AWT versus Swing (Java)
- Copy and paste Components (Java)
- Displaying GUI components relating to FloatControls (Java Sound) (Java)
Other Threads in the Java Forum
- Previous Thread: Stumped on a project
- Next Thread: . Class expected error need help
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt threads tree web websites windows






