?
Solved

java swing- create scrollable table

Posted on 2005-02-27
6
Medium Priority
?
344 Views
Last Modified: 2008-01-09
I need to display a table with persons names that have been entered.(using TabelModel and Tablecellrenderer)The table must include a column for person's first and last name, country, salary expectations. It must have column headers. If salary expected is above 100,000, must be displayed in red.
The table must have buttons to create, modify and delete entries. Also same functionalities must be possible thro a menu bar. When Sreate and Modify is selected, there should be a dialog box apperaring to input text. If entered item is already existing, should display appropriate message.  Table must be updated appropriately. Also an Exit button/menu should be there.

Thanks in advance.
0
Comment
Question by:Jaysri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 

Author Comment

by:Jaysri
ID: 13416412
Thanks for the links, expecially the www.objects.com.au is very good. But i am in a last minute rush, and not much time to look thro for the right soln. Pls. send some source code.

Thanks.

0
 

Author Comment

by:Jaysri
ID: 13416430
The EditableListExample is what i am looking for with the buttons and manu bar.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 13416551
theres a link to the source on the page
0
 
LVL 1

Accepted Solution

by:
anindyabecs earned 2000 total points
ID: 13417533
Hi Jaysri,
       This is one jTable example that I have written... It is in a package called testcal..
Just go through it... You will get all the necessary information..
       Just run it and check it out....
                                                       - Anindya



package testcal;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.AbstractTableModel;
import java.util.Vector;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2005
 * Company:
 * @author
 * @version 1.0
 */

public class scrollTab extends JFrame {
  JScrollPane jScrollPane1 = new JScrollPane();
  //JTable jTable1 = new JTable();
  JTable jTable1;
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  MyTableModel tableModel;

  public static void main(String args[])
  {
    System.out.println("In the main");
    scrollTab scT = new scrollTab();
    scT.setVisible(true);
  }

  public scrollTab() {

    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }

    showTable();
  }
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
    jScrollPane1.setBounds(new Rectangle(40, 131, 454, 236));
    jButton1.setText("Add");
    jButton1.setBounds(new Rectangle(295, 67, 80, 28));
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setText("Remove");
    jButton2.setBounds(new Rectangle(397, 67, 80, 28));
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    this.setDefaultCloseOperation(3);
    this.getContentPane().add(jScrollPane1, null);
    this.getContentPane().add(jButton1, null);
    this.getContentPane().add(jButton2, null);
    //jScrollPane1.getViewport().add(jTable1, null);
    this.setBounds(50,50,600,500);
  }

  void showTable()
  {
       Vector colHeaders = new Vector();
       colHeaders.addElement("Broker SP");
       colHeaders.addElement("Broker SEBI");
       colHeaders.addElement("Broker Name");
       tableModel = new MyTableModel(colHeaders);
       System.out.println("In show Table");
       jTable1 = new JTable(tableModel);
       //jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
       jScrollPane1.getViewport().add(jTable1, null);
       jTable1.addMouseListener(new java.awt.event.MouseAdapter()
         {
              public void mouseClicked(MouseEvent e)
                  {
                        table_mouseClicked(e);
                  }
            });

      jTable1.setVisible(true);
  }

      void table_mouseClicked(MouseEvent e)
    {
      int column=0;
      int row=0;
      try
          {
              column=jTable1.getSelectedColumn();
              row=jTable1.getSelectedRow();
              System.out.println("The row and coloum are :: "+row+"::"+column);
          }
      catch(Exception ex)
          {
          System.out.println("Exception in MOUSE CLICK");
          }

      }

  void jButton1_actionPerformed(ActionEvent e) {
            try
            {
            jTable1.getCellEditor().stopCellEditing();
            }
            catch(Exception ex)
            {

            }
            tableModel.addRow(this.addDefaultRow());
            jTable1.repaint();

  }

   /**
   * this function adds the default row in the table
   */
        Vector addDefaultRow()
        {
            Vector vec= new Vector();
            vec.addElement("");
            vec.addElement("");
            vec.addElement("");
            return vec;
         }

  void jButton2_actionPerformed(ActionEvent e)
  {
       try
        {
            try
            {
              jTable1.getCellEditor().stopCellEditing();
            }
            catch(Exception ex)
            {
            }

            int[] rows=jTable1.getSelectedRows();

            if(rows.length==0)
            {
                System.out.println("The length of the selected rows is 0");
                return;
            }
            int j=0;
            int index=rows[0]-1;
            jTable1.getSelectionModel().setSelectionInterval(index,index);

            for(int i=0;i<rows.length;i++)
            {
              tableModel.removeRow(rows[i]-j);
              j++;
            }
       }
        catch(Exception d)
        {
          System.out.println("Exception in row delete");
        }
  }



}





//This is the actual table model

class MyTableModel extends AbstractTableModel
{
    Vector colHeads;
    Vector data = new Vector();
    boolean[] editableCols;
    boolean editable = false;

    public MyTableModel(Vector colHeads)
    {
        this.colHeads = colHeads;

    }


    public int getRowCount()
    {
        return data.size();
    }

    public int getColumnCount()
    {
        return colHeads.size();

    }

    public void addRow(Vector vec)
    {
        data.addElement(vec);
        fireTableChanged(null);
    }

    public void modifyRow(int row, Vector vec)
    {
        data.setElementAt(vec, row);
        fireTableChanged(null);
    }

    public void removeRow(int row)
    {
        data.removeElementAt(row);
        fireTableChanged(null);
    }

    public Object getValueAt(int row, int col)
    {
        if(row < data.size())
        {
            Object obj = ((Vector)data.elementAt(row)).elementAt(col);
            if(obj instanceof Boolean)
            {
                return (Boolean)obj;
            }
            else
            {
                return obj;
            }
        }
        else
        {
            return null;
        }
    }

    public String getColumnName(int column)
    {
        return (String)colHeads.elementAt(column);
    }

    public Class getColumnClass(int c)
    {
        return getValueAt(0,c).getClass();
    }

    public boolean isCellEditable(int row,int col)
    {
      if(editable)
      {
        return editableCols[col];

      }
      else
      {
        return false;
      }
    }

    public void setValueAt(Object aValue, int row, int column)
    {

        ((Vector)data.elementAt(row)).setElementAt(aValue, column);

         fireTableCellUpdated(row, column);
    }

    public void removeAll(){
        data.removeAllElements();
        fireTableChanged(null);

    }

    public void setEditableColumns(boolean[] cols)
    {
      editable = true;
      editableCols = new boolean[cols.length];
      for(int i = 0; i < cols.length; i++)
      {
        editableCols[i] = cols[i];
      }
    }
}
0
 
LVL 1

Expert Comment

by:anindyabecs
ID: 13418204
Hi Jaysri,
      Thnaks a lot for those valuable points...
                                                     -      Anindya
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question