java JList adding elements to model is changing visibleRowCount - java

I am using the default JList model for adding elements to a JList. Occasionally when I would add an element to the model it would change the JLists visibleRowCount from 2 to 1. It does not seem to matter weather I add one or 10 elements, it keeps changing it to only 1 visible row even if I try to force it back after adding elements. I am using a panel with a flowlayout to hold the Jlist and its scroll with that panel in a gridBagLayout

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.

accessing Arraylist containing JPanels holding JFormattedTextFields (setText & getText)?

So I have around 6 or so JFormattedTextfields inside a JPanel, and a couple dozen of these JPanels. I can put each JPanel in it's own component array using:
Component[] layer1 = LayerPanel1.getComponents();
etc..
which gives me an array of the JFormattedTextfields in that panel. I can also put the component arrays in an ArrayList using:
layerSet_components.add(layer1);
etc..
What I need to know is how to setText the text of a particular JFormattedTextfield inside the ArrayList and to also getText of a particular JFormattedTextfield.

How do I avoid the array bound error with JTables?

Well my question is fairly simple, I have put a JTable in a JFrame, but I want that the table to automatically resizes according the size of an ArrayList or to have a side scroll bar
For example if I do something like this:
for (int i=0;i<list.size();i++){
jTable1.getModel().setValueAt(list.getVar(i),i,0);
}
If my ArrayList has 10 elements and my JTable was set up to 4 elements I got the arraybound error. Is there a way to put a scroll bar or something in the JTable so it can shows all the rows that I want (depending on the size of the ArrayList of course)?
Set the number of rows in the table before you run your code:
((DefaultTableModel)(jTable1.getModel())).setRowCount(list.size());
DefaultTableModel documentation
If you want to put the JTable in a scroll box, when you add it to the container you have to say
container.add(new JScrollPane(jTable1));
instead of
container.add(jTable1);

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

How do I programmatically control the position of JScrollPane?

I have a JScrollPane containing a JPanel and, in turn, a JList. I want for the viewport to always show the end of the list (from which items are being removed or added). I can accomplish this manually by dragging the ScrollBar to the bottom, where it stays, just the way I want it to. What should I do to accomplish this automatically?
It should be simple to do. Just use JList's ensureIndexIsVisible method and pass it the last index in the list (size - 1).
You can use "scrollto" call while after creating JScrollPane.
here is the link for documentation.
http://jscrollpane.kelvinluck.com/scroll_to.html

Categories

Resources