i've created an application in java swing.
The application is a JFrame
with a JTabbedPane
with 3 tabbedpanes.
In the first pane there is a progress checkbox, if the checkbox is select then on clicking on to another panes i'e 2nd or 3rd pane will get a ERROR
message showing "Tabbed 1 in progress".
The problem is that when i click on to other panes when the progress checkbox is selected, It will first goes to the pane which i selected then only the redirection works and also the Error message is executed twice.
i've prevented Twice execution of Error message somehow , but can't prevent going to clicked pane when the progress checkbox is clicked.
Can anyone please tell me some solution for this problem.
my complete code is given below.
import javax.swing.JOptionPane;
public class sample extends javax.swing.JFrame {
public static int status=0;
public sample() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
tab = new javax.swing.JTabbedPane();
jDesktopPane2 = new javax.swing.JDesktopPane();
jLabel1 = new javax.swing.JLabel();
progress = new javax.swing.JCheckBox();
jDesktopPane4 = new javax.swing.JDesktopPane();
jLabel2 = new javax.swing.JLabel();
jDesktopPane5 = new javax.swing.JDesktopPane();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tab.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
tab.setTabPlacement(javax.swing.JTabbedPane.LEFT);
tab.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
tabStateChanged(evt);
}
});
jDesktopPane2.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("TABBED PANE 1 BODY ");
jLabel1.setBounds(50, 60, 230, 40);
jDesktopPane2.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
progress.setText("Progress");
progress.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
progressItemStateChanged(evt);
}
});
progress.setBounds(50, 40, 90, 23);
jDesktopPane2.add(progress, javax.swing.JLayeredPane.DEFAULT_LAYER);
tab.addTab("Tabbed Pane 1", jDesktopPane2);
jDesktopPane4.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel2.setText("TABBED PANE 2 BODY ");
jLabel2.setBounds(30, 90, 230, 40);
jDesktopPane4.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
tab.addTab("Tabbed Pane 2", jDesktopPane4);
jDesktopPane5.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel3.setText("TABBED PANE 3 BODY ");
jLabel3.setBounds(60, 160, 230, 40);
jDesktopPane5.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
tab.addTab("Tabbed Pane 3", jDesktopPane5);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tab, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tab, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void progressItemStateChanged(java.awt.event.ItemEvent evt) {
if(progress.isSelected())
setStatus(1);
else
setStatus(0);
}
private void tabStateChanged(javax.swing.event.ChangeEvent evt) {
int o=getStatus();
if(o==1)
{
JOptionPane.showMessageDialog(null, "Tabbed 1 Progress!!!", "Confirmation!!!",JOptionPane.ERROR_MESSAGE);
setStatus(0);
tab.setSelectedIndex(0);
setStatus(1);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
sample s= new sample();
s.setLocationRelativeTo(null);
s.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JDesktopPane jDesktopPane2;
private javax.swing.JDesktopPane jDesktopPane4;
private javax.swing.JDesktopPane jDesktopPane5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JCheckBox progress;
private javax.swing.JTabbedPane tab;
// End of variables declaration
public static void setStatus(int i) {
status=i;
}
public static int getStatus()
{
return status;
}
}
it is because you using the tabStateChanged event, so the event will be triggered only when the tab is changed, so you can either change the event type to clicked or change the code so it don't show the error first, but change the selected Tab first then show the error box
int o=getStatus();
if(o==1 && tab.getSelectedIndex() != 0) //if the selected index isn't 0 and the o == 1
{
tab.setSelectedIndex(0);
JOptionPane.showMessageDialog(null, "Tabbed 1 Progress!!!", "Confirmation!!!",JOptionPane.ERROR_MESSAGE);
//setStatus(0); delete this
//setStatus(1); delete this
}