I'm trying to add a table to my program that has the column names at the top and a scroll bar down the side. For some odd reason everything works, but the column names don't show nor does the scroll bar.
Here's my code, if you need a running program let me know, but you should be able to just add this to an empty JFrame:
String[] columns = {"Sport", "Location", "Date", "Result"};
String[][] data = {{"Football", "AQA Highschool", "12.11.13", "5 - 0"},
{"Tennis", "Wembley", "26.11.14.", "TBC"}};
listTable = new JTable(data, columns);
listTable.setPreferredScrollableViewportSize(new Dimension(450, 750));
listTable.setFillsViewportHeight(false);
listTable.setBounds(25, 100, 450, 640);
JScrollPane scroll = new JScrollPane(listTable);
guestFixturesPanel.add(listTable);
guestFixturesPanel.add(listTable);
is wrong ! you must add the scroll like this:
guestFixturesPanel.add(scroll);
For some odd reason everything works, but the column names don't show
nor does the scroll bar.
First of all you're not adding the scroll pane but the table directly to guestFixturesPanel:
guestFixturesPanel.add(listTable);
Second, you must avoid the use of setBounds() method and use a suitable LayoutManager instead. Take a look to A visual guide to Layout Managers. You might also consider third party options suggested here. As #AndrewThompson always says:
Java GUIs might have to work on a number of platforms, on different
screen resolutions & using different PLAFs. As such they are not
conducive to exact placement of components. To organize the components
for a robust GUI, instead use layout managers, or combinations of
them, along with layout padding & borders for white space.
Related
I am making a simple GUI and I want to put them in a GridLayout like this
areaLabel, sAreaLabel, volumeLabel
areaField, sAreaField, volumeField
blank, radiusLabel, blank
blank, radiusField, blank
so the layout would be made like this
'''
new GridLayout(4, 3);
'''
I know how to use the The problem is that I don't know how to put components into specific places. I looked through the Java API and just online but I couldn't find anything. Do I just need to add blank components in the places I've indicated or is there a more elegant solution?
The order of the components is defined by the rows, columns, grid orientation and the order you add the components. See https://docs.oracle.com/javase/8/docs/api/java/awt/GridLayout.html
You could add blank JLabels as placeholders for the blank cells. Something like
JLabel blank = new JLabel("");
myGrid.add(blank);
I have a String[] with 115 names. Now of course, i cannot fit all these names at once on a screen that i've set to be 1280 x 720.
I would like to display let's say 15-20 of these as buttons from the top of the screen until they hit the bottom, and the right side of the screen is an image, so we're lookin for a GridLayout(115, 1);
How would i go about to create this so that i can see a few names, and the rest of the are further down in the list and i access them with a scrollbar? My program extends JFrame and the item to the right is a 720 x 720 JLabel that is one big picture.
Did not add my code since there is no specific questions asked here, merely what classes i'd be using and how i would go about to proceed and use them. I've done some research on JScrollPane , JList and such.
Start by taking a look at
How to use scroll panes
How to use lists
Basically, a scroll pane controls a single component which acts as the view. The scroll pane acts like a window, allowing you to see a position of the view if it is larger then the scroll panes viewable size...
JList iist = new JList();
List.setModel(...);
JScrollPane sp = new JScrollPane(list);
// add sp to something ...
Using a JScrollBar, you can add a class with myScrollBar.addAdjustmentListener(this) so it will call your AdjustmentListener's adjustmentValueChanged(AdjustmentEvent e) method. In this method, use e.getValue() to get the value of the scroll bar. Interpret this value (with some good 'ol mathematics) to update the visibility/invisibility and x/y positions of your buttons!
For more, read up a tutorial on JScrollBar's.
Don't forget to set the orientation, min and max values, etc.
I am attempting to put a JGoodies panel into a JScrollPane with only a vertical scroll bar; any elements larger than the current JScrollPane width should be truncated. However I can't figure out a way to make this work
Example of the effect I'm going for
What I don't want to happen
My current code is essentially:
FormLayout locationsLayout = new FormLayout("15dlu, pref, 5dlu, pref, 5dlu, pref:grow", "");
locationsBuilder = new DefaultFormBuilder(locationsLayout)
.background(Color.WHITE)
.lineGapSize(Sizes.ZERO);
locationsPane = new JScrollPane(locationsBuilder.getPanel());
locationsPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
locationsPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//...Sometime later, the user adds a folder...
FormLayout headerLayout = new FormLayout("pref, pref", "pref");
DefaultFormBuilder headerBuilder = new DefaultFormBuilder(headerLayout)
.background(Color.WHITE)
.lineGapSize(Sizes.ZERO);
headerBuilder.add(curContainer.getGuiHeader(), CC.xy(1, 1));
headerBuilder.add(curContainer.getGuiTablePrefix(), CC.xy(2, 1));
locationsBuilder.leadingColumnOffset(0);
locationsBuilder.append(headerBuilder.getPanel(), 6);
Things I've tried
Various permutations of min, pref, grow, fill, etc. Nothing changed this behavior
Passing a custom JPanel that implements Scrollable to the locationsBuilder DefaultFormBuilder constructor as documented here, here, or here
Trying the other vertical scroll bar options on JScrollPane
I don't know what else I can try. Does anybody have any suggestions?
I never could find an exact answer to this specific setup. My guess is that JGoodies dies bit handle nested layouts very well.
I ended up "fixing" this by using only one single panel for the entire locations scroll pane. This made the layout a bit complicated: Multiple cells now had to span columns and I had to manually adjust the column offset. But in the end it works
I have a GridLayout 2 rows by 5 columns, and I want to make the height of the first row to something like 50, and the second row to 200. I know GridLayout creates equally-sized cells, so this didn't work out. I also tried splitting the two rows into two GridLayouts setting their desired heights and adding them to a FlowLayout, but the columns didn't align the way I wanted it to. My code went something like this:
row1.setSize(WIDTH, 50); //GridLayout
row2.setSize(WIDTH, 200); //GridLayout
panel.add(row1);
panel.add(row2); //panel is a FlowLayout
The columns aligning are very important and I can't seem to get this right.
I'd look into GridBagLayout. Although, almost everyone I've talked seems to dislike it. I like it, though.
You can't do this with a GridLayout.
You should be able to use either a GridBagLayout or a SpringLayout. Check out the Swing tutorial on Layout Managers for some examples to get you started.
Also you don't add individual rows to the layout. You need to add all 10 components individually to the same panel using whatever layout manager you choose.
Each cell in a GridLayout is exactly the same size, so you'll have to use a different layout manager, e.g. GridBagLayout.
I have Java application which adds JTextFields # runtime to JPanel. Basically user clicks a button and new JTextField is added, clicks again added again...
Each new JTextField is directly below the previous one. Obviously I run out of space pretty soon so I'm trying to use JScrollPane and thats where the hell begins, because it just doesnt work no matter what I try.
Right click on JPanel and Enclose in Scroll Pane. Didnt work.
After reading some examples I realized I must have JPanel as an argument for JScrollPane constructor. Which I did via right clicking on ScrollPane and CustomizeCode. Because apparently auto-generated code is protected in NetBeans and I cannot just change all those declarations, etc. manually. Still doesnt work.
I did try to set PreferedSize to null for JPanel and/or JScrollPane, didnt help.
JScrollPane is a child of lets call it TabJPanel (which in turn is a tab of TabbedPane). I tried to mess with their relationships, basically trying every possible way of parentship between JFrame, JPanel(holding textfields), TabJPanel and JScrollPane, but nothing worked.
I also made VerticalScrollBar "always visible" just in a case. So I see the scrollbar, it's just that populating that JPanel with JTextFields does not affect it.
When there are too many JTextFields I they go "below" the bottom border of JPanel and I cannot see them anymore.
Code for adding new JTextFields is like this, in a case it's relevant.
JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);
JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();
Sorry for a long post I'm just trying to provide as much info as possible, because being newbie I dont know whats exactly relevant and whats not. Thanks in advance :)
As there is another answer now, I'm adding my suggestion too.
This sounds exactly like a problem to use a JTable with a single column. JList is not yet editable (and might never be).
JTable would handle the layout problems for you, and you can easily access the values via the table.
Use your own TableModel (a simple Vector should be sufficient in your case), and add values to it.
An option you have is to utilize a LayoutManager, instead of setting the bounds directly on the components. To test this, a simple single column GridLayout with the alignment set to vertical should prove the concept.
panel.setLayout(new GridLayout(0,1));
zero in the rows param allows for rows to be added to the layout as needed.
I do this way to add a scrollpane, create a panel and fill it with few components, then create a scrollpane in the component you want to add it, cut and paste the panel in which all your details will fall in and resize the scrollpane.Because the components take a larger space than the one visible right click on the scrollpane and select design this container, there you can increase the size of the scrollpane and add as many components as you have.