Hi!
All..
I am displaying 2 jlabels,1 jtext field, 1 Jcombo box and 2 jbuttons.
I have added a focus listener to the text field.On focus lost of this text field i am displaying a message .
The problem is..when the text field is loosing focus by clicking on the "ok" button,the message is appearing as usual.But after clicking on the message,the "ok" button is not coming back to original state.what to do so that it will come back to original state.
I am attaching the code below..any help will be appreciated.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
public class ChartNameFrame21 extends JFrame
{
JLabel jla_compName,jla_compID;
JComboBox jcmb_compName;
JTextField jtf_compID;
JButton jbtn_OK,jbtn_CANCEL;
JPanel jp_compPanel,jp_btnPanel;
String [] str_chart_Names = null;
Random rd_value;
static ChartNameFrame21 cname;
Container contentPane = null;
public ChartNameFrame21()
{
ImageIcon ima = new ImageIcon("middle.gif");
jla_compName = new JLabel("Component");
jla_compName.setPreferredSize(new Dimension(130,30));
jla_compName.setMaximumSize(new Dimension(130,30));
str_chart_Names = new String[]{"AreaChart2D","AreaChart3D","BarChart","BoxPlot",
"GroupedBarChart","LineChart","LineChart3D",
"PercentageAreaChart","PieChart","PieChartExplode",
"StackedBarChart","StockChart","StockChartVol","StockQuotes",
"TimeChart","XYLineChart","XYLineChart3D"};
jcmb_compName = new JComboBox(str_chart_Names);
jcmb_compName.setMaximumRowCount(3);
jcmb_compName.setPreferredSize(new Dimension(130,30));
jcmb_compName.setMaximumSize(new Dimension(130,30));
jcmb_compName.addItemListener(new ComboItemListener());
//first temp panel for label and combo box
JPanel jp_temp = new JPanel();
jp_temp.setLayout(new BoxLayout(jp_temp, BoxLayout.X_AXIS));
jp_temp.add(jla_compName);
jp_temp.add(jcmb_compName);
jbtn_OK = new JButton("OK", new ImageIcon("left.gif"));
jbtn_OK.setPreferredSize(new Dimension(100,50));
jbtn_OK.setPressedIcon(ima);
jbtn_CANCEL = new JButton("CANCEL");
//fourth panel for the above two buttons
jp_btnPanel = new JPanel();
jp_btnPanel.add(jbtn_OK);
jp_btnPanel.add(jbtn_CANCEL);
jla_compID = new JLabel("ID");
jla_compID.setPreferredSize(new Dimension(130,30));
//jla_compID.setMinimumSize(new Dimension(100,30));
jla_compID.setMaximumSize(new Dimension(130,30));
rd_value = new Random();
int i_idValue = Math.abs(rd_value.nextInt());
jtf_compID = new JTextField(5);
jtf_compID.setText("A2D"+(String.valueOf(i_idValue).substring(0,7)));
jtf_compID.setPreferredSize(new Dimension(130,30));
jtf_compID.setMaximumSize(new Dimension(130,30));
jtf_compID.addFocusListener(new TextFocusListener());
//second temp panel for label and text field.
JPanel jp_temp2 = new JPanel();
jp_temp2.setLayout(new BoxLayout(jp_temp2,BoxLayout.X_AXIS));
jp_temp2.add(jla_compID);
jp_temp2.add(jtf_compID);
//third panel for the above two temp panels
jp_compPanel = new JPanel();
jp_compPanel.setLayout(new BoxLayout(jp_compPanel,BoxLayout.Y_AXIS));
jp_compPanel.add(jp_temp);
jp_compPanel.add(Box.createRigidArea(new Dimension(0,20)));
jp_compPanel.add(jp_temp2);
contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel,BoxLayout.Y_AXIS));
topPanel.add(Box.createRigidArea(new Dimension(0,200)));
topPanel.add(jp_compPanel);
topPanel.add(Box.createRigidArea(new Dimension(0,80)));
topPanel.add(jp_btnPanel);
contentPane.add(topPanel);
//setSize(300,300);
//setSize(Toolkit.getDefaultToolkit().getScreenSize().width,Toolkit.getDefaultToolkit().getScreenSize().height);
setVisible(true);
}
class ComboItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if(e.getSource().equals(jcmb_compName))
{
int i_cmbIndex = jcmb_compName.getSelectedIndex();
Object o_txtValue = jcmb_compName.getItemAt(i_cmbIndex);
int j = Math.abs(rd_value.nextInt());
String str = String.valueOf(j).substring(0,7);
if(i_cmbIndex == 2 || i_cmbIndex == 3 || i_cmbIndex == 5 || i_cmbIndex == 7 || i_cmbIndex == 8 || i_cmbIndex == 10 || i_cmbIndex ==11 || i_cmbIndex == 14 || i_cmbIndex == 15)
jtf_compID.setText(String.valueOf(o_txtValue).substring(0,3)+str);
else
{
switch(i_cmbIndex)
{
case 0:
jtf_compID.setText("A2D"+str);
break;
case 1:
jtf_compID.setText("A3D"+str);
break;
case 4:
jtf_compID.setText("GBC"+str);
break;
case 6:
jtf_compID.setText("L3D"+str);
break;
case 9:
jtf_compID.setText("PEX"+str);
break;
case 12:
jtf_compID.setText("SVO"+str);
break;
case 13:
jtf_compID.setText("SQU"+str);
break;
case 16:
jtf_compID.setText("X3D"+str);
break;
}
}
}
}
}
class TextFocusListener implements FocusListener
{
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
System.out.println("lost focus event");
JTextField jt = (JTextField)e.getSource();
if((jt.equals(jtf_compID)) && !(e.isTemporary()))
{
Object o_ChartType = jcmb_compName.getSelectedItem();
String s_ChartID = jtf_compID.getText().trim();
boolean isConnection = false;
if(!isConnection)
{
System.out.println("Not unique id");
//JFrame f = new JFrame();
JOptionPane.showMessageDialog(jtf_compID, "The ComponentID is not UNIQUE");
}
}
}
}
public static void main(String args[])
{
cname = new ChartNameFrame21();
cname.pack();
cname.setSize(Toolkit.getDefaultToolkit().getScreenSize().width,Toolkit.getDefaultToolkit().getScreenSize().height);
}
}