Note that there are some explanatory texts on larger screens.

plurals
  1. POFormat the JTable as shown in Text File?
    text
    copied!<p>Hi i am reading a text file into <code>JTable</code>, but here is what my JTable Looks Like Now: <img src="https://i.stack.imgur.com/YwOyS.jpg" alt="enter image description here"></p> <p>How can I format it correctly and allow <code>JTable</code> to be editable by users?</p> <p>My Text File: File Name(people.txt)</p> <pre><code>COLUMN_NAME COLUMN_TYPE IS_NULLABLE COLUMN_KEY COLUMN_DEFAULT EXTRA Names VARCHAR(500) NO Address VARCHAR(500) NO </code></pre> <p>Coding so far:</p> <pre><code> import java.io.*; import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; import javax.swing.table.*; public class stackq extends AbstractTableModel { Vector data; Vector columns; public stackq() { String line; data = new Vector(); columns = new Vector(); try { FileInputStream fis = new FileInputStream("D:/joy/text/people.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); StringTokenizer st1 = new StringTokenizer(br.readLine(), " "); while (st1.hasMoreTokens()) columns.addElement(st1.nextToken()); while ((line = br.readLine()) != null) { StringTokenizer st2 = new StringTokenizer(line, " "); while (st2.hasMoreTokens()) data.addElement(st2.nextToken()); } br.close(); } catch (Exception e) { e.printStackTrace(); } } public int getRowCount() { return data.size() / getColumnCount(); } public int getColumnCount() { return columns.size(); } public Object getValueAt(int rowIndex, int columnIndex) { return (String) data.elementAt((rowIndex * getColumnCount()) + columnIndex); } public String getColumnName(int i){ return (String)columns.get(i); } public static void main(String s[]) { stackq model = new stackq(); JTable table = new JTable(); table.setModel(model); JScrollPane scrollpane = new JScrollPane(table); JPanel panel = new JPanel(); panel.add(scrollpane); JFrame frame = new JFrame(); frame.add(panel, "Center"); frame.pack(); frame.setVisible(true); } } </code></pre> <p>Thank You So much.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload