Re-Using JTableHeader - java

The code below displays 2 JTables.
As they both will have exactly the same headers I wanted, for the sake of efficiency, to reuse the header from the first table.
However running the code results in the header appearing in the second table but not in the table it came from originally.
I am less interested in work-arounds, but - for the sake of learning and understanding - more interested in finding out why the header does not appear in the first table.
Here is the code:
public class HeaderTest1 {
public void doTheTest() {
JFrame testFrame = new JFrame("Header Test");
JPanel pane = new JPanel();
Container theContentPane = testFrame.getContentPane();
BoxLayout box = new BoxLayout(pane, BoxLayout.Y_AXIS);
pane.setLayout(box);
theContentPane.add(pane);
String theData[][]
= {
{"One", "two", "3"},
{"four", "5", "six"},
{"7", "8", "9.0"},
{"£10.00", "11", "twelve"}
};
String columnNames[] = {"Column 1", "Column 2", "Column 3"};
JTable firstTable = new JTable(theData, columnNames);
JScrollPane thisScrollPane = new JScrollPane(firstTable);
JTableHeader thisTableHeader = firstTable.getTableHeader();
pane.add(thisScrollPane);
buildTheSecondTable(thisTableHeader, firstTable, columnNames, pane);
testFrame.pack();
testFrame.setVisible(true);
}
private void buildTheSecondTable(JTableHeader headerFromTheFirstTable,
JTable firstTable, String[] columnNames, JPanel pane) {
JTable secondTable = new JTable();
int columnCount = columnNames.length;
JScrollPane thisScrollPane = new JScrollPane(secondTable);
secondTable.setTableHeader(headerFromTheFirstTable);
Object[][] emptyData = new Object[1][columnCount];
for (int n = 0; n < columnCount; n++) {
emptyData[0][n] = "";
}
DefaultTableModel thisTableModel = new DefaultTableModel();
thisTableModel.setDataVector(emptyData, columnNames);
secondTable.setModel(thisTableModel);
secondTable.setLayout(firstTable.getLayout());
secondTable.setCellEditor(firstTable.getCellEditor());
pane.add(thisScrollPane);
}
public static void main(String[] args) throws SQLException, ParseException {
HeaderTest thisTest = new HeaderTest();
thisTest.doTheTest();
}
Any advice would be appreciated

A Swing component can only have a single parent so you can't share the table header component.
You can however share the Array of column names:
JTable firstTable = new JTable(theData, columnNames);
In your buildTheSecondTable method you have access to the array of column names so just use:
//DefaultTableModel thisTableModel = new DefaultTableModel();
DefaultTableModel thisTableModel = new DefaultTableModel(columnNames);
Then you can add data to the model and the model to the table.
Then reorder the code to create the JScrollPane after you add the model to the table.
Also, get rid of the table.setLayout() code. You would never use a layout manager on a table. You don't add components to the table. The table renders the data itself without using real components.

Related

Searching ArrayList using JTable and JButton

I have an ArrayList holding football matches and when the user types a date and presses "search" button, a new JTable opens showing you all matches played on that day. I have looped to get the date and compared it to the input inside the JTextField but it just gives me an empty table even if there is a record of a match played on the date the user enters. In this code below, I am just using hitting enter on JTextField to execute search because I do not know how to map JTextField to JButton. I have tried but it just prints the search Jbutton name.
public void searchMatch(ArrayList<Matches> searchMatch, String e)
{
DefaultTableModel searchModel = new DefaultTableModel();
for(int i = 0; i < searchMatch.size(); i++)
{
if(searchMatch.get(i).getM_date().equals(e))
{
System.out.println(searchMatch.get(i).getM_date());
String date = searchMatch.get(i).getM_date();
String teamName = searchMatch.get(i).getM_teamName();
String teamName2 = searchMatch.get(i).getM_teamName2();
int goalsScoredTeam1 = searchMatch.get(i).getGoalsTeam1();
int goalsScoredTeam2 = searchMatch.get(i).getGoalsTeam2();
Object[] row = {teamName, teamName2, goalsScoredTeam1, goalsScoredTeam2,date};
searchModel.addRow(row);
JTable searchTable = new JTable(searchModel);
searchTable.setFillsViewportHeight(true);
JPanel searchPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(searchTable);
searchPanel.add(scrollPane);
JFrame frame = new JFrame("Searched Matches");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
searchTable.setOpaque(true);
frame.setContentPane(searchPanel);
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
}
}
}
DefaultTableModel searchModel = new DefaultTableModel();
You TableModel has no columns to display.
Even though you add rows of data, none of the data can be displayed unless you also have defined the "column names" for the TableModel.
Your code should be something like:
String columnNames = { "Date", "Name", "..." };
DefaultTableModel searchModel = new DefaultTableModel(columnNames, 0);
Which will create an empty TableModel with just the column names. Your looping code will then add each row of data.
Note, you should also look at storing all the data in your TableModel and then just filter the TableModel. Read the section from the Swing tutorial on Sorting and Filtering for a working example.

Java Swing - JTable Not Showing in Scroll Pane

I created a class Cart and inside is a JTable and two ArrayLists. For some reason, my JTable is not displaying.
Here is my Cart Class:
class Cart {
ArrayList<Product> products = new ArrayList<>(); // Holds the products themselves
ArrayList<Integer> quantities = new ArrayList<>(); // Holds the quantities themselves
JTable prdTbl = new JTable(); // The GUI Product Table
DefaultTableModel prdTblModel = new DefaultTableModel(); // The Table Model
Object[] columns = {"Description","Price","Quantity","Total"}; // Column Identifiers
DecimalFormat fmt = new DecimalFormat("$#,##0.00;$-#,##0.00"); // Decimal Format for formatting USD ($#.##)
Cart() {
setTableStyle();
}
void renderTable() {
// Re-initialize the Table Model
this.prdTblModel = new DefaultTableModel();
// Set the Table Style
setTableStyle();
// Create a row from each list entry for product and quantity and add it to the Table Model
for(int i = 0; i < products.size(); i++) {
Object[] row = new Object[4];
row[0] = products.get(i).getName();
row[1] = products.get(i).getPrice();
row[2] = quantities.get(i);
row[3] = fmt.format(products.get(i).getPrice() * quantities.get(i));
this.prdTblModel.addRow(row);
}
this.prdTbl.setModel(this.prdTblModel);
}
void setTableStyle() {
this.prdTblModel.setColumnIdentifiers(columns);
this.prdTbl.setModel(this.prdTblModel);
this.prdTbl.setBackground(Color.white);
this.prdTbl.setForeground(Color.black);
Font font = new Font("Tahoma",1,22);
this.prdTbl.setFont(font);
this.prdTbl.setRowHeight(30);
}
JTable getTable() {
renderTable(); // Render Table
return this.prdTbl;
}
}
Note: some methods have been removed such as addProduct() and removeProduct(), as I feel they aren't necessary. If you need to see them, please ask.
Here is my initialize() method for the Swing Application Window:
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 590, 425);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Cart cart = new Cart();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, "cell 0 0,grow");
JPanel cartPanel = new JPanel();
tabbedPane.addTab("Cart", null, cartPanel, null);
cartPanel.setLayout(new MigLayout("", "[grow]", "[][grow]"));
JScrollPane scrollPane = new JScrollPane();
cartPanel.add(scrollPane, "cell 0 1,grow");
table = new JTable();
scrollPane.setViewportView(table);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] item = {"Macadamia", "Hazelnut", "Almond", "Peanut", "Walnut", "Pistachio", "Pecan", "Brazil"};
Double[] price = {2.00, 1.90, 1.31, 0.85, 1.12, 1.53, 1.25, 1.75};
int choice = (int) (Math.random() * item.length);
Product p = new Product(item[choice], price[choice]);
cart.addProduct(p);
table = cart.getTable();
}
});
cartPanel.add(btnAdd, "flowx,cell 0 0");
JButton btnRemove = new JButton("Remove");
cartPanel.add(btnRemove, "cell 0 0");
JButton btnClear = new JButton("Clear");
cartPanel.add(btnClear, "cell 0 0");
}
I'm not sure if I'm missing something here? It has worked fine like this in the past? I've also tried printing out values at table = cart.getTable();, and it seems to be receiving the values fine, so it leads me to believe it has something to do with the Swing initialize() rather than my Cart class, but just in case I posted the Cart class as well.
Are you sure you're adding the right table? Your code shows:
table = new JTable();
scrollPane.setViewportView(table);
I cannot see where's table declared, further more table contains nothing, no rows no columns, while inside actionListener you initialize table with a new instance:
table = cart.getTable();
but the scrollPane holds another instance of JTable.
It looks like you never associate your cart with your cartPanel; I think your problem is here:
JPanel cartPanel = new JPanel();
you make the new panel but never hook your cart to it. Looks good otherwise.
Good luck!

Can't update JTable's info

I create the JTable with data contained in an ArrayList and then add the JTable to a JScrollPane
ArrayList<Product> stock = new ArrayList<Product>(s);
String[] col = {"Nombre", "Cantidad", "Descripci\u00F3n", "Contenido"};
DefaultTableModel tableModel = new DefaultTableModel(col,0);
JTable table = new JTable(tableModel);
Collections.sort(stock, new Comparator<Product>() {
public int compare(Product p1, Product p2) {
return p1.getNombre().compareTo(p2.getNombre());
}
});
for (int i = 0; i < stock.size(); i++){
String nombre = stock.get(i).getNombre();
String cantidad = stock.get(i).getCantidad();
String descripcion = stock.get(i).getDescripcion();
String contenido = stock.get(i).getContenido();
Object[] data = {nombre, cantidad, descripcion, contenido};
tableModel.addRow(data);
}
table.getColumnModel().getColumn(0).setPreferredWidth(width*3/18);
table.getColumnModel().getColumn(1).setPreferredWidth(width/9);
table.getColumnModel().getColumn(2).setPreferredWidth(width*8/18);
table.getColumnModel().getColumn(3).setPreferredWidth(width/9);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
table.getTableHeader().setFont(new Font("Arial", Font.BOLD, f));
table.setFont(new Font("Arial", Font.PLAIN, f));
table.setBounds(1, 1, width*8/9, height);
table.setRowHeight(height/30);
table.setEnabled(false);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
for(int i = 0; i<4; i++){
table.getColumnModel().getColumn(i).setCellRenderer( centerRenderer);
}
JScrollPane sp = new JScrollPane(table);
frame.getContentPane().add(sp, BorderLayout.CENTER);
And then through a JButton I modify the contents of the ArrayList, then tell the TableModel that changes have been made and then revalidate() and repaint() the JTable
JButton btnVender = new JButton("Vender");
btnVender.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String n = JOptionPane.showInputDialog("Qué producto quieres vender?");
int x=1;
for(Product p : stock){
if(p.getNombre().equals(n)){
x=0;
String numero = JOptionPane.showInputDialog("Cuántos quieres vender?");
int num = Integer.parseInt(numero);
p.decrementar(num);
tableModel.fireTableDataChanged();
table.revalidate();
table.repaint();
break;
}
}
if(x==1){
JOptionPane.showMessageDialog(null, "No existe ese producto");
}
}
});
The problem is that the JTable won't update but the information does change in the ArrayList
The data for the JTable is not held in the ArrayList but rather resides in the table model itself. Bottom line: don't update the ArrayList but rather the table model itself, here a DefaultTableModel, and your JTable should then display the new data. Fortunately this should be easy to do as the table model has methods that allow you to extract data with getValueAt(...) and update values in its cells with setValueAt(...) as well as addRow(...) if a new row needs to be added.
Note that if you make changes through the DefaultTableModel, there's no need to directly call fireTableDataChanged(), and in fact you shouldn't call this method -- it's the model's responsibility to do so. Likewise there's no need to revalidate() or repaint() your JTable.
Please check out the DefaultTableModel API for the gory details.
If on the other hand you absolutely need to use the ArrayList as a data nucleus for your JTable, then you shouldn't use a DefaultTableModel but rather create your own TableModel by extending AbstractTableModel and using your ArrayList as its data nucleus. If you do this, then your model's code should take care to call the appropriate fire...(...) method after changing the model's state.

DefaultTableModel doesn't show columns

I have problem with DefaultTableModel it won't show me my columns in table, there is a part of code:
JTable table = new JTable() {
public boolean isCellEditable(int data, int columnNames) {
return false;
}
};
String columnNames[] = new String[] { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian", "asd", "asd" };
DefaultTableModel dtm = new DefaultTableModel(0, 0);
dtm.setColumnIdentifiers(columnNames);
table.setModel(dtm);
for (Reservation r : reservation) {
rez.add(new Reservation(r.getID(), r.getA(), r.getB(), r.getC(), r.getD(), r.getE(), r.getF()));
}
for (int i = 0; i < rez.size(); i++) {
int id = rez.get(i).getID();
String l = rez.get(i).getA();
String w = rez.get(i).getB();
String z = rez.get(i).getC();
String o = rez.get(i).getD();
String d = String.valueOf(rez.get(i).getE());
String g = rez.get(i).getF();
dtm.addRow(new Object[] { id, l, w, z, d,o,g });
}
JScrollPane sp = new JScrollPane(dtm);
add(table);
}
Im trying to make a dynamic table. Data will be from data base (posgreSQL) using hibernate, and thats fine, it work's but I cant see a column names from
String columnNames[] = new String[] { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian", "asd", "asd" };
Don't care about names of columns and name of getters i changed it for this post.
In addition I can't make it scrollable:
JScrollPane sp = new JScrollPane(dtm);
I'm not sure how this would be able to compile
JScrollPane sp = new JScrollPane(dtm);
add(table);
dtm is an instance of DefaultTableModel so it should never be possible to pass it to a JScrollPane,
Instead you should be using
JScrollPane sp = new JScrollPane(table);
add(sp);
See How to Use Tables and How to Use Scroll Panes for more details
Add the table rather than the TableModel to the JScrollPane
add(new JScrollPane(table));

How to convert an object into a JTable in Java

I recently started using OpenCSV's CSVReader to get data from a CSV file to a JTable in java, but I keep getting an error. DaTroop gave the answer to how to get data from the CSV here: import csv to JTable . In my case I copied the code
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
JTable table = new JTable(myEntries.toArray());
into Netbeans IDE 7.4, but I keep getting the error "Incompatible types - Object Cannot be converted to TableModel". in the
(myEntries.toArray());
Any ideas?
Thanks
As in comment, please find my whole code snippet. The JFrame works and views, so it should be called fine.
public static void LoadLog() throws FileNotFoundException, IOException {
LogViewer log = new LogViewer();
log.setVisible(true);
CSVReader reader = new CSVReader(new FileReader("testog.csv"));
String[][] rowData = {
{ "A", "B" },
{ "C", "D" }
};
List<String[]> myEntries = reader.readAll();
rowData = myEntries.toArray(new String[0][]);
String[] columnNames = { "Column 1", "Column 2" };
System.out.println(myEntries);
table1 = new JTable(rowData, columnNames);
log.pack();
log.setVisible(true);
}
First, note that reader.readAll() returns List<String[]>.
Also note that calling myEntries.toArray() returns Object[], and not Object[][], or more specifically String[][], which would be more preferable to work with.
The closest constructor for JTable that matches what you're trying to do would be this one:
JTable(Object[][] rowData, Object[] columnNames)
So you need to supply both rowData and columnNames. You can get the rowData like this:
List<String[]> myEntries = reader.readAll();
String[][] rowData = myEntries.toArray(new String[0][]);
Create a variable to hold the column names (or don't, your choice, but column names are still required):
String[] columnNames = { "Column 1", "Column 2" };
And then create your JTable:
JTable table = new JTable(rowData, columnNames);
Based on your comment, here's a short and simple code snippet for creating and displaying a table. As long as you initialize rowData and columnNames with the correct data, then it should work fine.
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[][] rowData = {
{ "A", "B" },
{ "C", "D" }
};
String[] columnNames = { "Column 1", "Column 2" };
JTable table = new JTable(rowData, columnNames);
frame.add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);

Categories

Resources