Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

java swing- create scrollable table

Posted on 2005-02-27
6
Medium Priority
?
348 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
  • 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
Independent Software Vendors: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

564 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