Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Swing repaint()

  • 13-02-2004 4:38pm
    #1
    Closed Accounts Posts: 364 ✭✭


    hello

    I am in the process of writing a simple GUI application and I have come across a small problem with the Java repaint(); method.

    I have a top level container with 2 panels in it. Panel one has 2 buttons, it is a sidebar on the left hand side of the window. The second panel is the main panel, and when I click on the first button in the side panel, I want buttons to appear in the main panel. When I click on the second button in the panel, I want the main panel to remove the original items and put a JTextArea and JTextfield in there.

    My problem is when I click either button, both the ( textfield and jlabel) appear with the (2 jbuttons). I only want them to appear once. I will include my code below. I have searched on java.sun.com and have not been able to find a good solution. Any help appreciated thanks.
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class NewWindow extends JFrame implements ActionListener 
    {
    	JPanel sideBar = new JPanel();
    	JPanel mainBar = new JPanel();
    
    	public NewWindow()
    	{
    	}
    	
    	public void setMainGui()
    	{
    		Container content = getContentPane();
    		setSize(400, 400);
    		content.setLayout(null);
    
    		sideBar.setBounds(0, 0, 120, 400);
    		sideBar.setBackground(Color.blue);
    		sideBar.setOpaque(true);
    
    		mainBar.setBounds(120, 0, 280, 400);
    		mainBar.setBackground(Color.white);
    		mainBar.setOpaque(true);
    		mainBar.setLayout(null);
    		sideBar.setLayout(null);
    		content.add(sideBar);
    		content.add(mainBar);
    	}
    
    	public void setSideBar()
    	{
    		JButton cust = new JButton("Customers");
    		cust.addActionListener(this);
    		cust.setBounds(5, 5, 100, 25);
    
    		JButton comments = new JButton("Comments");
    		comments.addActionListener(this);
    		comments.setBounds(5, 40, 100, 25);
    		
    		sideBar.add(cust);
    		sideBar.add(comments);
    	}
    	
    	
    	public void setMainBarGuiCustomer()
    	{
    		JButton but1 = new JButton("Matt");
    
    		JButton but2 = new JButton("Ro");
    
    		but1.setBounds(5, 20, 140, 25);
    		but2.setBounds(5, 50, 140, 25);
    
    		mainBar.add(but1);
    		mainBar.add(but2);
    		mainBar.revalidate();
    	}
    
    	public void removeComponents()
    	{
    
    	
    	public void setMainBarGuiComments()
    	{
    		mainBar.setLayout(null);
    		JTextArea com = new JTextArea();
    		com.setBounds(10, 10, 200, 200);
    
    		JLabel info = new JLabel("Comments welcome");
    		info.setBounds(30, 220, 100, 25);
    
    		mainBar.add(com);
    		mainBar.add(info);
    		mainBar.revalidate();
    	}
    	
    	public static void main(String [] args)
    	{
    		NewWindow test = new NewWindow();
    		test.setMainGui();
    		test.setSideBar();
    		test.show();
    
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		String action = e.getActionCommand();
    		if(action == "Customers");
    		{
    			setMainBarGuiCustomer();
    			repaint();
    		}
    		
    		if(action == "Comments");
    		{
    			setMainBarGuiComments();
    			repaint();
    		}	
    	}
    }
    	
    

    Thanks

    Matt


Comments

  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    two ways of doing this i think, the first is adding both the customer and comments panel to a single panel using cardlayout. This single panel would represent the main panel. The buttons in the sidebar would then call the appropiate card.

    The second way would be to remove all components before adding to the panel. use the removeAll method. That remove method you have shoud be causing errors since there's no closing brace.


Advertisement