Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 85345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:04:29+00:00 2026-05-10T22:04:29+00:00

I have a function which gets a key from the user and generates a

  • 0

I have a function which gets a key from the user and generates a Hashtable (on a pattern specified by the key). After creating a Hashtable, I would like to populate a JTable so that each each column represents a key and every rows represents the values associated with the key. I tried everything but couldn’t get this work. I’m not creating the table from within the constructor as I need to get input from the user.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:04:30+00:00Added an answer on May 10, 2026 at 10:04 pm

    See How to Use Tables: Creating a Table Model.

    The JTable constructor used by SimpleTableDemo creates its table model with code like this:

    new AbstractTableModel() {     public String getColumnName(int col) {         return columnNames[col].toString();     }     public int getRowCount() { return rowData.length; }     public int getColumnCount() { return columnNames.length; }     public Object getValueAt(int row, int col) {         return rowData[row][col];     }     public boolean isCellEditable(int row, int col)         { return true; }     public void setValueAt(Object value, int row, int col) {         rowData[row][col] = value;         fireTableCellUpdated(row, col);     } } 

    You basically have to wrap your hashtable in the above manner. Here’s an example.

    package eed3si9n.hashtabletable;  import java.awt.BorderLayout; import java.util.Enumeration; import java.util.Hashtable;  import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.JButton; import java.awt.Dimension;  public class MainForm extends JFrame {      private static final long serialVersionUID = 1L;     private JPanel jContentPane = null;  //  @jve:decl-index=0:visual-constraint='23,38'     private JScrollPane m_scrollPane = null;     private JTable m_table = null;     private Hashtable<String, String> m_hash = null;     private JButton m_btnAdd = null;          /**      * This is the default constructor      */     public MainForm() {         super();         initialize();         m_hash = new Hashtable<String, String>();         m_hash.put('Dog', 'Bow');     }      private void onButtonPressed() {         m_hash.put('Cow', 'Moo');         m_table.revalidate();     }      /**      * This method initializes this      *       * @return void      */     private void initialize() {         this.setSize(409, 290);         this.setTitle('JFrame');         this.setContentPane(getJContentPane());     }      /**      * This method initializes jContentPane      *       * @return javax.swing.JPanel      */     private JPanel getJContentPane() {         if (jContentPane == null) {             jContentPane = new JPanel();             jContentPane.setLayout(new BorderLayout());             jContentPane.setSize(new Dimension(500, 500));             jContentPane.setPreferredSize(new Dimension(500, 500));             jContentPane.add(getM_scrollPane(), BorderLayout.NORTH);             jContentPane.add(getM_btnAdd(), BorderLayout.SOUTH);         }         return jContentPane;     }      /**      * This method initializes m_scrollPane       *        * @return javax.swing.JScrollPane        */     private JScrollPane getM_scrollPane() {         if (m_scrollPane == null) {             m_scrollPane = new JScrollPane();             m_scrollPane.setViewportView(getM_table());         }         return m_scrollPane;     }      /**      * This method initializes m_table        *        * @return javax.swing.JTable         */     private JTable getM_table() {         if (m_table == null) {             m_table = new JTable();             m_table.setModel(new AbstractTableModel(){     private static final long serialVersionUID = 1L;      public int getColumnCount() {         return 2;     }      public int getRowCount() {         return m_hash.size();     }      public String getColumnName(int column) {         if (column == 0) {             return 'Animal';         } else {             return 'Sound';         }     }      public Object getValueAt(int rowIndex, int columnIndex) {         if (columnIndex == 0) {             return getKey(rowIndex);         } else {             return m_hash.get(getKey(rowIndex));         } // if-else      }      private String getKey(int a_index) {         String retval = '';         Enumeration<String> e = m_hash.keys();         for (int i = 0; i < a_index + 1; i++) {             retval = e.nextElement();         } // for          return retval;     }              });         }         return m_table;     }      /**      * This method initializes m_btnAdd       *        * @return javax.swing.JButton        */     private JButton getM_btnAdd() {         if (m_btnAdd == null) {             m_btnAdd = new JButton();             m_btnAdd.setPreferredSize(new Dimension(34, 30));             m_btnAdd.addActionListener(new java.awt.event.ActionListener() {                 public void actionPerformed(java.awt.event.ActionEvent e) {                     onButtonPressed();                 }             });         }         return m_btnAdd;     }      public static void main(String[] args) {         //Schedule a job for the event-dispatching thread:         //creating and showing this application's GUI.         javax.swing.SwingUtilities.invokeLater(new Runnable() {             public void run() {                 MainForm frame = new MainForm();                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 frame.setSize(500, 500);                 frame.setVisible(true);             }         });     } }  //  @jve:decl-index=0:visual-constraint='10,10' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The way it's done in django.views.generic.date_based is: {'date_field__range': (datetime.datetime.combine(date, datetime.time.min),… May 11, 2026 at 3:32 pm
  • added an answer Note: I am leaving my original answer intact, but don't… May 11, 2026 at 3:32 pm
  • added an answer I would say that the chain of responsability pattern could… May 11, 2026 at 3:32 pm

Related Questions

I recently started working on a large complex application, and I've just been assigned
I am getting data from a database through AJAX and appending tags to a
I'm running into a mental roadblock here and I'm hoping that I'm missing something
I'm having a problem where a collection of objects isn't being accessed correctly when

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.