Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
when the applet runs add, at first the Jlist on the right is barely visible, but after adding elements to the list the center buttons resize taking up almost the entire space leaving a small space for both JLists.
Vector vector = new Vector()
 
JButton add = new JButton("add");
 
JButton remove = new JButton("remove");
 
DefaultListModel model =new DefaultListModel();
 
JList firstList = new JList(vector);
 
Jlist selectedList = new Jlist(model);
 

JPanel rightPanel = new JPanel();
 
rightPanel.add(selectedList);
 
JPanel leftPanel = new JPanel();
 
leftPanel.add(firstList );
 
JPanel centerButtons = new JPanel(new GridLayout(2,1);
 
centerButtons.add(add);
centerButtons.add(remove);
 
add.addActionListener(new ButtonListener());
remove.addActionlistener(new RemoveButton());
 
setLayout(new BorderLayout());
 
add(rightPanel, BorderLayout.EAST);
add(leftPanel, BorderLayout.WEST);
add(centerButtons, BorderLayout.CENTER);
 
     private class ButtonListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e) {
 

                model.addElement(firstList.getSelectedValue());
           
          
 
        }
Posted 22-Feb-13 6:47am
Edited 22-Feb-13 6:48am
34K
v2
Comments
nameJulian at 16-May-13 5:42am
   
I believe that your problem might be the GridLayout for your centerButtons panel. GridLayout tells the program, that the buttons can ocupy all the space they can aquire. Try using this:
 
JPanel centerButtons=new JPanel();
centerButtons.setLayout9(new BoxLayout(centerButtons,BoxLayout.Y_AXIS);
 
This is much like GridLayout(2,1). It display's the buttons vertical in the Panel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 475
1 OriginalGriff 248
2 ChintanShukla 185
3 Bhavik_Patel 170
4 PhilLenoir 125
0 Sergey Alexandrovich Kryukov 8,713
1 OriginalGriff 7,357
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100