Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Refreshing a JTabbedPane?

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Refreshing a JTabbedPane?

    Alright, yet another question. Below I have a sample of a JTabbedPane I've been working on.
    Every time the user clicks the sword icon it counts up.
    I used this code to make it update the count displayed when it detects its been clicked:
    swordCount.setText("Sword Count: " + count);
    So basically I'm lazy and want to know if there is a different way to refresh a tab?
    I thought these might do the trick:
    panel1.revalidate();
    panel1.repaint();
    and I've included them in the code below.
    But alas they do nothing. I'm not really sure what they are suppose to do anyway.
    Any suggestions?
    Thanks.


    //25.13 1039
    import java.awt.*;
    import java.awt.event.*;
    import java.net.MalformedURLException;
    import java.net.URL;
     
    import javax.swing.*;
     
    public class CCD  extends JDialog 
    {
    	public int count = 0;
     
    	private JButton fancyJButton;
     
    	public JLabel swordCount = new JLabel("Sword Count: " + count, SwingConstants.CENTER);;
     
    	JPanel panel1 = new JPanel(); // create first panel
     
    	// set up GUI
    	public CCD(Frame owner, String title, boolean modal)
     
    	{
    		 super(owner, title, modal);
     
    		 System.out.println("In Command Center");
     
    		JTabbedPane tabbedPane = new JTabbedPane(); // create JTabbedPane
     
    		// set up panel1 and add it to JTabbedPane
    		JLabel label1 = new JLabel("panel one ", SwingConstants.CENTER);
     
    		panel1.add(label1); // add label to panel
    		tabbedPane.addTab("Tab One", null, panel1, "First Panel");
     
    		// creates plain button and adds it to panel1
    		JButton plainJButton = new JButton("Plain Button"); // button with text
    		panel1.add(plainJButton);
     
    		// creates a fancy button and adds it to panel1
    		Icon sword1 = new ImageIcon(("http://oi52.tinypic.com/307rt36.jpg"));
    		Icon sword2 = new ImageIcon(("http://i55.tinypic.com/2u9780i.jpg"));
     
    		try
    		{
    			sword1 = new ImageIcon(new URL(
    					"http://oi52.tinypic.com/307rt36.jpg"));
    		} catch (MalformedURLException e)
    		{
    			e.printStackTrace();
    		}
    		try
    		{
    			sword2 = new ImageIcon(new URL(
    					"http://i55.tinypic.com/2u9780i.jpg"));
    		} catch (MalformedURLException e)
    		{
    			e.printStackTrace();
    		}
    		fancyJButton = new JButton("Sword Button", sword1); // set image
    		fancyJButton.setRolloverIcon(sword2); // set rollover image
    		panel1.add(fancyJButton); // add fancyJbutton
     
     
    		panel1.add(swordCount); // add label to panel
     
    		// create new BUttonHandler for button event handling
    		ButtonHandler handler = new ButtonHandler();
    		plainJButton.addActionListener(handler);
    		fancyJButton.addActionListener(handler);
     
    		// set up panel2 and it to JTabbedPane
    		JLabel label2 = new JLabel("panel two", SwingConstants.CENTER);
    		JPanel panel2 = new JPanel(); // create second panel
    		panel2.setBackground(Color.YELLOW); // set background color to yellow
    		panel2.add(label2); // add label to panel 2
    		tabbedPane.addTab("Tab Two", null, panel2, "Second Panel");
     
    		// set up panel3 and add it to JTabbedPane
    		JLabel label3 = new JLabel("panel three");
    		JPanel panel3 = new JPanel(); // create third panel
    		panel3.setLayout(new BorderLayout());// use border layout
    		panel3.add(new JButton("North"), BorderLayout.NORTH);
    		panel3.add(new JButton("West"), BorderLayout.WEST);
    		panel3.add(new JButton("East"), BorderLayout.EAST);
    		panel3.add(new JButton("South"), BorderLayout.SOUTH);
    		panel3.setBackground(Color.BLACK); // set background color to black
    		panel3.add(label3, BorderLayout.CENTER);
    		tabbedPane.addTab("Tab THree", null, panel3, "Third Panel");
     
    		add(tabbedPane); // add JTabbedPane to frame
     
    	}// end public JTabbedPaneFrame
     
    	// inner class forbutton event handling
    	private class ButtonHandler implements ActionListener
    	{
    		// handle button event
    		public void actionPerformed(ActionEvent event)
    		{
    			JOptionPane.showMessageDialog(CCD.this, String.format(
    					"You pressed %s", event.getActionCommand()));
     
    			if (event.getSource() == fancyJButton)
    			{
    				/*
    				 * JOptionPane.showMessageDialog(JTabbedPaneFrame.this,
    				 * String.format( "You pressed %s", event.getActionCommand()));
    				 */
    				count++;
    				System.out.printf("The sword has been clicked: %d\n", count);
     
     
    				panel1.revalidate();  
    				panel1.repaint();  
    				swordCount.setText("Sword Count: " + count);
     
    			}
    		}
    	}
    }// end class JTabbedPaneFrame

    For Testing.
    //25.14 1039
    import javax.swing.JFrame;
     
    public class JTabbedPaneDemo
    {
     
    	public static void main(String[] args)
    	{
     
    		CCD tabbedPaneFrame = new CCD(null, "Dialog Frame", true);
    		tabbedPaneFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		tabbedPaneFrame.setSize(250, 200); // set frame size
    		tabbedPaneFrame.setLocationRelativeTo(null);
    		tabbedPaneFrame.setVisible(true); // display frame
     
    	}// end main
     
    }// end class JTabbedPaneDemo


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Refreshing a JTabbedPane?

    int x  = 5;
    int y = 2 * x;
    System.out.println(y);
    x = 10;
    System.out.println(y);
    The output of the above code is 10 & 10 not 10 & 20. Just because x is changed y is not automagically updated.
    public JLabel swordCount = new JLabel("Sword Count: " + count, SwingConstants.CENTER);;
    If I understand correctly you are expecting the same thing with your code. Just because the value of count changes it does not mean your JLabel will automagically be updated too. If you want the JLabel to display something different then you must call the setText method.

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Refreshing a JTabbedPane?

    Quote Originally Posted by Hallowed View Post
    So basically I'm lazy and want to know if there is a different way to refresh a tab?
    I don't get it - you have to write the setText("..." + count) code whatever way you do it, but you only have to write it once - what has being lazy got to do with anything?

    If you want the text to change, you have to change the text...

Similar Threads

  1. refreshing a jtable with database
    By kollyisrealisaac in forum JDBC & Databases
    Replies: 0
    Last Post: May 7th, 2011, 01:50 PM
  2. [SOLVED] How to close all tabs in JTabbedPane?
    By LeonLanford in forum AWT / Java Swing
    Replies: 7
    Last Post: June 28th, 2010, 10:58 AM
  3. Replies: 3
    Last Post: April 14th, 2010, 07:33 PM
  4. refreshing JEditorPane
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: April 9th, 2010, 04:01 AM
  5. traversing multiple jTabbedPane?
    By dewboy3d in forum AWT / Java Swing
    Replies: 3
    Last Post: October 2nd, 2009, 07:26 PM