JButton inside JTable inside JTable inside JTabbedPane - java

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.

Related

(Java) Trying to format the layout of multiple JTables with other elements in a JPanel

I've been working on a Pokemon-themed quiz game in Java (modeled after Sporcle, if you're familiar). Pretty much everything works how I want it to, except for the layout of the different components of the program.
I've never been very good with the different layout managers, and I don't know how to get them to do what I need.
Here's what the window looks like right now:
Now, I'll play around with font sizes later, but the tables themselves look exactly how I want them to. The problem is, I want them to be under the text fields and buttons and stuff. Here's the portion of the code where I add all the components to my JPanel:
panel.add(label,FlowLayout.LEFT); //adding the "big question text"
panel.add(answerfield); //adding the JTextField
panel.add(correctAnswerTracker); //adding the "x / 151" text
for(int x = sPanes.length-1; x >=0; x--) //as you keep adding to left, it gets pushed over, so doing it backwards results in the correct order
panel.add(sPanes[x],FlowLayout.LEFT);
//each table is in a scrollPane, and all my scrollPanes are in the array sPanes, so I'm looping through that to add the tables
panel.add(startStopButton); //button that says "Start"
panel.add(exit); //button that says "Exit
panel.add(timer); //the timer
As you can see, the statements to add the text field, and correct answer tracker are all written before the add statement for the tables, and yet the tables are at the top. Additionally, there's the issue of my tables in that loop being added in the backwards order, so I had to reverse the direction of the loop iterations to get the tables to appear in the correct order. I've tried using stuff like setLocation and setBounds to get my components more where I want them, but nothing happened. Also, everything just appears in a row below the tables (and I know that's what FlowLayout does), but how would I go about customizing exactly where things appear?
Wrap a panel with BorderLayout around ones with FlowLayout. Put all the content that should be above the tables in a panel and add it with BorderLayout.NORTH. Put all the content that should be below the tables in another panel and add it with BorderLayout.SOUTH. Then put the tables in their own panel just as your are now, and add it with BorderLayout.CENTER.
Either use a LayoutManager or setLayout(null). In the latter case, you can move your components around by calling setBounds on them. I've been doing that lately too (not using a LayoutManager), it's quite liberating.

making two tables in the same jScrollPane

I want to make many tables in the same pane and show the one I want by clicking on a button by hidding a table and showing the other , the problem is i can't put a table on top of the other in NetBeans. How can i put all tables one above the other?
and show the one I want by clicking on a button by hidding a table and showing the other
Don't attempt to have two tables in the same scrollpane.
If you want to change the component in the scrollpane you would just use:
scrollPane.setViewportView( theOtherTable );
Or you can just change the model of the table:
table.setModel( ... );

How to delete table row using jButton from another jFrame?

I am currently using FileWriter to write data into .txt tile.
Once I double click the row in the table, it links to another jFrame.
I wanted to set a button at the particular jFrame so that it is able to delete the row in the table. How can I perform the action?
Use the MVC (Model-View-Controller) paradigm.
Have a controller class, maybe containing the main method.
That holds the views (JFrame) and the data models (i.e. a DefaultTableModel).
That table model is passed to the table, which actually is also a listener to changes of the table model.
On button press let the button tell the controller that a row should be deleted.
This is done on the table model, and change events are fired. Automatically when using a DefaultTableModel. Or do it manually when using an AbstractTableModel.
MVC is not necessarily more direct, but there are not calls from one component to another components littered through the sources. It decouples things.
This can be done by adding a New button. Go into the codding of the button and add a simple sql statement saying to drop a row from the table And the another statement to show the table again by Select * from tablename;

Separating multiple JTables

I'm writing a GUI. In that GUI I have a dropdown box in which I can select different persons. Each person has a CardLayout Pane and in that Pane a table with information about themselves and things they own.
I have written a class called PanelTableItems(Person person).
On program start I use this class to create mutliple of these tables (one for each person in my program). And each CardLayout Pane has one of these tables. I have just implemented a ListSelectionListener to store the last selected row which I am using a private function to get the selected item from the table. However as I implemented this selection listener I tried with a print and for some reason it seems that if I have two persons in my program the selection listener made two prints even though the "second" table was not in view and therefore not selected.
Here is my question:
How do I make sure to only operate the table that is currently in "view" using the CardLayout? The second pane is hidden but it seems that all function calls to the first pane also manages to run on the second one as they are of the same type.
I could post a MCVE, but this is more of a theoretical/solution question than an actual coding question.
Thanks in advance.
On person selection, you could just switch visible panel with:
CardLayout cl = (CardLayout) cards.getLayout();
cl.show(cards, "idOfTheSelectedPersonPanel");
So, you should have registered those panels already in the layout each one with different id in regard to the person it represents.
Hidden (not visible) panels do not get any user input - if you see such behaving then your code does something wrong.
In ListSelectionListener you need to filter to process only events that something new is selected.
I suggest you to recheck the way you are adding the components to your cards you are maybe adding all tables to the same container witch makes only the last one visible, try adding each table to a JPanel or a JScrollPane.

Use TAB to change selection and focus in a JSplitPane (from one JTable to another)

I have a very simple GUI using 2 JTables in a SplitPane and I want to use TAB to remove selection in one JTable and move focus and select the first row of the other JTable. The gui is just each JTable in its own ScrollPane. Both ScrollPanes in the SplitPane and the SplitPane is in my class which extends JFrame.
The way I went about it was:
table_LEFT.getInputMap().put(KeyStroke.getKeyStroke("TAB"),
"doSomething");
table_LEFT.getActionMap().put("doSomething",
new myAction(table_RIGHT));
This will register the key TAB to myAction which extends AbstractAction. In AbstractAction I have to override the 'actionPerformed(ActionEven ae)' method. The problem is I cannot access the other table (in this case the 'table_RIGHT' from the ae event. I have to go through the containers to the top where the property of myClass is the other table. This seems very cumbersome and wrong + plus I managed to select a row in table_RIGHT, but don't give it focus ... . Am I going about this right?
I have to override the 'actionPerformed(ActionEven ae)' method.
Correct.
The problem is I cannot access the other table (in this case the 'table_RIGHT' from the ae event.
When you create your Action, you can always pass in the reference to the right table when you create the Action. Then you store the reference so you can access it in the actionPerformed(...) method.
plus I managed to select a row in table_RIGHT, but don't give it focus ... .
If you have a reference there is no reason you can't give the focus to the table. The basic code would be:
rightTable.requestFocustInWindow();
rightTable.changeSelection(...)
If you need more help then post your SSCCE that demonstrates the problem.

Categories

Resources