Java Swing: components not added to JPanel with JScrollPane - java

This code is just a silly example which I need to understand to solve a much bigger problem about another project I am working on: as you can see, in the for cycle, 23 buttons are supposed to be added to JPanel. As they cannot be shown all together I decided to add a JScrollPane but a strange thing happens: only 19 are shown and I do not understand why. The size matrix has 1 column for 23 rows so it is correct. Do you know why this happens? Thanks
package proveGUI;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import layout.TableLayout;
public class Main {
public static void main(String argv[]) {
JFrame jframe = new JFrame("Protocollo UTL");
//jframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jframe.setSize(1200, 450);
JPanel body = new JPanel();
double[][] size = {
{0.05},
{0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05,0.05}
};
body.setLayout(new TableLayout(size));
for(int i=0; i<22; i++) {
body.add(new JButton(String.valueOf(i)), "0,"+String.valueOf(i));
}
JScrollPane scrollPane = new JScrollPane(body,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jframe.add(scrollPane);
jframe.setVisible(true);
}
}

The problem stems from you using size values between 0.00 and 1.00. These are interpreted as relative sizes/percentages, in this case 5%. That's why it's showing 20 rows only (0 to 19 inclusive). Try using "50.0" instead and the problem goes away. Or use TableLayout.PREFERRED.

I don't know much about TableLayout because I've didn't ever use it. If it is your first experience with Swing, try the common Swing Layouts.
For your case, you can simple try
body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS)); // instead of TableLayout
When U'll understand how layouts work in Swing, use GridBagLayout, because it is the most adjustable layout.

Related

Why don't size and preferredSize make this label bigger?

I'm building up a panel that will go in a larger program; the following program still illustrates my question, but it looks a bit more complicated than it absolutely has to because there are places I will add things later.
package sandbox;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SpacingPlay extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
SpacingPlay sp = new SpacingPlay();
sp.setVisible(true);
}
public SpacingPlay()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new DragNDropPanel();
add(panel);
pack();
}
class DragNDropPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public DragNDropPanel()
{
JPanel currentImagePanel = getCurrentImagePanel();
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.add(currentImagePanel);
// other things will go here; I cut them out to make this simpler.
setLayout(new BorderLayout());
// put things in a containing panel so that they aren't stretched being in the WEST part of a borderlayout.
JPanel leftContainingPanel = new JPanel();
leftContainingPanel.add(leftPanel);
add(leftContainingPanel, BorderLayout.WEST);
}
private Component createStandardSpace()
{
return Box.createRigidArea(new Dimension(0, 15));
}
private JPanel getCurrentImagePanel()
{
JPanel currentImagePanel = new JPanel();
currentImagePanel.setLayout(new BoxLayout(currentImagePanel, BoxLayout.PAGE_AXIS));
JLabel currentImageLabel = new JLabel("none");
currentImageLabel.setBorder(BorderFactory.createDashedBorder(Color.BLUE));
currentImageLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
Dimension defaultLabelSize = new Dimension(150,150); // was expecting this to enlarge the label.
currentImageLabel.setPreferredSize(defaultLabelSize);
currentImageLabel.setSize(defaultLabelSize);
JButton clearButton = new JButton("Clear");
clearButton.setAlignmentX(Component.CENTER_ALIGNMENT);
clearButton.setBorder(BorderFactory.createLineBorder(Color.GREEN));
currentImagePanel.add(currentImageLabel);
currentImagePanel.add(createStandardSpace());
currentImagePanel.add(clearButton);
currentImagePanel.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
return currentImagePanel;
}
}
}
I would like the currentImageLabel to be a standard size; I intend for it to get different images put into it during the program, and want it to get these without changing size. My idea was to set a size and preferred size and then scale the images I put there to that size.
However, the defaultLabelSize doesn't have the effect I thought it would. The label goes into a boxLayout panel; it is added, then a rigid space, then a button. I expected the label to be the default size, not shrunk to min allowed. I've put in colored borders to try to understand better what's happening; it appears that the preferred size is honored for the overall boxLayout panel, but not for the placement of the button below the label. EDIT: In other words, I want the button below the label to be placed below the label when the label is forced to be bigger. But the size I put on the label doesn't seem to work.
What do I need to do to fix the size of currentImageLabel?
Not 100% why the label is only sized to the text and not the (hard coded) preferred size. I have not been able to duplicate this behaviour using other combinations of panels and layout managers.
You are using pack so all components should be sized to their preferred sizes.
Dimension defaultLabelSize = new Dimension(150,150); // was expecting this to enlarge the label.
currentImageLabel.setPreferredSize(defaultLabelSize);
currentImageLabel.setSize(defaultLabelSize);
A few comments:
Setting the size will never work. The layout manager will always override the size/location based on the rules of the layout manager.
The layout manager can use (or ignore) the preferred, minimum and maximum sizes of a component. In the case of the BoxLayout is does attempt to use the preferred size but will respect the minimum and maximum sizes (depending on the available space in the parent panel).
What do I need to do to fix the size of currentImageLabel?
So, to achieve your desired goal of a fixed preferred size for the JLabel you can use:
Dimension defaultLabelSize = new Dimension(150,150); // was expecting this to enlarge the label.
currentImageLabel.setPreferredSize(defaultLabelSize);
currentImageLabel.setMinimumSize(defaultLabelSize);
currentImageLabel.setMaximumSize(defaultLabelSize);
//currentImageLabel.setSize(defaultLabelSize);
Edit:
was looking for why this doesn't seem to work
For further clarification, change your original code to:
currentImageLabel.setPreferredSize(defaultLabelSize);
System.out.println(currentImageLabel.getPreferredSize());
System.out.println(currentImageLabel.getMinimumSize());
System.out.println(currentImageLabel.getMaximumSize());
You will see the min/max sizes of the label are not affected.
From point 2 above you will see that the BoxLayout is respecting the maximum size.
Therefore, by also overriding the maximum size, you allow the label to be displayed at is preferred size.
However, when you calculate the preferred size of the "currentImagePanel" the ( hardcoded) preferred size of the label is used in the preferred size calculation of the panel, so that panel is displayed at the preferred size.
Another note. The "leftContainingPanel" is not needed. You can just add the "leftPanel" to the BorderLayout.WEST, since the BorderLayout will respect the width of the component you add.

How to create a fluid 2-column GridLayout within a ScrollPane?

