I have write a program which is suppose the set the application property. I am using JSplit Pane with JScrollPane. Program is loading fine but it is not changing the value of the right panel in response to list element on the left. Please let me know if I am doing anything wrong. I have refered following program
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ApplicationPropertyDlg extends JDialog implements ActionListener,ListSelectionListener {
private DButton pb_OK = null ;
private DButton pb_CANCEL = null ;
private DButton pb_APPLY = null ;
private String[] appProp = null;
private JList appList = null;
private JSplitPane appPanel = null;
private JScrollPane listScrollPanel,appScrollPanel = null;
public ApplicationPropertyDlg ( AppDefaultWin parent ) {
super ( parent, "Properties Application", true ) ;
initializeAppProp();
intializeAppList();
initializeGUI();
ButtonPanel buttonPanel = new ButtonPanel () ;
setSize ( 800,700 ) ;
WinUtil.centerChildInParent ( this, parent ) ;
pb_OK = new JButton ( ) ;
pb_APPLY = new JButton ( ) ;
pb_CANCEL = new JButton ( ) ;
pb_OK.addActionListener ( this ) ;
pb_APPLY.addActionListener ( this ) ;
pb_CANCEL.addActionListener ( this ) ;
GUISystem.setPreferredButton ( pb_OK ) ;
GUISystem.setPreferredButton ( pb_CANCEL ) ;
GUISystem.setPreferredButton ( pb_APPLY ) ;
getContentPane().setLayout ( new BorderLayout (5,5) ) ;
getContentPane().add(appPanel);
getContentPane().add ( buttonPanel, BorderLayout.SOUTH ) ;
buttonPanel.add ( pb_OK ) ;
buttonPanel.add ( pb_APPLY ) ;
buttonPanel.add ( pb_CANCEL ) ;
setVisible ( true ) ;
}
private void initializeGUI() {
// TODO Auto-generated method stub
listScrollPanel = new JScrollPane(appList);
appScrollPanel = new JScrollPane(new GeneralPage());
appPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,listScrollPanel,appScrollPanel);
appPanel.setOneTouchExpandable(true);
appPanel.setDividerLocation(200);
//minimum size for individual Panel
Dimension minimumSize = new Dimension(100, 50);
listScrollPanel.setMinimumSize(minimumSize);
appScrollPanel.setMinimumSize(minimumSize);
//Provide a preferred size for the split pane.
appPanel.setPreferredSize(appPanel.getPreferredSize());
}
private void intializeAppList() {
// TODO Auto-generated method stub
appList = new JList(appProp);
appList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
appList.setSelectedIndex(0);
appList.addListSelectionListener(this);
}
private void initializeAppProp() {
// TODO Auto-generated method stub
appProp = new String []{"General","Task Bar", "Look and Feel","Country"};
}
public void propertyChanged ( int property, Object value ) {
if ( property == PropertySystem.PROPERTY_LANGUAGE )
setText() ;
else if ( property == PropertySystem.PROPERTY_LAF )
GUISystem.setLookAndFeel ( this ) ;
else
GUISystem.setPropertiesOnPanel ( getContentPane() ) ;
}
public void actionPerformed ( ActionEvent e ) {
dispose() ;
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
JList list = (JList)e.getSource();
updateAppPanel(appProp[list.getSelectedIndex()]);
}
private void updateAppPanel(String panelName) {
// TODO Auto-generated method stub
if(panelName.equalsIgnoreCase("General"){
appScrollPanel.removeAll();
appScrollPanel.add(new GeneralPage());
}
else if (panelName.equalsIgnoreCase("Task Bar"){
appScrollPanel.removeAll();
appScrollPanel.setViewportView(new TaskBarPage());
}
else if (panelName.equalsIgnoreCase("Language"){
appScrollPanel.removeAll();
appScrollPanel.setViewportView(new LanguagePage());
}
else if (panelName.equalsIgnoreCase("Look and Feel"){
appScrollPanel.removeAll();
appScrollPanel.setViewportView(new LookFeelPage());
}
else if (panelName.equalsIgnoreCase("Country"){
appScrollPanel.removeAll();
appScrollPanel.setViewportView(new SelectCountryPage());
}
appScrollPanel.revalidate();
appScrollPanel.repaint();
}
}
First, add a new control container to the class (preferable a
JPanel):Then, in the code section where your code initialize the
appScrollPanelvariable, instead of passing a new instance of aGeneralPane, pass thetabContainerlike this:Then, in the
updateAppPanelmethod, replace theappScrollPanel.setViewportViewinvocations like this:And it should work:
May I suggest you to put the
updateAppPanelcode inside a loop? It could save some trouble if you need to make a modification to the inner body of everyelse ifblock