Giga Tocka Giga Tocka - 1 month ago 11
Java Question

Why can't I use DefaultTableModel? Am I missing something Obvious? (Java)

Here is my code:

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

class Test{
static CardLayout cardLayout;
static JPanel card = new JPanel();

public static void main(String[] args) {
JFrame frame = new JFrame("AddressBook");
JPanel contentPane = (JPanel)frame.getContentPane();
card.setLayout(cardLayout = new CardLayout());

JPanel cardTop = new JPanel();
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Name");
model.addColumn("Number");
String[] John = {"John", "1234"};
model.addRow(John);
String[] Beth = {"Beth", "4444"};
model.addRow(John);
JTable table = new JTable(model);
JScrollPane jsp = new JScrollPane(table);
cardTop.add(jsp);


card.add("Card Top", cardTop);
contentPane.add(card);

frame.setVisible(true);
frame.setSize(507, 191);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
}


When I try to compile, it says it doesn't recognize DefaultTableModel. Also, the code above is part of my main and I'm sure I've imported the right libraries.

Here is the error:

Test.java:15: error: cannot find symbol
DefaultTableModel model = new DefaultTableModel();
^
symbol: class DefaultTableModel
location: class Test
Test.java:15: error: cannot find symbol
DefaultTableModel model = new DefaultTableModel();
^
symbol: class DefaultTableModel
location: class Test
2 errors


Help please?

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

Isn't enough.

DefaultTableModel lives in the javax.swing.table package, you need to include it in with your imports...

import javax.swing.table.*
Comments