I have done tons of searching over the past two hours, and I've given up. The image below shows what I am trying to achieve (don't judge my drawing it's late and I made it in paint quickly):
Basically, I want a JScrollPane to have a JPanel with a 2-column GridLayout, and as I add elements I want the GridLayout to expand downward. I want the elements to use their Preferred Size and to NOT expand within the GridLayout.
Currently I have a JScrollPane and a JPanel with a GridLayout, and a JPanel containing the grid with a FlowLayout. As a test, I add 10 buttons to the grid. Here's my current code:
// Setup main panel
JPanel pnlUsers = new JPanel(new GridLayout(0, 2));
pnlUsers.setOpaque(true);
pnlUsers.setBackground(Color.GREEN);
// Setup GridLayout Container
JPanel pnl2 = new JPanel();
pnl2.setOpaque(false);
pnl2.add(pnlusers);
// Setup scrollpane
JScrollPane scrUsers = new JScrollPane(pnl2);
scrUsers.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrUsers.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrUsers.setOpaque(false);
scrUsers.getViewport().setOpaque(false);
scrUsers.setBorder(null);
// Add users
for (int i = 0; i < 10; i++) {
pnlUsers.add(new JButton("Button " + (i + 1));
}
This gives a very good result, with the buttons using their preferred size as seen in the image below:
Unfortunately, the buttons are still not filling the horizontal space. So, I attempted to make pnl2 a BoxLayout instead and add some vertical glue...
// Setup GridLayout Container
JPanel pnl2 = new JPanel();
pnl2.setLayout(new BoxLayout(pnl2, BoxLayout.Y_AXIS));
pnl2.setOpaque(false);
pnl2.add(pnlusers);
pnl2.add(Box.createVerticalGlue());
I also created my own temporary button class that sets the preferred size to use the minimum size:
public class TempButton extends JButton {
public PLTempButton(String msg) {
super(msg);
this.setPreferredSize(this.getMinimumSize());
}
}
Which resulted in the following:
This is much better, but there's still a problem. If there are not enough buttons to cause the JScrollPane to scroll, the buttons height are not consistent and will resize as you resize the window vertically. Why?
Obviously when I add 100 buttons, they use their preferred size:
Maybe I'm just not understanding the differences between minimum, preferred and maximum size? But I want the buttons to use the height they're set even if there aren't enough to cause the scroll-pane to, well, scroll. What can I do to fix this?
1) How do I even get a GridLayout within a ScrollPane?
Create a JPanel
Apply a GridLayout to the JPanel
Wrap the JPanel in a JScrollPane
For example
JPanel panel = new JPanel(new GridLayout(0, 2));
JScrollPane scrollPane = new JScrollPane(panel);
// Add the scroll pane to what ever parent container you're using
2) How do I get said GridLayout to expand horizontally, including the added components?
That doesn't make sense with regards to all previous part of the question, you said "and have it slowly expand downward the more things I add"
Having said that, the "basic" answer is, you configure the GridLayout and let it do it's job. The above example is configured for 2 columns and n number of roes
3) How would I add a "margin" to the components?
That's a broad answer, you could:
Make use of the horizontal and vertical gap properties of the GridLayout
Use a compound layout and adjust the insets of the an appropriate layout manager (like GridBagLayout)
Recommendations
I would recommend reading through Laying Out Components Within a Container to get a better understanding of the layout managers.
Remember, you not stuck to using one.
I would also recommend reading through How to Use Scroll Panes as you're asking basic questions about the API which are better covered through the tutorials
The following mre demonstrates creating a JPanel with GridLayout warped by JScrollPane and setting its horizontal and vertival gaps.
The Jpanel is added using a BorderLayout (the default layout manager of JFrame content pane) which allows it to expand:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class SwingMain {
private String text ="Growing ";
private JPanel grid;
private JFrame f;
SwingMain() {
creategui();
}
void creategui(){
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
JButton addComponentBtn = new JButton("Add Component");
addComponentBtn.addActionListener(e-> addComponent());
f.add(addComponentBtn, BorderLayout.PAGE_START);
grid = new JPanel(new GridLayout(0, 2, 10, 10)); //any number of rows, 2 columns, H and V gap
f.add(new JScrollPane(grid), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
void addComponent() {
grid.add(new JLabel(text) );
text +="."; //make text longer so JLable grows
f.pack();
}
public static void main(String[] args) {
new SwingMain();
}
}

Why does the JTable not show the Jtable heading even when added to JScrollPane?

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.awt.Checkbox;
import java.awt.Paint;
import java.awt.Toolkit;
import java.awt.event.*;
public class Main extends JPanel {
static Object [][] Services = {{"Google.exe","Chickeaen.exe","Crp.exe"}};
static String [] ColNames = {"Processes:","Crolly:","Haler:"};
static JFrame Fram = new JFrame();
static JTextField CBox = new JTextField();
static JTable Tabs = new JTable(Services,ColNames);
JScrollPane ScrollArea = new JScrollPane();
static JButton ExitB = new JButton();
Dimension ScreenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
Border BlackLineB = BorderFactory.createLineBorder(new Color(50,50,50));
public Main() {
Fram.setTitle("Jared Console");
Fram.setUndecorated(true);
Fram.setVisible(true);
Fram.setDefaultCloseOperation(Fram.EXIT_ON_CLOSE);
Fram.setResizable(false);
Fram.setSize((int)Math.round(ScreenSize.getWidth()*0.45),(int)Math.round(ScreenSize.getHeight()*0.33));
Fram.setBackground(new Color(0,0,0,150));
Fram.add(this);
CBox.setSize((int)Math.round(Fram.getWidth()*0.80),(int)Math.round(Fram.getHeight()*0.25));
CBox.setBackground(new Color(255,255,255));
CBox.setBorder(BorderFactory.createCompoundBorder(BlackLineB,BorderFactory.createEmptyBorder(0,20,0,0)));
CBox.setLocation((int)Math.round(Fram.getWidth()*0.1),(int)Math.round(Fram.getHeight()*0.70));
CBox.setFont(new Font("Arial",Font.BOLD,20));
CBox.setVisible(true);
ScrollArea.setSize((int)Math.round(Fram.getWidth()*0.80),(int)Math.round(Fram.getHeight()*0.50));
ScrollArea.setLocation((int)Math.round(Fram.getWidth()*0.10),(int)Math.round(Fram.getHeight()*0.10));
ScrollArea.setBorder(BlackLineB);
ScrollArea.setLayout(null);
ScrollArea.setVisible(true);
Tabs.setSize((int)Math.round(Fram.getWidth()*0.995),(int)Math.round(Fram.getHeight()*0.995));
Tabs.setLocation((int)Math.round(Fram.getWidth()*0.003),(int)Math.round(Fram.getHeight()*0.005));
Tabs.setFillsViewportHeight(true);
Tabs.setBackground(new Color(255,255,255));
this.add(CBox);
this.add(Tabs);
this.add(ExitB);
ScrollArea.add(Tabs);
this.add(ScrollArea);
this.setBorder(BorderFactory.createLineBorder(new Color(50,50,50),5));
this.setLayout(null);
this.setBackground(new Color(0,0,0));
this.setVisible(true);
}
public void paintComponent(Graphics Gla) {
Paint Plat = new GradientPaint(0f, 0f, new Color(0, 40, 0, 0), 0.0f, Fram.getHeight(), new Color(0, 0, 0, 150), true); //Made 200 equal to Fram Background Alpha.
Graphics2D Quo = (Graphics2D)Gla;
Quo.setPaint(Plat);
Quo.fillRect(0, 0, Fram.getWidth(), Fram.getHeight());
}
public static void main(String[] args) {
Main CScreen = new Main();
GraphicsEnvironment GE = GraphicsEnvironment.getLocalGraphicsEnvironment(); // Have to study lines 57,58 and 59
GraphicsDevice GD = GE.getDefaultScreenDevice();
boolean CheckTransL = GD.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
if (!CheckTransL) {
System.out.println("PERPIXEL TRANSLUCENT NOT SUPPORTED - LOL UPDATESCRUB!");
System.exit(0);
};
}
}
Why does the JTable not show the Jtable heading even when added to JScrollPane?
Also the Console shows a error message at first then quickly goes away and launches the program? So yea I'd like to know what's wrong with this and also can you can note me of some bad habits in this program such as the way it's being typed.
Problems
null layout. Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See Laying Out Components Within a Container for more details
Over use of static. static is not your friend and you should avoid using it. It is not a cross object communication mechanism and over use like this will burn you
You don't actually wrap the JTable in JScrollPane
Breaking the paint chain by not calling super.paintComponent, this is going to produce a series of wonderful paint artifacts. See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing
You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
All interactions, creations and modifications to the UI should be done from within the context of the Event Dispatching Event, to reduce the risk of potential race conditions, dead locks and rendering artifacts. See Initial Threads for more details
Toolkit.getDefaultToolkit().getScreenSize() is a bad indicator for the viewable space of a screen, it does not take into account OS specific elements, like the dock or task bar, which could have your application appearing under them (and I really, really, REALLY hate it when that happens). Use appropriate layout managers and pack to pack the window around the content. You can then use JFrame#setLocationRelativeTo and pass it null and it will center the frame in the screen
JScrollPane issue...
The simply solution would be to use JScrollPane's constructor to pass it the reference of the JTable...
static JTable Tabs = new JTable(Services,ColNames);
JScrollPane ScrollArea = new JScrollPane(Tabs);
But, then you do this later in your code...
this.add(Tabs);
This will remove the table from the scroll pane to add it to you panel, as a component can only have a single parent.
Another option would be to specifiy the scroll pane's viewport's view component...
this.add(CBox);
//this.add(Tabs);
this.add(ExitB);
//ScrollArea.add(Tabs);
ScrollArea.setViewportView(Tabs);
this.add(ScrollArea);
You should never add components directly to a scroll pane (or it's underlying viewport), they have their own internal layout management functionality going on. Instead, you need to supply the component as the "view" to the JViewport
Take a look at How to Use Scroll Panes for more details.
First of all you should add a JTable to the ViewPort of the a JScrollPane in order to JTableHeader could be visible.
After that you should not add your JTable to both the JScrollPane and also the underlying container. You should:
add the JTable to the ViewPort of the JScrollPane.
add the JScrollPane to the underlying container.
and remove the line that add the JTable to the container explicitly.
Good Luck.

Java. Prevent enlarging of Button

How to prevent Button from UpSize? I use a vertical Box with 2 lines, in first line → horizontal Box with many controls, in second line → Button, but button very enlarged, how to prevent this irritating behavior?
I was tried to set maximum size of button, it is works, but How to calculate this size correctly?
Sorry, I bad speak English.
Example:
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JProgressBar;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
public class TEST{
public static void main(String[] args){
Frame mainWindow = new Frame("hello");
Box vertBox = new Box(BoxLayout.Y_AXIS);
mainWindow.add(vertBox);
Box firstLine = new Box(BoxLayout.X_AXIS);
vertBox.add(firstLine);
SpinnerModel sm = new SpinnerNumberModel(100, 0, 200, 1);
for(int i = 0; i < 10; i++){
firstLine.add(new JSpinner(sm));
firstLine.add(new Label("Hello"));
}
Box secondLine = new Box(BoxLayout.X_AXIS);
vertBox.add(secondLine);
secondLine.add(new JProgressBar());
secondLine.add(new Button("RUN-THIS"));
mainWindow.pack();
mainWindow.setVisible(true);
}
}
Button, but button very enlarged, how to prevent this irritating behavior?
Don't use AWT components in a Swing application!
Swing components start with "J". You should be using JFrame, JLabel, JButton. This will fix your problem. A JButton will automatically calculate the maximum size equal to the preferred size and BoxLayout will respect this.
Also, class name should only start with a single upper case character. "TEST" should be "Test".

Visualizing arraylist

I have a lage JPanel in my JFrame which contains arraylist of "Pages" - data type which extends JPanel, it defaultly contains JPanel "area", height equaling the cumulative one of it's children (which it has none by default), and BOX.filler (preffered height is full height of the "Page"). So, by default, the whole "Page" is filled with filler, which decreases as I add elements to "area".
Also, I have an array list which contain JPanels. They are elements which go onto "area". As I add them, when height of filler becomes zero, I create a new "Page" and move last element (which went "off bounds") onto it. Basically, it's supposed to work similar to MS Word.
Now the catch. User can add and remove those elements freely, from whichever part of the document. The question is, how can I add an element to a random spot on "area"? Addition will probably be made by invoking a keylistener attached to another element, so I will know between which two elements is the random one being put.
Is there a layout manager that can be used for this or should it be done another way?
Which way?
If I understand you problem correctly, you are trying to insert a component at a certain position instead of appending it?
If that's your problem, you might want to use a BoxLayout and the add(component, index) method of java's swing containers. Of course you might again need to move some childrens to the following page, and recursively childrens of the following page to the one after that.
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AddAt {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel one = new JLabel("one");
JLabel two = new JLabel("two");
JLabel between = new JLabel("between");
frame.add(panel);
panel.add(one);
panel.add(two);
panel.add(between, 1);
frame.pack();
frame.setVisible(true);
}
}
results in:

Categories

Resources