• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

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.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic