Clearing a JScrollPane but can't then 'attach' tables - java

I am creating an application where I have 3 Panes with buttons which dynamically create buttons in the next pane depending on the selection, clicking the last button shows a table of data brought up via an SQL query:
[buttonPane1][buttonPane2][buttonPane3][table]
If a user has clicked a button on all 3 panes and then wants to change their choice on buttonpanel1, it will bring up the choices in buttonpanel2 and using
buttonPanel3.removeAll();
buttonPanel3.repaint();
I can clear the third button panel, my problem is how to clear the table. I want to remove it from the Table ScrollPanel however if I try
tableScrollPanel.removeAll();
it just means that the table never shows.
How can I remove any current table but allow a table to be 'reattached' I am doing this to create and 'attach' the table
jTableTemp.setModel(new DefaultTableModel(
tableContent, tableTitles));
tableScrollPanel.setViewportView(jTableTemp);
Thanks Very Much

Try setting the table model to a DefaultTableModel with empty data and the original headers, then repaint. As long as you have a JScollPane wrapped around the JTable, the headers should show up, assuming this is what you want.
Other wise, you can set the viewport to a new instance of a JTable with new headers.

Related

CheckBox Selection in JTable

I want, in a JTable, has a column that when it is clicked, a JPanel to appear with the names of all the columns, and it can select (with JCheckBox) which we want to continue in the JTable. It would be a column with "#".
Just create such a column in your data model implementation, with a custom artificial data object. Then register an editor (setDefaultEditor) in the jtable to show a checkbox or open a dialog with a checkbox. based on the user selection you may then alter your model (add or remove columns) and fire an according changed event.
Not exactly what you asked for but maybe you can use the Table Column Manager.
The Table Column Manager manages which columns are visible in the table. You invoke the Table Column Manager by right clicking on any column in the column header. You are presented with a popup menu that uses check boxes to hide/show columns.

multiple Jpanel with same functionality buttons

i want to create JFrame with multiple panel each panel displaying some information from database (say product_id and description ) and each frame have a button(add to cart) which will add the information from that panel to cart table in my database how will i implement this?
I would try to simply things by using a JTable to display such data, with each column displaying a database field. I'd add an extra column, say a Boolean column that displays as a check box, and then assess the state of that column when needing to decide which to add to a cart.
Edit You ask:
how will i know how much quantity of that product is selected,
Then add a quantity field as well, perhaps one that uses a JSpinner as its editor.
and also i am thinking to add image of that product also can i do it in Jtables
Absolutely. It knows how to display ImageIcons for instance. Please have a look at my answer to another question for an example.
You should create a JPanel (called PanelButton) which contents [Add to cart] button.
In other JPanels, you add PanelButton.
So, by this way, you can reuse your button without implement a lot of [Add to cart] button for each panel.

Insert/add data to JTable & Database using a popup frame after clicking the "add" button

May I know how do I make a popup frame to do the insert of data since I'd like my GUI to popup another frame to ask for user input to the database and as well as the JTable? Thank you!
What I don't get is how to make use of the add button, then insert the entry and make the listener notice about "e.getType()==TableModelEvent.INSERT".
Edit:
At the same panel as the JTable, I would have an add button at the bottom. This add button would then in another 'frame' ask users to enter the items to add to the database as well as adding and refreshing the JTable.
you must use a DataModel with your JTable, and add item to your datamodel and then update your JTable with yout DataModel.
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");
// Append a row
model.addRow(new Object[]{"v1", "v2"});
inserting new data from external class into jtable
Have a look at this , I hope your problem will be solved , Pass the Table reference in the constructor of your JFame/JDialogue (Better if you use JDialogue Instead of JFrame click here to know some good reasons) and modify table model accordingly

Display values of a selected row from a dynamically varying JTable

I have to develop a front end in Swing.I have a JFrame in Netbeans with 3 panes in it.
JScrollPane 1:it contains a JTable
JScrollPane 2: It should display the value of the fields of the selected row
ListPane : which contains the list of tables from which a user chooses a table to be displayed.
Now since the content of JTable varies(the no of rows and columns also vary) dynamically based on the table chosen by user I can't drag and drop the TextBoxes in the 3rd scroll pane to display the selected row's values.It would be helpful if anybody can suggest a way to do it or any pointers to problems that deal with similar issue
Add a ListSelectionListener to your JList. When a particular table is selected from the list, use setModel() to change the TableModel of your JTable to one that is correct for the chosen table. A related example using setModel() is shown here.

Dynamic fields addition in java/swing form

I'm pretty new to java, and using netbeans for design a UI.
What I am trying to do is...
in the form. there are a jComboBox and a JTextField, where user can input the service he is choosing and an observation. So far so good. JComboBox is populated from database entries.
The problem is, a user can input N different services at once (there are too much to be a bunch of checkboxes). I was thinking into add a "[+]" button (along with a "[-]" for removal). Thus, users click on [+] and another new line with a jcombobox + jtextfield appear right below the previous ones.
I'm stucked at this point. On [+] button ActionPerformed I just can't clone and add previous nodes. Any idea on how proceed.
My background is webdev. Doing this with javascript would be really quick. Well, I think you already know what I'm trying to do. Waiting for some light. Thx.
You're on the right track. Here's some source code to give you some ideas
The basic idea is that the EntryList is responsible for keeping track of the rows to display; each row has a plus/minus button, and then delegates out the actual adding/removing to this EntryList. It also exposes methods to disable the minus/plus button so that the list view can ensure that you don't remove a single entry (so that you don't have an empty display)
This doesn't work perfectly; you'll notice you need to resize the frame to get the new rows to show up correctly. But this should be enough to get you started.
Create your main panel to use a layout manager that displays component horizontally. The Box class is easy to use for this. Then you just create a new panel with the components you want to display and add this panel to your main panel. Something like:
JComboBox checkBox = new JComboBox(...);
JTextField textField = new JTextField(...);
JPanel row = new JPanel();
row.add( comboBox );
row.add( textfield );
mainPanel.add( row );
mainPanel.revalidate();

Categories