I have a problem creating a JTable in my GUI. The GUI is created in the main thread and enables a file to be opened. The file is then used to create a table model and add info to it. A JTable is then created with the table model and added to the GUI. My problem is that the GUI doesn't show. Code:
package example;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Example extends JFrame{
private JButton button;
private JTable table;
private DefaultTableModel model;
private String path = "C:/Users/gilbert/Documents/11111.xls";
public Example(){
super("Example");
setLayout(new BorderLayout());
button = new JButton("Start");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
createTableModel_Three_By_Six(path);
}
});
add(button,BorderLayout.NORTH);
setSize(400, 400);
}
public void createTableModel_Three_By_Six(String fpath){
model = new DefaultTableModel();
ExcelParser exPareser = new ExcelParser(fpath);
int rows = exPareser.getRowNumber();
String rowToAdd[] = new String[3];
int i, j = 0;
while(j < rows){
i= 0;
while(i < 3){
rowToAdd[i] = exPareser.accessRow(j);
i++;
j++;
if(j == rows){
if(i==1){
rowToAdd[1] = "";
rowToAdd[2] = "";
}
else if(i==2){
rowToAdd[2] = "";
}
}
}
model.addRow(rowToAdd);
}
table = new JTable(model);
add(new JScrollPane(table));
}
public static void main(String[] args) {
Example app = new Example();
app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
app.setVisible(true);
}
}
The problem, with your example, is the fact that there are no columns for the table, which means when you add the table to the frame, it doesn't know how to display the table contents.
So, by doing something as simple as...
model = new DefaultTableModel(new Object[]{"A", "B", "C"}, 0);
//...
table = new JTable(model);
add(new JScrollPane(table));
revalidate();
I was able to get the table to appear properly, with it's contents
Beware though, each time you call this method, a new JTable will be created. Instead, you should construct the JScrollPane and JTable at an earlier stage and simply update the TableModel
Related
How to update data to JTable from a vector after a button click? I have the code to add data to a JTable but it displays the same data for all the rows. Here's the following code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import mygui.MainParent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Test {
Vector<String> row;
DefaultTableModel DFMO;
Vector<Vector> rowData;
JFrame frame;
private int count = 0;
public static void main(String arg[]){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton mybtn = new JButton("Click Me");
frame.getContentPane().add(mybtn, BorderLayout.SOUTH);
row = new Vector<String>();
rowData = new Vector<Vector>();
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("Column One");
columnNames.addElement("Column Two");
columnNames.addElement("Column Three");
DFMO = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(DFMO);
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
mybtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
datachange();
}
});
datachange();
}
public void datachange(){
count++;
row.addElement("Row"+count+"-Column1");
row.addElement("Row"+count+"-Column2");
row.addElement("Row"+count+"-Column3");
rowData.addElement(row);
DFMO.fireTableDataChanged();
}
}
However, when I execute this code it doesn't show the updated row, even if the count value changes. The following image shows the output of the code.
Try this. You kept adding the same Vector
public void datachange(){
count++;
Vector<String> newRow = new Vector<>();
newRow.addElement("Row"+count+"-Column1");
newRow.addElement("Row"+count+"-Column2");
newRow.addElement("Row"+count+"-Column3");
DFMO.addRow(newRow);
//rowData.addElement(row);
DFMO.fireTableDataChanged();
}
You are adding the data to the Vector object (rowData).
What you should do is add the data directly to the DefaulTableModel object.
Replace
rowData.addElement(row);
with
DMFO.addRow(row);
in your datachange() method.
This has been asked many times already, but i really cant solve this problem.
I'm trying to refresh a table in a JFRame while it is running after i added a row so I dont have to restart it.I've tried the following method, but it still doesnt work.
The method is used in a ActionListener.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JButton;
#SuppressWarnings("serial")
public class GUI2 extends JFrame {
private static JPanel contentPane;
private static JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
GUI2 frame = new GUI2();
}
/**
* Create the frame.
*/
public GUI2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
setContentPane(contentPane);
table = new JTable();
contentPane.add(table, BorderLayout.CENTER);
JButton btnRefresh = new JButton("Refresh");
contentPane.add(btnRefresh, BorderLayout.NORTH);
btnRefresh.addActionListener(new Refresh());
}
public class Refresh implements ActionListener{
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}
public static void repaintGUI() {
contentPane.repaint();
contentPane.validate();
table.repaint();
table.validate();
}
}
Something similar to that. The Button is pressed after i have changed something in the table.
Your posted code doesn't work well as an example since the JTable is not visualized, but if you give the contentPane a BorderLayout, the JTable displays, and if refresh is pushed, then the GUI does in fact repaint, but as expected, it doesn't change its appearance since a simple repaint or revalidate won't change appearances if nothing else is done.
OK, here's where I need to guess since your question is still lacking key details (in the future, please ask it with an eye towards our point of view: people who have no idea what your current program does, looks like or what your problem is), but if the problem is that you're trying to reset a JTable's data, then the solution is to do just that -- reset your JTable's model by calling setRowCount(0) which will remove all data from the JTable, if it uses a DefaultTableModel. If it doesn't use the DefaultTableModel, then make sure to either give the JTable a new table model, or else call a method of your own table model that removes all data.
For instance:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class GUI3 extends JPanel {
private static final String[] COLS = { "Foo", "Bar", "Baz" };
private static final int ROWS = 5;
private DefaultTableModel tableModel = new DefaultTableModel(COLS, ROWS);
private JTable table = new JTable(tableModel);
private ResetAction resetAction = new ResetAction("Reset", KeyEvent.VK_R);
private RandomizeAction randomizeAxn = new RandomizeAction("Randomize",
KeyEvent.VK_Z);
public GUI3() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(resetAction));
btnPanel.add(new JButton(randomizeAxn));
setLayout(new BorderLayout());
add(new JScrollPane(table));
add(btnPanel, BorderLayout.PAGE_END);
}
private class ResetAction extends AbstractAction {
public ResetAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
tableModel.setRowCount(0);
}
}
private class RandomizeAction extends AbstractAction {
private Random random = new Random();
public RandomizeAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
public void actionPerformed(ActionEvent e) {
tableModel.setRowCount(ROWS);
int rows = ROWS;
int columns = tableModel.getColumnCount();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
int data = random.nextInt(10);
tableModel.setValueAt(data, row, col);
}
}
};
}
private static void createAndShowGui() {
GUI3 mainPanel = new GUI3();
JFrame frame = new JFrame("GUI3");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
I get an error when I remove a row a sorted row in a JTable.
The error appears only when the table is sorted, and I know where the error source is:
the method updateRowHeights() in the tableChanged causes an Exception java.lang.ArrayIndexOutOfBoundsException.
I guess that the line int rowHeight = table.getRowHeight(); causes the problem,
but I don't know why.
Here is my code:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TableExample {
String [] title = new String [] {"Title A", "Title B"};
Object [][] data = new String [][] {{"aaaaaaaaaaaa aaaaaa aaaaaaa", "bbbbbbbb bbbb bbbbbb bbbbbb"},
{"cccccccccc cccccccc ccccccc", "ddddddd ddd dddddddd dddddd"},
{"eeeeeeeeee eeeeeeee eeeeeee", "fffffff ffff ffffff fffffff"}};
private JTable table;
private JFrame frame;
private DefaultTableModel model;
private JScrollPane pane1;
TableExample() {} //constructor
public JPanel createTable() {
JPanel panel = new JPanel();
//creating tables and table models
model = new DefaultTableModel(data, title);
table = new JTable(model);
table.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
updateRowHeights();
}
});
//enable table sorting
table.setAutoCreateRowSorter(true);
pane1 = new JScrollPane(table);
pane1.setPreferredSize(new Dimension(300,300));
updateRowHeights();
panel.add(pane1);
//delete a row after del keystroke
keyBindings();
return panel;
}
void showTable() {
//create and show frame
JPanel testPanel = createTable();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testPanel);
frame.pack();
frame.setVisible(true);
}//showTable
void updateRowHeights() {
for (int row = 0; row < table.getRowCount(); row++) {
int rowHeight = table.getRowHeight();
Component comp = table.prepareRenderer(table.getCellRenderer(row, 1), row, 1);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
table.setRowHeight(row, rowHeight);
}
}
void keyBindings() {
int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
InputMap inputMap = table.getInputMap(condition);
ActionMap actionMap = table.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
actionMap.put("delete", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
model.removeRow(row);
}
});
}
public static void main(String[] args) {
TableExample example = new TableExample();
example.showTable();
}//main
}//TableExample
How can I solve this problem?
As noted here, "When using a sorter, always remember to translate cell coordinates." In your delete action, for example,
row = table.convertRowIndexToModel(row);
A similar problem afflicts updateRowHeights(), although I did not pursue this.
Also consider overriding getPreferredScrollableViewportSize(), instead of calling setPreferredSize(); more details here.
As tested:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TableExample {
String[] title = new String[]{"Title A", "Title B"};
Object[][] data = new String[][]{
{"aaaaaaaaaaaa aaaaaa aaaaaaa", "bbbbbbbb bbbb bbbbbb bbbbbb"},
{"cccccccccc cccccccc ccccccc", "ddddddd ddd dddddddd dddddd"},
{"eeeeeeeeee eeeeeeee eeeeeee", "fffffff ffff ffffff fffffff"}};
private JTable table;
private JFrame frame;
private DefaultTableModel model;
private JScrollPane pane1;
public JPanel createTable() {
JPanel panel = new JPanel();
//creating tables and table models
model = new DefaultTableModel(data, title);
table = new JTable(model);
//enable table sorting
table.setAutoCreateRowSorter(true);
pane1 = new JScrollPane(table);
panel.add(pane1);
//delete a row after del keystroke
keyBindings();
return panel;
}
void showTable() {
//create and show frame
JPanel testPanel = createTable();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testPanel);
frame.pack();
frame.setVisible(true);
}//showTable
void keyBindings() {
int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
InputMap inputMap = table.getInputMap(condition);
ActionMap actionMap = table.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
actionMap.put("delete", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
row = table.convertRowIndexToModel(row);
model.removeRow(row);
}
});
}
public static void main(String[] args) {
TableExample example = new TableExample();
example.showTable();
}//main
}//TableExample
I would like the JTable to autoscroll to the bottom whenever I add a new column and show the last 10 rows. However, I have the option of scrolling to anywhere I want (mouse listener?). Do you know how to do that? Here's the code I have so far. It builds a JTable and adds a new row for every mouse click on the JButton.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class sampleGUI extends JFrame implements ActionListener {
private JButton incrementButton;
private JTable table;
private DefaultTableModel model;
private int count;
private JScrollPane scroll;
public sampleGUI() {
JFrame frame = new JFrame("sample frame");
frame.setLayout(new BorderLayout());
incrementButton = new JButton("Increase the count!");
model = new DefaultTableModel();
model.addColumn("column 1");
table = new JTable(model);
frame.add(incrementButton, BorderLayout.NORTH);
scroll = new JScrollPane(table)
frame.add(scroll, BorderLayout.CENTER);
count = 0;
incrementButton.addActionListener(this);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#Override
public synchronized void actionPerformed(ActionEvent e) {
if (e.getSource() == incrementButton) {
count++;
model.addRow(new Object[] { count });
}
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
sampleGUI gui = new sampleGUI();
}
});
}
}
Thanks!
Required to change selection in JTable, add code line
table.changeSelection(table.getRowCount() - 1, 0, false, false);
to
public (synchronized) void actionPerformed(ActionEvent e) {
I would like the JTable to autoscroll to the bottom whenever I add a new column
I assume you mean scroll to the bottom when you add a new row?
model.addRow(new Object[] { count });
table.scrollRectToVisible(...);
You forget add JScrollPane to the table :
//...
frame.add(new JScrollPane(table), BorderLayout.CENTER);
//...
and don't forget
import javax.swing.JScrollPane;
The program consists of a tabbed view with a few JTextField's and JButtons in the first two and 2 JTables and JButton in the third. The buttons work fine in the first two tabs, but on the third it is un-clickable. I tried changing the button to something else like a JComboBox or JTextField but those are uneditable and unclickable also. Setting the setEnabled() and setEditable() functions to true doesn't seem to work. I have no idea what's going on.
The only thing i have done different from normal is this piece of code to forcefully repaint the tab when I click on a cell in the table. But even when this hasn't been called i still cant
ContentIWantToRepaint.this.paintImmediately(ContentIWantToRepaint.this.getVisibleRect());
Some other information about the layout:
The contents of the tab are held in a JPanel with a GridBagLayout
JTextField
Table Header
Table Contents
JTextField
Table Header
Table Contents
Button
Any suggestions?
checkOutPanel.java
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
public class checkOutPanel extends JPanel {
private JTable userTable;
private JTable bookTable;
private JScrollPane scrollUserTable, scrollBookTable;
private JLabel userLabel, bookLabel;
private DefaultTableModel userTableModel = new DefaultTableModel(){
public boolean isCellEditable(int row, int column){return false;}
};
private DefaultTableModel bookTableModel = new DefaultTableModel(){
public boolean isCellEditable(int row, int column){return false;}
};
private int selectedUser, selectedBook;
private JComboBox date_month, date_day, date_year;
public checkOutPanel(){
JPanel mainGrid = new JPanel();//panel that will hold all stuff in tab
JPanel buttonPanel = new JPanel();//panel for holding buttons
mainGrid.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx=0; c.gridy=0; //first row first column
c.anchor = GridBagConstraints.WEST;//align left
c.fill = GridBagConstraints.HORIZONTAL;
//1 row, 2 columns, 15 horizontal padding, 0 vertical padding
//buttonPanel.setLayout(new GridLayout(1,2,15,0));
//labels to describe what the user has to do
userLabel = new JLabel("User :");
bookLabel = new JLabel("Book :");
JLabel dateLabel = new JLabel("Date :");
JPanel datePanel = new JPanel();
bookTable = new JTable(bookTableModel);
bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userTable = new JTable(userTableModel);
userTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Dimension d = new Dimension(500, 120);
scrollBookTable = new JScrollPane(bookTable);
scrollBookTable.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBookTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollBookTable.setMaximumSize(d);
scrollBookTable.setMinimumSize(d);
scrollBookTable.setPreferredSize(d);
scrollUserTable = new JScrollPane(userTable);
scrollUserTable.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollUserTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollUserTable.setMaximumSize(d);
scrollUserTable.setMinimumSize(d);
scrollUserTable.setPreferredSize(d);
updateTables();
//String[] month = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
//Vector<Integer> days = new Vector<Integer>();
//for (int i=1 ; i<=31 ; i++) { days.add(i); }
//Vector<Integer> years = new Vector<Integer>();
//for (int i=2012 ; i>=1900 ; i--) { years.add(i); }
//date_month = new JComboBox(days);
JButton testButton = new JButton("WORK!!!!!!!");
//date_day = new JTextField(6);
//date_year = new JTextField(6);
//datePanel.add(dateLabel);
//datePanel.add(date_month);
//datePanel.add(date_day);
//datePanel.add(date_year);
mainGrid.add(userLabel, c); c.gridy++;
mainGrid.add(userTable.getTableHeader(), c); c.gridy++;
mainGrid.add(scrollUserTable, c); c.gridy++;
c.insets = new Insets(15, 0, 0, 0);//put some padding between users and books
mainGrid.add(bookLabel, c); c.gridy++;
c.insets= new Insets(0,0,0,0);//back to no padding
mainGrid.add(bookTable.getTableHeader(), c); c.gridy++;
mainGrid.add(scrollBookTable, c); c.gridy++;
c.insets = new Insets(15, 0, 0, 0);//put some padding between books and date
mainGrid.add(testButton, c); c.gridy++;
this.add(mainGrid);//add contents to tab
userTable.addMouseListener(new mouseListener());
bookTable.addMouseListener(new mouseListener());
}
public class mouseListener implements MouseListener{
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {
if(e.getSource()==userTable){
selectedUser=userTable.getSelectedRow();
userLabel.setText("User : "+ (selectedUser!=-1 ? selectedUser : ""));
//don't know why i should need to use this, but it makes it work
checkOutPanel.this.paintImmediately(checkOutPanel.this.getVisibleRect());
}else if(e.getSource()==bookTable){
selectedBook = bookTable.getSelectedRow();
System.out.println("Date Month : "+date_month.getSelectedItem().toString());
bookLabel.setText("Book : "+ (selectedBook!=-1 ? selectedBook : ""));
//don't know why i should need to use this, but it makes it work
checkOutPanel.this.paintImmediately(checkOutPanel.this.getVisibleRect());
}
}
}
private void updateTables(){
bookTableModel.setDataVector(fillBookData(SystemStart.getDbName()), fillBookColNames());
userTableModel.setDataVector(fillUserData(SystemStart.getDbName()), fillUserColNames());
}
private Vector<String> fillUserColNames(){
Vector<String> colNames = new Vector<String>();
colNames.add("ID");
colNames.add("First");
colNames.add("Last");
colNames.add("Phone");
colNames.add("Email");
colNames.add("Address");
colNames.add("City");
colNames.add("State");
colNames.add("Zip");
return colNames;
}
private Vector<String> fillBookColNames(){
Vector<String> colNames = new Vector<String>();
colNames.add("Title");
colNames.add("Format");
colNames.add("Author");
colNames.add("ISBN");
colNames.add("Year Pub.");
colNames.add("Publisher");
colNames.add("Call #");
colNames.add("Copy #");
return colNames;
}
private Vector<Vector<String>> fillUserData(String dbName){
Vector<Vector<String>> v = new Vector<Vector<String>>();
Vector<String> u;
for (int i=0; i<10; i++){
u = new Vector<String>(9);
u.add(""+i);
u.add("First");
u.add("Last");
u.add("Phone");
u.add("Email");
u.add("Address");
u.add("City");
u.add("State");
u.add("Zip");
v.add(u);
}
return v;
}
private Vector<Vector<String>> fillBookData(String dbName) {
Vector<Vector<String>> v = new Vector<Vector<String>>();
Vector<String> b;
for (int i=0; i<10; i++){//only include books that arent check out
b = new Vector<String>(8);
b.add("Title "+i);
b.add("Format");
b.add("Author");
b.add("ISBN");
b.add("Year Pub.");
b.add("Publisher");
b.add("Call #");
b.add("Copy #");
v.add(b);
}
return v;
}
}
Use this file to see how it doesn't work:
import javax.swing.JFrame;
public class SystemStart{
private static String dbName = "dbnamehere.db";
public static void main(String[] args) {
//new TabbedFrame("Libary System");
JFrame jf = new JFrame("WORK!");
jf.add(new checkOutPanel());
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static String getDbName(){return dbName;}
}
You're adding to the panel a label, then the table's header and then the scrollpane which contains the table (...and the table header). Remove the two lines where you're adding to mainGrid the table headers (you should just add the scrollpanes):
mainGrid.add(userTable.getTableHeader(), c); c.gridy++;
mainGrid.add(bookTable.getTableHeader(), c); c.gridy++;
Your button will work now.