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