• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JTabbedPane

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using JTabbedPane.I want to use call a JFrame when I click tab2. I am not sure how the event handling works with Tabs.

This is my code: It's just a Test, you can do it in a different way just to explain:

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.*;
import java.awt.event.*;



public class TabPanel extends JPanel {
Component panel1,panel2, panel3;;

public TabPanel() {
//JPanel panel = new JPanel();
JTabbedPane tabbedPane = new JTabbedPane();

JTabbedPane jTabbedPane1 = new javax.swing.JTabbedPane();
JTabbedPane jTabbedPane2 = new javax.swing.JTabbedPane();


panel1 = makeTextPanel("This is a Test: panel1");
tabbedPane.addTab("Tab One",panel1);
tabbedPane.setSelectedIndex(0);

panel2 = makeTextPanel("This is a Test: panel2");
tabbedPane.addTab("Tab Two",panel2);
tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
tabbedPaneStateChanged(evt);
}
});


//Add the tabbed pane to this panel.
setLayout(new GridLayout(1, 1));
add(tabbedPane);
}

private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
Object obj = evt.getSource();
//if tab2 is selected,I want to call another frame
JFrame frame2 = new JFrame();

}
protected Component makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}

public static void main(String[] args) {
JFrame frame = new JFrame("TabPanel");

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{System.exit(0);}
});

System.out.println("This is a Test :: inside main");

frame.getContentPane().add(new TabPanel(),
BorderLayout.CENTER);
frame.setSize(400, 125);
frame.setVisible(true);


}

}
 
Patrick Mugabe
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am making progress.

There is a getSelectedIndex() method for Tabs. So if I select the first tab, it's 0, second tab it's 1 e.t.c. So I think once I have the selectedIndex value, I can call the frame I want.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can alter your class to implement ChangeListener. Then, implement the method stateChanged(ChangeEvent e). In here, do getSelectedIndex() on your JTabbedPane (which would return 1 for tab 2) and call your frame.

When the user clicks any of the tabs, that method will automatically be called.
 
Danger, 10,000 volts, very electic .... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic