How to change column model of my TableModel dynamically - java

I have a simple Java Desktop application that show a JTable with a custom TableModel.
In base of a boolean variables, I want to show a Table with different column.
But I'm not able to do this.
This is my code:
static String[] ColName = { "Cod.Articolo","Nome Articolo","Cod.Barre", "Qtà.iniziale","Scontrini(-)","Bolla(-)","Fattura(-)","DDT(-)","NC(+)","Carico(+)"};
static String[] ColNameNero = { "Cod.Articolo","Nome Articolo","Cod.Barre", "Qtà.iniziale","Scontrini(-)","Scontrini Nero(-)","Bolla(-)","Fattura(-)","DDT(-)","NC(+)","Carico(+)"};
public void creaTabellaMerci(boolean isNero){
try{
if(isNero)
tableMovimentiMagazzinoMerci = new MyTableModelMovimentiMagazzinoMerci(ColNameNero,isNero);
else
tableMovimentiMagazzinoMerci = new MyTableModelMovimentiMagazzinoMerci(ColName,isNero);
tableMovimentiMerci = new DefaultTableCustom(tableMovimentiMagazzinoMerci);
sorter = new TableRowSorter<MyTableModelMovimentiMagazzinoMerci>(tableMovimentiMagazzinoMerci);
tableMovimentiMerci.setRowSorter(sorter);
jScrollPaneAmministrazione = new javax.swing.JScrollPane();
jScrollPaneAmministrazione.setViewportView(tableMovimentiMerci);
jScrollPaneAmministrazione.setPreferredSize(dTabella2);
jScrollPaneAmministrazione.getViewport().add(tableMovimentiMerci);
tableMovimentiMagazzinoMerci.fireTableDataChanged();
tableMovimentiMerci.repaint();
}catch(Exception e){
log.logStackTrace(e);
}
}
Now at first time, I call the method with variables isNero = true. At the second time, I call the same method with variables isNero = false but the columns not changes.
How can I fix it ?

jScrollPaneAmministrazione = new javax.swing.JScrollPane();
You create a new JScrollPane, but you never add the scroll pane to the frame. Changing the value of the reference variable does NOT add the component to the frame.
Don't create a new JTable or JScrollPane!
Instead you can just update the TableModel of the table that is currently display on the frame:
//tableMovimentiMerci = new DefaultTableCustom(tableMovimentiMagazzinoMerci);
tableMovimentiMerci.setModel( tableMovementiMagazzinoMerci );
sorter = new TableRowSorter<MyTableModelMovimentiMagazzinoMerci>(tableMovimentiMagazzinoMerci);
tableMovimentiMerci.setRowSorter(sorter);
//jScrollPaneAmministrazione = new javax.swing.JScrollPane();
//jScrollPaneAmministrazione.setViewportView(tableMovimentiMerci);
//jScrollPaneAmministrazione.setPreferredSize(dTabella2);
//jScrollPaneAmministrazione.getViewport().add(tableMovimentiMerci);
//tableMovimentiMagazzinoMerci.fireTableDataChanged();
//tableMovimentiMerci.repaint();

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!

Adding data to jTable from Database (via Netbeans) no errors, but table disappears

somehow in debugging data is fully retrieved and resultModel actually has got column names, and data for rows. Although when compiled and ran in Netbeans after a search, table disappears, no data is shown even column names. Here is the code:
private void search(){
String[][] rowData = new String[0][4];
String[] columns = {"appointmentid", "fname", "lname", "registration", "make", "model", "engine", "year", "mileage", "type", "date", "time"};
resultModel = new DefaultTableModel(rowData,columns);
add(new JScrollPane(jTable1));
jTable1 = new JTable(resultModel);
jTable1.setAutoCreateRowSorter(true);
try{
Model_Customer[] appointment = Controller_ManageCustomer.FindCustomers(Searchtxt.getText());
resultModel.setRowCount(0);
for(int i = 0; i < appointment.length; i++)
resultModel.insertRow(i,new Object[]{appointment[i].GetID(),appointment[i].GetFName(), appointment[i].GetLName(), appointment[i].GetRegistration(), appointment[i].GetMake(), appointment[i].GetModel(), appointment[i].GetEngine(), appointment[i].GetYear(), appointment[i].GetMileage(), appointment[i].GetType(), appointment[i].GetDate(), appointment[i].GetTime()});
}catch(Exception ex){
JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
DefaultTableModel resultModel
It could just be me...but this looks very suspicious...
add(new JScrollPane(jTable1));
jTable1 = new JTable(resultModel);
Not to mention, you go a build and nice new DefaultTableModel but don't actually apply it to anything that is actually on the screen...
Try something more like...
resultModel = new DefaultTableModel(rowData,columns);
try{
Model_Customer[] appointment = Controller_ManageCustomer.FindCustomers(Searchtxt.getText());
resultModel.setRowCount(0);
for(int i = 0; i < appointment.length; i++) {
resultModel.insertRow(i,new Object[]{appointment[i].GetID(),appointment[i].GetFName(), appointment[i].GetLName(), appointment[i].GetRegistration(), appointment[i].GetMake(), appointment[i].GetModel(), appointment[i].GetEngine(), appointment[i].GetYear(), appointment[i].GetMileage(), appointment[i].GetType(), appointment[i].GetDate(), appointment[i].GetTime()});
}
if (jTable1 == null) {
jTable1 = new JTable(resultModel);
add(new JScrollPane(jTable1));
} else {
jTable1.setModel(resultModel);
}
}catch(Exception ex){
JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
Now, personally, I would simply create the JTable and add it to the screen, and leave it alone, and simply change the TableModel when ever you wanted to update it's content...
Swing uses a form of the MVC paradigm, which means it separates the view from the model, meaning that when you want to change what the JTable is showing (the view), you simply change the model...

Trouble with Java's Vector's and JTable

I'm tinkering with JTables and Vectors for the first time in Java, and I've hit an interesting snag. My code compiles correctly, but when I go to run it, I get the following exception:
Exception in thread "main" java.lang.ClassCastException:
java.lang.String cannot be cast to java.util.Vector
I don't see anywhere where I'm casting, so I'm a bit confused.
Vector<String> columnNames = new Vector<String>();
columnNames.add("Tasks");
Vector<String> testing = new Vector<String>();
testing.add("one");
testing.add("two");
testing.add("three");
table = new JTable(testing, columnNames); // Line where the error occurrs.
scrollingArea = new JScrollPane(table);
My goal is to have a table of JPanels, but I have the same type of error when I try to use a Vector of < taskPanel > Here's the class that extends JPanel:
class taskPanel extends JPanel
{
JLabel repeat, command, timeout, useGD;
public taskPanel()
{
repeat = new JLabel("Repeat:");
command = new JLabel("Command:");
timeout = new JLabel("Timeout:");
useGD = new JLabel("Update Google Docs:");
add(repeat);
add(command);
add(timeout);
add(useGD);
}
}
You need to use a Vector of Vectors here:
Vector<Vector> rowData = new Vector<Vector>();
rowData.addElement(testing);
JTable table = new JTable(rowData, columnNames);
For a multi-column Vector table model, see this example.
Your testing vector should be vector of vectors as each row is supposed to contain data for all columns e.g.
Vector<Vector> testing = new Vector<Vector>();
Vector<String> rowOne = new Vector<String>();
rowOne.add("one");
Vector<String> rowTwo = new Vector<String>();
rowTwo.add("two");
Vector<String> rowThree = new Vector<String>();
rowThree.add("three");
testing.add(rowOne);
testing.add(rowTwo);
testing.add(rowThree);
table = new JTable(testing, columnNames); // should work now
scrollingArea = new JScrollPane(table);
The casting is the < String >. You can't have Vector Strings at the moment. Take a look at this.

Creating multi-line header for JTable

I use JTable GUI component with NetBeans.
I want to create multi-line headers
I am able to this, when I create custom renderer for table headers
BUT I don't want to, because I like default one (I use it everywhere else in program).
So the problem is : How to make
header heigher?
My code :
String headers[] = new String[3];
headers[0] = "Header1";
headers[1] = "Header2";
headers[2] = "<html><center>Long<br>Centered</br></center></html>";
DefaultTableModel dtm = new DefaultTableModel();
dtm.setColumnIdentifiers(headers);
dtm.setRowCount(5);
jTable1.setModel(dtm);
jTable1.getTableHeader().setPreferredSize(
new Dimension(jTable1.getColumnModel().getTotalColumnWidth(),32));
Try the following:
table.getTableHeader().setPreferredSize(
new Dimension(table.getColumnModel().getTotalColumnWidth(), 32));
This will change the size of the header:
To center the text, you can use the <center>-tag:
headers[2] = "<html><center>Long Centered<br>Header";
EDIT:
The alignment is set by your LookAndFeel. WindowsTableHeaderUI for example explicitly calls
setHorizontalAlignment(LEADING);
The easiest way to solve this is:
(JLabel)table.getTableHeader().getDefaultRenderer())
.setHorizontalAlignment(SwingConstants.CENTER);
Note that this change is lost when you change your LookAndFeel afterwards.
hi guys i tried this and it works:
DefaultTableCellRenderer r;
String headers[] = new String[3];
headers[0] = "Category";
headers[1] = "Item";
headers[2] = "<html><center>Packaging<br>unit</center></br></html>";
DefaultTableModel dtm = new DefaultTableModel();
table.setModel(dtm);
r = (DefaultTableCellRenderer)table.getTableHeader().getDefaultRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
dtm.setColumnIdentifiers(headers);
table.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(),32));

Categories

Resources