- #1
zak100
- 462
- 11
Hi,
I am trying to store values in a listbox. I have created frame & a panel inside a frame & i am using BorderLayout inside panel. I have created a button & a listBox & added them to the panel. But when i run the application, it displays the button only & when i increase the size of window then it displays the listbox neighboring to the button.
Its showing the button when application is started but when i slightly increase the window it shows the listbox also. I feel it can display the listbox without incrementing the window. Some body please guide me what's the problem.
Zulfi.
I am trying to store values in a listbox. I have created frame & a panel inside a frame & i am using BorderLayout inside panel. I have created a button & a listBox & added them to the panel. But when i run the application, it displays the button only & when i increase the size of window then it displays the listbox neighboring to the button.
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** the panel
*/
public class ListBoxEg extends JFrame{
private JButton button;
JList<String> listbox;
DefaultListModel<String> model;
public ListBoxEg() {
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setSize(500,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
panel.setLayout( new BorderLayout() );
button=new JButton("Hello");
panel.add(button, BorderLayout.WEST);
String listData = "ABCDE";
// Create a new DefaultListModel
model = new DefaultListModel<> ( );
//Adding Elements in the model
model.addElement(listData);
listData ="12345";
model.addElement(listData);
listData ="!";
model.addElement(listData);
listData ="@@@@@@";
model.addElement(listData);
//Creating a new ListBox
listbox= new JList<>();
//Adding Elements in the ListBox
listbox.setModel(model);
panel.add( listbox, BorderLayout.CENTER );
pack();
MyActionListener listener=new MyActionListener(); //added
button.addActionListener(listener);
}
public static void main(String args[ ]) {
ListBoxEg obj = new ListBoxEg( );
}
}
class MyActionListener implements ActionListener{
public void actionPerformed (ActionEvent e) {
System.out.println("pressed button");
}
}
Its showing the button when application is started but when i slightly increase the window it shows the listbox also. I feel it can display the listbox without incrementing the window. Some body please guide me what's the problem.
Zulfi.