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.
Related
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.
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.
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
Lets say I have 2 JPanels.
I create both of them while program is loading and set 2nd to visible(false). 2nd JPanel uses information entered in 1st JPanel. I want to use that information.
However since both panels are created at the same time the value from 1st Panel will be null.
I need something like repaint(), but it should repaint all JTextFields with new variable values. I can't find anything useful in java api.
Is there something like this in java?
If not, what are my options?
Create a method in the second JPanel that sets all the values up.
Pass a reference to the second JPanel into the first.
When the first is ready it can call the method in the second to set all the values.
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