plip1111
plip1111

Reputation: 13

How can i set a text for a cell in Javaeditor jTable?

I made a Programm to count the letters of a text and now i want to show the results of that in a table but i dont know and cant find any way to "manipulate" these cells! I have just started Programming a few weeks ago so please describe any answers given :) .

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;

    public class Test extends JFrame {

      private JTable jTable1 = new JTable(28, 2);
        private DefaultTableModel jTable1Model (DefaultTableModel) jTable1.getModel();
        private JScrollPane jTable1ScrollPane = new JScrollPane(jTable1);


      public Test() { 
        super();
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        int frameWidth = 696; 
        int frameHeight = 680;
        setSize(frameWidth, frameHeight);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (d.width - getSize().width) / 2;
        int y = (d.height - getSize().height) / 2;
        setLocation(x, y);
        setTitle("Test");
        setResizable(true);
        Container cp = getContentPane();
        cp.setLayout(null);

        jTable1ScrollPane.setBounds(112, 136, 452, 478);
        jTable1.getColumnModel().getColumn(0).setHeaderValue("Letter");
        jTable1.getColumnModel().getColumn(1).setHeaderValue("Count");
        cp.add(jTable1ScrollPane);

        setVisible(true);
      }

      public static void main(String[] args) {
        new Test();
      } 
    } 

Upvotes: 1

Views: 42

Answers (1)

camickr
camickr

Reputation: 324118

private JTable jTable1 = new JTable(28, 2);

You created a table with 28 rows and 2 columns.

So now you can change data using the setValueAt(...) method:

table.setValueAt("A", 0, 0);
table.setValueAt(5, 0, 1 );

So you would need to read the text file and parse the data to get your counts for each letter. I would suggest you could use a Hashtable to track each letter/count. Then you can iterate through the Hashtable to update the JTable.

cp.setLayout(null);
jTable1ScrollPane.setBounds(112, 136, 452, 478);

Don't use a null layout and random numbers to display components. Swing was designed to be used with layout managers.

Upvotes: 1

Related Questions