Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

warning mesage before leaving particular tab in swings  RSS feed

 
sireesha Makkapati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi All,


I have a tabbed pane,its having differenet tabs,
I wanted to display some message on console before leaving particular tab.

With example the following example the message is displaying while leaving any of the tab,but i wanted to dsplay the message only when i am leaving from TAB A.

import java.awt.BorderLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
public class tabbedPane implements FocusListener {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
String titles[] = { "A", "B", "C", "D", "E", "F" };
int mnemonic[] = { KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E,
KeyEvent.VK_F };
for (int i = 0, n = titles.length; i < n; i++) {
add(tabbedPane, titles[i], mnemonic[i]);
}

ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
int index = sourceTabbedPane.getSelectedIndex();

if((!sourceTabbedPane.getTitleAt(index).equals("A")))
{
System.out.println("tab changed from a");
}

}
};
tabbedPane.addChangeListener(changeListener);

frame.add(tabbedPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);

}

static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
int count = tabbedPane.getTabCount();
JButton button = new JButton(label);
tabbedPane.addTab(label, button);
tabbedPane.setMnemonicAt(count, mnemonic);

}
}

Thanks in advance...
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please use code tags while you post any code. As far as you code is concerned you need a small logical change in the code, i.e you need to know whether the previous clicked tab was A or not.



 
sireesha Makkapati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks for your reply..
Have a Nice Day.
 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Report post to moderator
...
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Mahesh,
I am sorry but I had to edit our your post as you were hijacking someone else's thread.
http://faq.javaranch.com/java/UseOneThreadPerQuestion

You can post your question by starting a new thread.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!