I have a problem in my table model to update the data I print in it.
I have the class AgendaTableModel who is the table model, and the class Screen that is the main UI, when I press the button 'Listar Contatos' the table model should appear on JScrollPane in the center of the JFrame, but it continues blank.
What should be the problem ?
You should really post the code or better an SSCCE.
Here's the Oracle's tutorial on JTable.
I'll give you some hints:
Each JTable has a TableModel associated
You don't display the TableModel but the JTable, that is a view of your model
When you add components dynamically you should revalidate the parent component, so if you are adding a JTable somewhere, try to revalidate its container.
If you are trying to add the JTable to an already existing JScrollPane (empty or containing something else), consider to instantiate a new JScrollPane rather than updating its content.
Related
I didn't write any code yet, so sorry about that but need some direction and clarification before proceeding. Can I create a dynamic link between with a JTable and Hashmap? So when ever my listenrs add something new or delete something, it will update the Hashmap and that would update the JTable, I was thinking of re-creating the JTable everytime a change happens? That is one of my buttons being pressed.
Any suggestions?
A JTable is just the visible component, the data is contained in a TableModel. Any changes inside the model will be reflected in the JTable itself. So you can create a TableModel that uses a HashMap internally.
Here's Oracle's table tutorial for more info
https://docs.oracle.com/javase/tutorial/uiswing/components/table.html
I want to print a Jpanel with two things in it.
I have a Jtable that could have as many as 500 row in it, and I need to be able to print all of them.
I have a JTextfield under the JTable in the JPanel.
I want to print both of these at the same time. The problem I am having is when I print the jtable it only prints the visible part. What I really care about is the content of the JTable and not the Table itself.
Edit: I use Netbeans to build my Gui so I don't really know how to display the code for the Panel, Table, and TextFields.
Here is a picture of the frame everything is in:
The table can have more rows than you can see at once. All the items here except for the button are in a Jpanel, so I need to print this jpanel. What I have found and tried doesn't print all of the jtable, just what is visible to the user.
When you call table.print() it will offer a dialog and you click print. I can't help you more until you post some code but this works for me.
So the code behind it is quite massive.
I have JTabbedPane with a Tab for each table(inside JSCrollPane ) and a Summary table which contains tables that uses the same models as models in the tabs. So when I add rows to the table everything renders properly. When one of the tables has its data changed everything renders OK , except the JButtons (the button in the first row always renders) . Clicking on the line fixes it , even if it changes again the button does not disappear.
When data changes I only fireDataChanged() for the model of contained tables, the model does fire repaint on the contained tables and the container table.
Each swing component can be in only one container. The problem comes if you try to put into two Jpanels, if you do that with 2 Jtables that use the same instance of a model ,what happens is originally everything renders properly , but will break as soon as you activate the component(button). It will stop rendering in the table where you pressed it , in some cases both.
I'm new to swing and I faced my first serious problem, here goes:
I have a JPanel with JTable and lots of checkboxes below the table. I'm trying to align the checkboxes below the table with checkbox located in the first table column. The problem is - this has to be done when model data changes.
Immediatly after the fireTableDataChanged() gets triggered in JTable there is no way to get the screen location of its custom cell renderer component (which in my case is JCheckBox).
As far as I understand this happens because JTable gets redrawn asynchronously. Whenever I try to get the location the IllegalComponentStateException gets thrown.
Any ideas on this are highly appreciated.
You shouldn't do it manually. You should use the appropriate layoutManager. Check this link for the different Layouts:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
This might help you to get screen location of row and column.
Rectangle rect= cartTable.getCellRect(row,column,true);
//cartTable-Jtable object name
int x=Double.valueOf(rect.getX()).intValue();
int y=Double.valueOf(rect.getY()).intValue();
I need a Container with similar JPanels lined up one below the other which can be selected. I could:
Use a JList with a custom renderer but those JPanels would be passive elements, that's not what I want.
Implement my own Container with 'active' JPanels but those would not be selectable. Or could they made selectable?
Maybe a MouseListener and access to the system default selected-background-colors could be a way but it seems a bit too much effort
Use a JTable or JTree with custom cell editors rendering the 'active' JPanel. But these active parts would only react at the 'second' click, first to activate the editor, second to perform the real action of the JPanel. this is also not acceptable.
To get a more visual impression, here is an example of what this could mean:
A JList containing list items which have each two functional JButtons.
As you've discovered, simply putting a JPanel inside a JList doesn't quite work as you'd like. The JPanel will be passive and won't receive events - essentially all that is happening is your JPanel is simply being drawn, it's not a living component.
Instead of using a JList to put your panels in a list, use a list-like layout manager, such as BoxLayout or GridLayout. If you want all your panels to be the same size, use GridLayout with only a single column.
How to Use GridLayout
How to use BoxLayout
I'm not sure I understand your "example". If you want two functional buttons, then use a JTable where the functional buttons are contained is separate columns. Then your data would be displayed in other columns.
Table Button Column shows how you can do this.
your question(s) isn't clear for me, maybe here
there is JTable, with one TableColumn but without TableHeader, contains JPanel with active JComponents inside (you can implements TableCellEditor for all JComponents) as JComboBox, JButton and JTextField