Adding JButton in JList

Hi
I wonder if it is possible to add JButton in a JList? Or how can I produce a list of JButtons? Each button represents a person (an object), is there anyway to add listener to the buttons so i can get the person thats represnted by the button? I can decide in advance how many button ther will be...that depends on the number of person in the db.

Use a panel with a GridLayout. The Panel can be added to a JScrollPane. Read this section on "Layout Managers":
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Similar Messages

  • Adding JButton, JTextField to scroll pane...

    I have developed an appln in which I have added JPanel to JFrame.
    To this JPanel I m adding JButton & JTextField. But the JButton & JTextField are increasing dynamically in my appln[according to DBMS query]. So I wanted to use scrolpane or something like so that the window can be scrolled vertically to view all the components[JButton & JTextField]. I m developing this as a standalone appln.
    Plz help..
    Thanking in advance.

    Thanx fouadk ,
    But my problem still persists. Now though I m able to add JPanel to JScrollPane, but when the components in the JPanel increase than the height of the Pane, the components at the dowm are not visible unless I resize the window by manually dragging the corners. Actually I wanted to keep the size of the window constant and make use of the VERTICAL SCROLLBAR.
    Plz help.
    Thanking in advance.
    Following is code:
    import javax.swing.*;
    public class testt extends JFrame {
    private JPanel p = new JPanel();
    private JScrollPane sp = new JScrollPane(p
    ,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
    ,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    private JButton b[] = new JButton[15];
    public testt() {
    super("TEST");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(500, 400);
    setLocation(50, 20);
    this.add(sp);
    p.setLayout(null);
    sp.setAutoscrolls(true);
    for(int i = 0, y = 10; i < 15; i++, y += 40)
         b[i] = new JButton("BUTTON "+ (i+1));
         b.setBounds(10,y,100,20);
         p.add(b[i]);
    public static void main(String[] args) {
    testt t = new testt();
    t.setVisible(true);

  • Adding JButtons to a Java2d Graphics program

    Hi,
    I apologise to all you seasoned programmers if this seems an easy question, but I can't seem to see the solution to this one.
    I'm trying to add 2 JButtons to an existing Java2D graphics program. When I run the program I get the following error,
    'java.lang.IllegalArgumentException: adding a window to a container'
    I can't seem to see how to correct this error. My current code is as follows,
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    public class PacMan2Clicks extends JFrame implements ActionListener {
        public static int mode = 0;
        private static JButton rightButton;
        private static JButton leftButton;
        public PacMan2Clicks() {
            Container contentPane = getContentPane();
            JPanel panel = new JPanel();
            leftButton = new JButton("Leftt");
            leftButton.addActionListener(this);
            panel.add(leftButton);
            rightButton = new JButton("Right");
            rightButton.addActionListener(this);
            panel.add(rightButton);
            contentPane.add(panel, BorderLayout.SOUTH);
        public void paintComponent(Graphics g) {
            Dimension d = getSize();
            Graphics2D g2 = (Graphics2D)g;
            int size = 100;
            Ellipse2D.Double head =
                    new Ellipse2D.Double(0, 0, size, size);
            Ellipse2D.Double eye =
                    new Ellipse2D.Double(size/2-1, size/5-1,
                    size/10, size/10);
            GeneralPath mouth = new GeneralPath();
            mouth.moveTo(size, size/4);
            mouth.lineTo(size/8, size/2);
            mouth.lineTo(size, size*3/4);
            mouth.closePath();
            Area pacman = new Area(head);
            pacman.subtract(new Area(eye));
            pacman.subtract(new Area(mouth));
            g2.setPaint(Color.yellow);
            g2.fill(pacman);
            g2.setPaint(Color.black);
            g2.draw(pacman);
        public void actionPerformed(ActionEvent event) {
            if(event.getSource().equals(rightButton)) {
            } else if(event.getSource().equals(leftButton)) {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Drawing stuff");
            frame.add(new PacMan2Clicks());
            frame.setSize(600, 600);
            //frame.setContentPane(new PacMan2Clicks());
            frame.setVisible(true);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
    }Any help appreciated. Thank you.

    Your public class extends JFrame so it is a JFrame. In your main method you create a new JFrame and try to add the enclosing class (a JFrame by extension) to it. So you can either:
    1 &#8212; remove the JFrame extension from the class declaration and leave the code in the main method as&#8212;is:
    public class PM2C implements ActionListener {or,
    2 &#8212; leave the JFrame extension and remove the new JFrame instance in the main method.
    public class PM2C extends JFrame implements ActionListener {
        public PM2C(String title) {
            super(title);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
            Container contentPane = getContentPane();
            JPanel panel = new JPanel();
            leftButton = new JButton("Leftt");
            leftButton.addActionListener(this);
            panel.add(leftButton);
            rightButton = new JButton("Right");
            rightButton.addActionListener(this);
            panel.add(rightButton);
            contentPane.add(panel, BorderLayout.SOUTH);
            setSize(600, 600);
            setVisible(true);
        public static void main(String[] args) {
            new PM2C("Drawing stuff");
    }

  • How can I add  several  JButton into JList?

    I want to add several button into a Jlist. I tried this method, list.add(button,1),nothing shows up.I am not sure whether jList has the function.If it is,please show me how to achieve that.Thanks in advance!

    A JList is used to display text Strings. You should be able to add a JButton to the JList but all you will see is the toString() representation of the JButton. You will not see a button or be able to click on it.
    Try creating a panel using a GridLayout with a single column and add all you JButtons to the panel.

  • Adding JButtons to JTable

    Hello all,
    How can I add JButtons to a JTable?
    I found an article that is supposed to show you how to do just that:
    http://www.devx.com/getHelpOn/10MinuteSolution/20425
    I downloaded the code in the article and it works to an extent - I can see the buttons in the table but the buttons seem as if they are disabled :( Since I used this code in my application, I get the same behavior too.
    Is there a bug in the code? Is there a simpler solution? What am I missing?
    Raj

    Thanks. That makes the button clickable. But now the button changes back to it's old value when you click on another cell. I suppose that's because we have 2 buttons, one for rendering and one for editing. So I added a setValueAt(Object value, int row, int column) to MyModel. This works throws class cast exception.
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    public class ButtonInTable extends JFrame {
        JTable t=new JTable(new MyModel());
        public ButtonInTable() {
            this.getContentPane().add(new JScrollPane(t));
            this.setBounds(50,50,300,200);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            t.setDefaultEditor(Object.class,new ButtonEditor());
            t.setDefaultRenderer(Object.class,new ButtonRenderer());      
        public static void main(String[] args) {
            ButtonInTable buttonInTable1 = new ButtonInTable();
            buttonInTable1.show();
        class ButtonEditor extends JButton implements TableCellEditor {
            Object currentValue;
            Vector listeners=new Vector();
            public ButtonEditor() {
                            this.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                    currentValue=JOptionPane.showInputDialog("Input new value!");
                                    if (currentValue!=null) {
                                        setText(currentValue.toString());
            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                currentValue=value;
    //            this.setText(value.toString());
                this.setText( ((JButton)value).getText() );
                return this;
            public Object getCellEditorValue() {
                return currentValue;
            public boolean isCellEditable(EventObject anEvent) {
                return true;
            public boolean shouldSelectCell(EventObject anEvent) {
                return true;
            public boolean stopCellEditing() {
                ChangeEvent ce=new ChangeEvent(this);
                for (int i=0; i<listeners.size(); i++) {
                    ((CellEditorListener)listeners.get(i)).editingStopped(ce);
                return true;
            public void cancelCellEditing() {
                ChangeEvent ce=new ChangeEvent(this);
                for (int i=0; i<listeners.size(); i++) {
                    ((CellEditorListener)listeners.get(i)).editingCanceled(ce);
            public void addCellEditorListener(CellEditorListener l) {
                listeners.add(l);
            public void removeCellEditorListener(CellEditorListener l) {
                listeners.remove(l);
        class ButtonRenderer extends DefaultTableCellRenderer {
            public Component getTableCellRendererComponent
                    (JTable table, Object button, boolean isSelected, boolean hasFocus, int row, int column) {
                return (JButton)button;
        class OldModel extends DefaultTableModel {
            public OldModel() {
                super(new String[][]{{"1","2"},{"3","4"}},new String[] {"1","2"});
        class MyModel extends AbstractTableModel {
            private String[] titles = { "A", "B" };
            private Object[][] summaryTable =
                    { new JButton("11"), new JButton("12") },
                    { new JButton("21"), new JButton("22") }
            public MyModel(){
                super();
            public int getColumnCount() {
                return titles.length;
            public String getColumnName(int col) {
                return titles[col];
            public int getRowCount() {
                return summaryTable.length;
            public Object getValueAt(int row, int col) {
                return summaryTable[row][col];
            public void setValueAt(Object value, int row, int column) {
                summaryTable[row][column] = value;
                fireTableCellUpdated(row, column);
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            public boolean isCellEditable(int row, int col) {
                return true;
    }

  • Problems adding to a JList

    in my filter panel i have a text field, and a search button. when i enter some text and click search, the text should appear in the JList. but all that happens is the test field clears (which should happen) but doesnt appear in the JList. here is my code below:
    firstly for the setting up of the JList:
              //the JList with a model, renderer and selectionListener
              filterListModel = new DefaultListModel();
              filterList = new JList(filterListModel);
              filterList.setCellRenderer(new CustomCellRenderer());
              filterList.addListSelectionListener(new MyListSelectionListener());
              filterList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);then the action performed for the search button
                   public void actionPerformed(ActionEvent e) {
                        if (textFilter.getText().equals("")) {
                             JOptionPane.showMessageDialog(null, "No keyword has been entered");
                        } else {
                             int index = filterList.getSelectedIndex(); //get selected index
                             if (index == -1) { //no selection, so insert at beginning
                                  index = 0;
                             } else { //add after the selected item
                                  index++;
                             filterListModel.insertElementAt(textFilter.getText(), index);
                             //Reset the text field.
                             textFilter.requestFocusInWindow();
                             textFilter.setText("");
                             //Select the new item and make it visible.
                             filterList.setSelectedIndex(index);
                             filterList.ensureIndexIsVisible(index);
                   }i also wanted to be able to detect selected items and display them as cyan in color. i have created 2 inner classes for this.
    class MyListSelectionListener implements ListSelectionListener {
         public void valueChanged(ListSelectionEvent e) {
              if (e.getValueIsAdjusting() == false) {
                   if (filterList.getSelectedIndex() == -1) {
                        //No selection, disable fire button.
                        removeFilter.setEnabled(false);
                   } else {
                        //Selection, enable the fire button.
                        removeFilter.setEnabled(true);
    } //end class MyListSelectionListener
    class CustomCellRenderer extends JLabel implements ListCellRenderer {
         public Component getListCellRendererComponent(JList list, // the list being redrawn
         Object value, // value to display
         int index, // cell index
         boolean isSelected, // is the cell selected
         boolean cellHasFocus) // the list and the cell have
         //  the focus
         { //begin getListCellRendererComponent() body
              if (isSelected) { //set the background for selected
                   setBackground(Color.cyan);
              } else { //set the background for not selected
                   setBackground(Color.white);
              } //end else
              return this; //return component used to render
         } //end getListCellRendererComponent()
    } //end class CustomCellRenderer any help is much appreciated

    when i enter some text and click search, the text should appear in the JListThe section in the Swing tutorial on "How to Use Lists" has an example almost exactly like this:
    http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
    i also wanted to be able to detect selected items and display them as cyan in color.It is generally better to extend the DefaultListCellRenderer (it extends JLabel) as some methods have been optimized for painting efficiency.
    I don't know if this is your problem, but whenever you extend JLabel directly you need to use
    setOpaque( true );
    otherwise the background color is inherited from the parent component.

  • Adding JButton in a JTable

    hi
    i know this has been discussed quite a number of times before, but i still couldn't figure it out..
    basically i just want to add a button to the 5th column of every row which has data in it.
    this is how i create my table (partially)
         private JTable clientTable;
         private DefaultTableModel clientTableModel;
    private JScrollPane scrollTable;
    clientTableModel = new DefaultTableModel(columnNames,100);
              clientTable = new JTable(clientTableModel);
              TableColumn tblColumn1 = clientTable.getColumn("Request ID");
              tblColumn1.setPreferredWidth(70);
              tblColumn1 = clientTable.getColumn("Given Name");
              tblColumn1.setPreferredWidth(300);
              tblColumn1 = clientTable.getColumn("Address");
              tblColumn1.setPreferredWidth(350);
              tblColumn1 = clientTable.getColumn("Card Serial");
              tblColumn1.setPreferredWidth(100);
              tblColumn1 = clientTable.getColumn("Print Count");
              tblColumn1.setPreferredWidth(70);
              tblColumn1 = clientTable.getColumn("Print?");
              tblColumn1.setPreferredWidth(40);
              clientTableModel.insertRow(0,data);
              //clientTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
              scrollTable = new JScrollPane(clientTable);and i call this function void listInfoInTable(){
              JButton cmdPrint[];
              Vector columnNames = new Vector();
              Object[] data={"","","","","",""};
              Statement stmt=null;
              ResultSet rs=null;
              PreparedStatement ps;
              String query=null;
              String retrieve=null;
              int i,j=0;
              TableColumnModel modelCol = clientTable.getColumnModel();
              try{
                   con = DriverManager.getConnection(url);
                   JOptionPane.showMessageDialog(null,"Please wait while the program retrieves data.");
                   query="select seqNo, givenName, address1, address2, address3, address4, cardSerNr, PIN1, PrintFlag from PendPINMail where seqNo<250;";
                 ps = con.prepareStatement(query);
                 rs=ps.executeQuery();
                 while (rs.next()){
                      data[0]= rs.getString("seqNo");
                      data[1]= rs.getString("givenName");
                      data[2]= rs.getString("address1");
                      data[3]= rs.getString("cardSerNr");
                      data[4]= rs.getString("PrintFlag");
    //                  modelCol.getColumn(5).setCellRenderer();
                      clientTableModel.insertRow(j,data);
                      j++;
              }catch (SQLException ex){
                   JOptionPane.showMessageDialog(null,"Database error: " + ex.getMessage());
         } to display data from database inside the table.
    How do I add JButton to the 5th column of each row? This documentation here http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#width says that i need to implement TableCellEditor to put JButton in the table. How do i really do it? I want the button to call another function which prints the data from the row (but this is another story).
    Any help is greatly appreciated. Thanks

    you would need CellRenderer i think that it is in
    javax.swing.table.*;
    see if you can get started with that.
    Davidthanks, i'll try and have a look at it
    Yes, that's definitely what you need to start with,
    but you also need a CellEditor to return the
    button as well, otherwise the button will not be
    clickable (the renderer just paints the cell for
    display, it doesn't allow you to interact with it).
    You could maintain a list of components for rendering
    each cell of the table so that your
    CellRenderer and CellEditor always
    return the same object (i.e. a JButton for any
    given cell).
    CB.thanks for the info.. could you point me to some examples? is sounds quite complicated for me....
    thanks again

  • Adding files to jlist

    hi,
    i trying to add File objects to a JList can keep getting a nullpointer exception, here is the code:
    private DefaultListModel listModel;
    File file = new File("c:");
    File[] files = file.listFiles();
    addToListModel(files);
    fileList = new JList(files);
    public void addToListModel(File[] list) {
         System.out.println("lenght is " + list.length);
         System.out.println(list[0]);
         for(int i = 0; i < list.length; i++) {
              listModel.addElement(list);
    i put a println in the method, it print 5 as the lenght and does output the first value in the array, but then throws the nullpointer excpetion, any ideas?
    Thank you.

    Are you sure you created listModel? I don't see
    listModel = new DefaultListModel()in your sample? If you didn't it would be null.

  • Adding Images in JList?

    Hi guys,
    how do i add Images in JList along with Text? please help me here.
    cheers,
    Sachin

    You will have to provide some images (place them in the same directory as the compiled class file) for the following example.
    import javax.swing.*;
    import java.awt.*;
    public class JListExample extends JFrame
        private class Value
            Value(String value, Icon image)
                this.value = value;
                this.image = image;
            String value;
            Icon image;
        private Icon getIcon(String name)
            java.net.URL url = getClass().getResource(name);
            return new ImageIcon(url);
        public JListExample()
            super("Simple JList Example");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final Value[] VALUES =
                new Value("<html><body>One for<br>      the road", getIcon("Add24.gif")),
                new Value("<html><body>Two pear<br>      trees",  getIcon("Copy24.gif")),
                new Value("<html><body>Three<br>      stooges",  getIcon("Cut24.gif")),
                new Value("<html><body>Four<br>      bauld tires",  getIcon("Edit24.gif")),
            JList list = new JList(VALUES);
            list.setCellRenderer(new SimpleCellRenderer());
            getContentPane().add(list);
            pack();
        public static void main(String[] args)
            try
                new JListExample().setVisible(true);
            catch (Exception e)
                e.printStackTrace(System.out);
        class SimpleCellRenderer extends JLabel implements ListCellRenderer
            public SimpleCellRenderer()
                setOpaque(true);
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
                Value val = (Value)value;
                setText(val.value);
                setIcon(val.image);
                setBackground(isSelected ? Color.red : (index & 1) == 0 ? Color.cyan : Color.green);
                setForeground(isSelected ? Color.white : Color.black);
                return this;
    }

  • Adding JButton in BorderLayout

    Hi everyone,
    I have a question about Panel. I want to know how can we add 2 button at the bottom of panel2 and then add panel2 into panel1. here is what I did.
         // 2 buttons for panel2
         JButton b1 = new JButton ("Start");
         b1.addActionListener(this);
         JButton b2= new JButton("Stop");
         b2.addActionListener(this);
         panel2.add(b1);
         panel2.add(b2);
         //now add panel2 to panel1
         panel1.add(panel2, Borderlayout.SOUTH);
         getContentPane().add(panel1, Borderlayout.CENTER);
    but this code is not compiling.
    it gives me these error. where can i be wrong?
    test.java:33: cannot resolve symbol
    symbol : class add
    location: package panel2
    panel2.add(b1);
    ^
    test.java:34: cannot resolve symbol
    symbol : class add
    location: package panel2
    panel2.add(b2);
    ^
    test.java:38: cannot resolve symbol
    symbol : class add
    location: package panel1
    panel1.add(panel2, Borderlayout.SOUTH);
    Thanks

    This may help u
    import javax.swing.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.*;
    public class Test1 extends JFrame implements ActionListener
         JPanel panel1,panel2;
         public void init()
    JButton b1 = new JButton ("Start");
    b1.addActionListener(this);
    JButton b2= new JButton("Stop");
    b2.addActionListener(this);
    panel2.add(b1);
    panel2.add(b2);
    //now add panel2 to panel1
    panel1.setLayout(new BorderLayout());
    panel1.add(panel2, BorderLayout.SOUTH);
    this.getContentPane().add(panel1, BorderLayout.CENTER);
    public void actionPerformed(ActionEvent e)

  • Adding JButton into JTable cells

    Hi there!!
    I want to add a JButton into JTable cells.In fact I have got two classes.Class2 has been extended from the AbstractTableModel class and Class1 which is using Class2's model,,,here's the code,,
    Class1
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    public class Class1 extends javax.swing.JFrame {
       //////GUI specifications
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TestTableButton().setVisible(true);
            Class2 model=new Class2();
            jTable1=new JTable(model);
            jScrollPane1.setViewportView(jTable1);
        // Variables declaration - do not modify                    
        private javax.swing.JScrollPane jScrollPane1;
        // End of variables declaration                  
        private javax.swing.JTable jTable1;
    }Class2
    import javax.swing.table.*;
    public class Class2 extends AbstractTableModel{
        private String[] columnNames = {"A","B","C"};
        private Object[][] data = {
        public int getColumnCount() {
            return columnNames.length;
        public int getRowCount() {
            return data.length;
        public String getColumnName(int col) {
            return columnNames[col];
        public Object getValueAt(int row, int col) {
            return data[row][col];
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
         * Don't need to implement this method unless your table's
         * editable.
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
                return false;
         * Don't need to implement this method unless your table's
         * data can change.
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
    }what modifications shud I be making to the Class2 calss to add buttons to it?
    Can anybody help plz,,,,,??
    Thanks in advance..

    Hi rebol!
    I did search out a lot for this but I found my problem was a bit different,,in fact I want to use another class's model into a class being extended by JFrame,,so was a bit confused,,,hope you can give me some ideas about how to handle that scenario,,I know this topic has been discussed before here many a times and also have visited this link,,
    http://forum.java.sun.com/thread.jspa?threadID=465286&messageID=2147913
    but am not able to map it to my need,,,hope you can help me a bit...
    Thanks .....

  • Adding color to JList

    Hello,
    I have a Jlist that I would like to color the rows on. Each row I in the Jlist displays
    a string, I would like for everyother row to be light blue or something. I have looked at the
    api and the only method i see that deals with setting bg color is...
    setSelectionBackground(Color selectionBackground)
    but I dont think this is what I want. Is this possible...if so could I see a example.
    thanks,
    jd

    Well I was hoping to get this up here before anyone else, but I guess that didn't happen. Here's a sample:
    import java.awt.*;
    import javax.swing.*;
    public class ListTest extends JFrame {
            private JList list;
            public ListTest() {
                    getContentPane().setLayout( new FlowLayout() );
                    String[] data = { "one", "two", "three", "four", "five" };
                    list = new JList( data );
                    list.setCellRenderer( new MyCellRenderer() );
                    getContentPane().add( list );
                    setTitle( "List Test" );
                    setSize( 320, 240 );
                    setLocationRelativeTo( null );
                    setVisible( true );
            public static void main( String[] args ) {
                    ListTest application = new ListTest();
                    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    class MyCellRenderer extends JLabel implements ListCellRenderer {
            public MyCellRenderer() {
                    setOpaque( true );
            public Component getListCellRendererComponent( JList list, Object object, int index, boolean isSelected, boolean cellHasFocus ) {
                    setText( object.toString() );
                    if ( isSelected ) {
                            setBackground( Color.BLUE );
                            setForeground( Color.WHITE );
                    else {
                            if ( index % 2 == 0 ) {
                                    // Even cell
                                    setBackground( Color.WHITE );
                                    setForeground( Color.BLACK );
                            else {
                                    // Odd cell
                                    setBackground( Color.LIGHT_GRAY );
                                    setForeground( Color.BLACK );
                    setEnabled( list.isEnabled() );
                    setFont( list.getFont() );
                    return this;
    }

  • Probelms Adding items to JList Component

    i tried to add items to JList from the database.... and it can add to the variable but the list isnt displaying....
    here is my code for that button...
    list_ListTopic = new JList(new DefaultListModel());
    DefaultListModel listModel = (DefaultListModel)list_ListTopic.getModel();
    jScrollPane1 = new JScrollPane(list_ListTopic);
    jScrollPane1.setViewportView(list_ListTopic);
    Vector data = new Vector();
    listModel.clear();
    while (lrs.next())
            data.add(lrs.getString("tName"));                                       
            i++;
    list_ListTopic.setListData(data);
    jScrollPane1.setViewportView(list_ListTopic);Thanks!

    Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/list.html]How to Use Lists. It shows you how to initially load data into a list and then how to add/remove entries from the list. In general, you don't update the Vector after the ListModel has been created you update the model directly.

  • Adding JButton onto a Rectangle

    Hi All,
    Is there any way to add a JButton onto the Rectangle..???
    I have drawed a rectangle by using Graphics class and i need a button to be embedded onto that.
    Thanks in advance.
    regards,
    Viswanadh

    A Rectangle is not a Component so no you can't "add" it to a Rectangle.
    Is the Rectangle the same size as the button so only one button fits in the rectangle. Or do you have multiple buttons in the Rectangle.
    In the first case you could just add a Border to the button. In the second case you add a Border to a JPanel and then add the buttons to the panel.
    I suggestion you start by reading the [Swing tutorial|http://java.sun.com/docs/books/tutorial/uiswing/TOC.html]. Maybe the section on Layout Managers so you learn how to control the positioning of Components in a Container.

  • Adding JButton to JTextAreas

    I'm publishing reports from data onto an uneditable JTextArea (so the user can save it as a text doc if they wish).
    I'd like to have a button next to each report so the user can view the data for that report, but presumably this can't be done on a JTextArea?
    Anyone have any suggestions as to how I can have a 'View data' button, but still be able to save the text of the report?
    Many thanks.

    If you are trying to produce a panel that has a JTextArea and a JButton on it, that is relatively easy to do.
    You cannot put a JButton into a text area but it is easy enough to put it either next to or below the text area and provide the functionality that you are looking for.

Maybe you are looking for

  • Error in writing to file '$ORACLE_HOME/jdk/jre/bin/java'

    Hi, I have installed Forms & Reports Services Standalone (10.1.2.0.2) on RHEL 5 and now I am applying the Oracle Application Server 10g Release 2, Patch Set 3 (10.1.2.3), but OUI gives the following error: Error in writing to file '$ORACLE_HOME/jdk/j

  • Filling of mara-volum and -voleh within BAPI_MATERIAL_SAVEDATA

    I like to update mara-volum and mara-voleh by using BAPI_MATERIAL_SAVEDATA, but these fields are not available in structure BAPI_MARA, only in structure BAPI_MARM. Please let me know how to solve this problem.

  • Change AE CC 2014.2 new Highlight color

    Hi, I like the new UI improvements, but the new Highlight-Color is cancer for my eyes and is more distracting than helpful. Is there a way to change this? Cheers - Michael

  • Synch'ing SAP - LDAP ( WAS6.10)

    Hi, I have used the fantastic functionality available from WAS6.10 to synchronise SAP backend systems with an LDAP.. BUT, I'm trying to synchronise the security access a user has in a 4.6c SAP system to Active Directory.  What options do I have here?

  • MD04 and rescheduling date

    Hi, This is my scenario: Material A is composed by material B. When the stock of material B is 0, MD04 does not take in account the time to provisioning Material B when a Material A has to be produced. Example: Time to create Material A: 3 days. Crea