Separating multiple JTables - java

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.

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.

Java Swing CardLayout: get componente associated to a String

is there a command that returns the componente associated to a speciefied String name in Java Swing CardLayout? I have many panels in my CardLayuot which are at first empty, at runtime i have to "update" those panels to add buttons in it (the panels already have GridBagLayout), i would prefer NOT to make panels all over again and replace the old ones, as it would be slower to charge, and instead get those panels and add something. Thanks guys! (i checked this but there's nothing like what i need https://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html)
Every Component, such as JPanel, has a name field, and you can set it with setName(). If your program already has a List<JPanel>, you can search the list and check getName(). If not, you can add your panels to a Map<String, JPanel> and get() the one you want by name.

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.

How to select an Item from a JList among other JLists in the same JPanel

I'm working on a small File Management program in Eclipse(Version:Kepler). Also am new to Java Programming.
I designed a JPanel in a JFrame, where there are two JScrollPanes each containing a JList. The two JLists are programmed to show a list of files in two separate folders(i.e. JList1 shows list of files in Folder A and JList2 shows list of files in Folder B). Also there is a button for selection.
I need to program the button in such a way that only one Item is selected i.e. if I select an Item in JList1 then only that Item gets selected, not any Item in JList2 and vice-versa.
Its something to do with focus methods but I don't know how. I've searched the web, but search-results show item selection in one JList (not two JLists in the same panel).
Sample Codes would be appreciated.
PanelBrowser is an example that uses ListSelectionModel.SINGLE_SELECTION to preclude multiple selection.
Addendum: How do I determine that the currently selected Item is of JList1?
You can use a ListSelectionListener, as shown in the example cited above and How to Write a List Selection Listener. Also consider Action, shown here.

Active Elements in swing JTree, JTable or JList

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

Categories

Resources