help with tabbed pane - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help with tabbed pane

-Necro--Necro- Posts: 5Member
hello!

i have a tabbed pane with two tabs.
in tab1 i have a button(for example), how can i do that when i press the button in tab1 it will draw something in tab2?

i tried to do this but it draw what i want in tab1(the tab with the button)

if the source code will help i will post it here

sorry for my bad english

Comments

  • moomoo Posts: 147Member
    >> hello!
    >>
    >> i have a tabbed pane with two tabs.
    >> in tab1 i have a button(for example), how can i do that when i press
    >> the button in tab1 it will draw something in tab2?
    >>

    hi,
    first i know it's stupid to answer a question with a question, but what do you intend to do? i'm not really sure if it is possible to really draw on a hidden tab of a tabbed pane, but it's definately possible to modify the hidden tab so the next time you switch to it, it will appear changed. i wrote a litte something for you. if you modify the actionPerformed(ActionEvent) method (remove the comment from the last two lines) you can see what actually happens if you try to do a paint on the hidden tab. it will overwrite the current view.

    ok, i hope this'll help you out a bit

    [code]
    /**
    * tabpane demo
    */
    public class Tab implements ActionListener {
    /**
    * just a JPanel, that paints itself a little bit differently
    */
    private class MyPanel extends JPanel {
    /**
    * save the color to paint the Panel
    */
    private int color;

    /**
    * just paint a rectangle in random color
    */
    public void paint(Graphics g) {
    //set the background the bitwise inverse of foreground
    setBackground(new Color(~color));
    setForeground(new Color(color));

    super.paint(g);
    g.draw3DRect(10, 10, 200, 200, true);
    }
    }

    /**
    * the frame for this demo
    */
    private JFrame theFrame;

    /**
    * this constructor creates the demo and makes it visible (main work is
    * here)
    */
    public Tab() {
    //create frame and its content pane
    theFrame = new JFrame();
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());

    //create the tabPane and add two tabs
    JTabbedPane tabPane = new JTabbedPane();
    tabPane.addTab("tab one", new MyPanel());
    tabPane.addTab("tab two", new MyPanel());

    //add tabPane to contentPane
    contentPane.add(tabPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.99,
    GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
    0, 0, 4, 0), 0, 0));

    //create the button for our draw action
    JButton jbDrawHidden = new JButton("draw on hidden tab");
    jbDrawHidden.addActionListener(this);

    //add button to contentPane
    contentPane.add(jbDrawHidden, new GridBagConstraints(0, 1, 1, 1, 1.0,
    0.01, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
    new Insets(0, 0, 0, 0), 0, 0));

    //set contentPane of frame, set size and show it!
    theFrame.setContentPane(contentPane);
    theFrame.setSize(400, 300);
    theFrame.setResizable(false);
    theFrame.setVisible(true);
    }

    /**
    * handle draw action
    */
    public void actionPerformed(ActionEvent e) {
    JTabbedPane tabPane = (JTabbedPane) theFrame.getContentPane()
    .getComponent(0);

    //we only have 2 tabs so, index has to be 0 if 2nd tab is selected and vice versa
    int index = (tabPane.getSelectedIndex() == 0) ? 1 : 0;
    MyPanel myPanel = (MyPanel)tabPane.getComponentAt(index);
    myPanel.color = (int)(Math.random() * 0xffffff);

    //remove comment from next two lines here!
    //Graphics g = myPanel.getGraphics();
    //myPanel.paint(g);
    }

    /**
    * start the application
    */
    public static void main(String args[]) {
    new Tab();
    }
    }

    [/code]
  • -Necro--Necro- Posts: 5Member
    : >> hello!
    : >>
    : >> i have a tabbed pane with two tabs.
    : >> in tab1 i have a button(for example), how can i do that when i press
    : >> the button in tab1 it will draw something in tab2?
    : >>
    :
    : hi,
    : first i know it's stupid to answer a question with a question, but what do you intend to do? i'm not really sure if it is possible to really draw on a hidden tab of a tabbed pane, but it's definately possible to modify the hidden tab so the next time you switch to it, it will appear changed. i wrote a litte something for you. if you modify the actionPerformed(ActionEvent) method (remove the comment from the last two lines) you can see what actually happens if you try to do a paint on the hidden tab. it will overwrite the current view.
    :
    : ok, i hope this'll help you out a bit
    :
    : [code]
    : /**
    : * tabpane demo
    : */
    : public class Tab implements ActionListener {
    : /**
    : * just a JPanel, that paints itself a little bit differently
    : */
    : private class MyPanel extends JPanel {
    : /**
    : * save the color to paint the Panel
    : */
    : private int color;
    :
    : /**
    : * just paint a rectangle in random color
    : */
    : public void paint(Graphics g) {
    : //set the background the bitwise inverse of foreground
    : setBackground(new Color(~color));
    : setForeground(new Color(color));
    :
    : super.paint(g);
    : g.draw3DRect(10, 10, 200, 200, true);
    : }
    : }
    :
    : /**
    : * the frame for this demo
    : */
    : private JFrame theFrame;
    :
    : /**
    : * this constructor creates the demo and makes it visible (main work is
    : * here)
    : */
    : public Tab() {
    : //create frame and its content pane
    : theFrame = new JFrame();
    : JPanel contentPane = new JPanel();
    : contentPane.setLayout(new GridBagLayout());
    :
    : //create the tabPane and add two tabs
    : JTabbedPane tabPane = new JTabbedPane();
    : tabPane.addTab("tab one", new MyPanel());
    : tabPane.addTab("tab two", new MyPanel());
    :
    : //add tabPane to contentPane
    : contentPane.add(tabPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.99,
    : GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
    : 0, 0, 4, 0), 0, 0));
    :
    : //create the button for our draw action
    : JButton jbDrawHidden = new JButton("draw on hidden tab");
    : jbDrawHidden.addActionListener(this);
    :
    : //add button to contentPane
    : contentPane.add(jbDrawHidden, new GridBagConstraints(0, 1, 1, 1, 1.0,
    : 0.01, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
    : new Insets(0, 0, 0, 0), 0, 0));
    :
    : //set contentPane of frame, set size and show it!
    : theFrame.setContentPane(contentPane);
    : theFrame.setSize(400, 300);
    : theFrame.setResizable(false);
    : theFrame.setVisible(true);
    : }
    :
    : /**
    : * handle draw action
    : */
    : public void actionPerformed(ActionEvent e) {
    : JTabbedPane tabPane = (JTabbedPane) theFrame.getContentPane()
    : .getComponent(0);
    :
    : //we only have 2 tabs so, index has to be 0 if 2nd tab is selected and vice versa
    : int index = (tabPane.getSelectedIndex() == 0) ? 1 : 0;
    : MyPanel myPanel = (MyPanel)tabPane.getComponentAt(index);
    : myPanel.color = (int)(Math.random() * 0xffffff);
    :
    : //remove comment from next two lines here!
    : //Graphics g = myPanel.getGraphics();
    : //myPanel.paint(g);
    : }
    :
    : /**
    : * start the application
    : */
    : public static void main(String args[]) {
    : new Tab();
    : }
    : }
    :
    : [/code]
    :

    why you did this?: JTabbedPane tabPane = (JTabbedPane) theFrame.getContentPane().getComponent(0);

    can't i normally create a tabbed pane?

    i think i have better solution but it not works too, i tried to select the tab(with setSelectedIndex()) where i want to draw and then draw on it normally


Sign In or Register to comment.