Reduce gap between JPanels - java

I have a simple Class StatPanel that extends JPanel and has a few JTextFields, a JCheckBox, and a couple JLabels. The panel is going to be used as a series of panels used to display information and allow the user to edit the data in some of the JTextFields and I will probably have about 40 of them all stacked together on the side of the program in a JScrollPane JPanel. The layout I am using is a GridLayout(0,1).
I have them working but when the are in the scroll panel they are spaced too far apart vertically. I have tried changing the margins, changing the spacing in the GridLayout and changing the margins of the various items. Nothing I do seems to make them closer.
This is the constructor for the StatPanel class:
public StatPanel(String statName, String statAbility){
super();
this.skillName = statName;
this.setAlignmentY(CENTER_ALIGNMENT);
isClassSkill = new JCheckBox();
statLabel = new JLabel(skillName);
statTotalField = new JTextField(maxLength + 1);
statTotalField.setHorizontalAlignment(JTextField.CENTER);
statAbilityLabel = new JLabel("= " + statAbility + " ");
statAbilityModField = new JTextField(maxLength);
statAbilityModField.setHorizontalAlignment(JTextField.CENTER);
statSeperator1 = new JLabel(" + ");
statRanksField = new JTextField(maxLength);
statRanksField.setHorizontalAlignment(JTextField.CENTER);
statSeperator2 = new JLabel(" + ");
statMiscModField = new JTextField(maxLength);
statMiscModField.setHorizontalAlignment(JTextField.CENTER);
this.add(isClassSkill);
this.add(statLabel);
this.add(statTotalField);
this.add(statAbilityLabel);
this.add(statAbilityModField);
this.add(statSeperator1);
this.add(statRanksField);
this.add(statSeperator2);
this.add(statMiscModField);
}
}
When I use it in the program it looks like this:
As I am going to stack so many of them I want them pretty much one on top of the other but I can seem to remove the gap between them.
How is this done?
Thank you for your help.

Check out the FlowLayout API. By default a JPanel uses a FlowLayout with default horizontal/vertical gap of 5 pixels. You will want to change the vertical gap used by the layout manager. So at the top of your method you will want to add:
setLayout( new FlowLayout(...) );
Edit:
Once you change the gap you will also lose the gap at the top/bottom of the main panel so you might want to add an EmptyBorder to the panel.

Related

Resizing GridLayout area

I have a 4x4 gridlayout and I'm trying to add 2 title bars above the tables I've created. I'm creating panels for title bars but because of the gridlayout it fills up all the space. Here's the code:
private WebScrollPane createPosRiskPanelWindow(int w, int h)
{
//Content area
posRiskPanel = new WebPanel((new GridLayout(2,2)));
posRiskPanel.setBackground(Color.WHITE);
//Add title bars
WebPanel posTitle = new WebPanel(new WebLabel("Current Positions"));
posRiskPanel.add(posTitle);
WebPanel riskTitle = new WebPanel(new WebLabel("Risk Exposure"));
posRiskPanel.add(riskTitle);
//Panels
CreatePositionPanel(posRiskPanel);
CreateRiskParamsPanel(posRiskPanel);
//Scroll content
posRiskScroll = new WebScrollPane (posRiskPanel, false);
posRiskScroll.setPreferredSize (new Dimension (w, h));
return posRiskScroll;
}
And here's what it looks like:
Is there a way I can resize those top 2 boxes?
Don't use GridLayout for the overall layout, since the only thing it knows to do is to create cells that are all exactly the same size. Since you don't want this, your GUI will require a different layout. Instead use GridBagLayout or else you could use nested JPanels with differing layouts, but I think that GridBagLayout for the overall layout is likely your best bet here. Other considerations include MigLayout if you're willing to use a 3rd party library.
I found it easier to just put the tables in their own panel and a titled border like so:

Why does the last item added to a panel occupy the whole panel?

I'm adding a quantity of JTextField to a panel, and all of them are added but, the last one added takes the whole panel and seems all other text boxes added on the last one..... here is the code
public JPanel crearCartonFormulario() {
panel = new JPanel();
panel.setLayout(new BorderLayout());
JTextField[] textBoxes = new JTextField[25];
int cont = 0;
int posX = 10;
int posY = 0;
llenarArreglo();
while (cont <= 4) {
for (int i = 0; i <= 4; i++) {
if (cont == 2 && i == 2) {
textBoxes[i] = new JTextField("");
} else {
textBoxes[i] = new JTextField(String.valueOf(numeros[cont][i]));
}
textBoxes[i].setBounds(i + posX, 15 + posY, 40, 40);
textBoxes[i].setEditable(false);
panel.add(textBoxes[i]);
posX += 50;
}
posY += 50;
posX = 10;
cont++;
}
return panel;
}
This is returned at a panel where I keep multiple panels of this one, it works but in this one the last JTextField takes the whole panel space....
The new JFrame that contains the panels created by the method, adopt the last JTextField size and that text box doesn't take the bounds indicated by the method, but all the other text boxes still inside and correctly added.
panel.setLayout(new BorderLayout());
You are using a BorderLayout.
panel.add(textBoxes[i]);
When you use the add() method the default is to add the component to the CENTER of the BorderLayout. However, only a single component can be added to the center so the layout manager will only manage the size/location of the last component added. The rules of the BorderLayout is to make the component take up all the available space.
However, you have also used the setBounds() methods for the other text fields which is causing a problem. You should NOT attempt to use a layout manager and manage the bounds of the components yourself.
The solution is to just use a layout manager and let the layout manager do its job. Read the section from the Swing tutorial on Using Layout Managers for more information and use a more appropriate layout manager.
Update:
its a bingo table
Then maybe you shouldn't even be using JTextFields. Maybe a JTable would be a better component to use. The tutorial also has a section on How to Use Tables.
Your problem is here:
panel.setLayout(new BorderLayout());
You set the layout to BorderLayout and yet add components to the JPanel as if it were a GridLayout. Understand that when you add components to a BorderLayout-using container in a default way, the components get added in the BorderLayout.CENTER position which fills this position, covering anything added prevsiously.
Perhaps you wish to use a GridLayout instead? You will want to read the layout manager tutorial for more.
This is because you are using BorderLayout and BorderLaout Always requires a parameter like BorderLayout.CENTER, BorderLayout.WEST, BorderLayout.EAST, BorderLayout.NORTH and BorderLayout.SOUTH.
So basically BorderLayout only has 5 position where a component can go. And if you do not specify where when adding a component it defaults to BorderLayout.CENTER. And as there can only be one component at a time in the BorderLayout.CENTER position it only really adds the last one. So I'd suggest an other layout manager like GridLayout( if you want all the components to be equally sized).
I hope this helps :).
P.S. If you want me to give some explination on GridLayout just ask.

Java: How to nest JPanel in a GridLayout?

I want to know how to nest JPanels using a GridLayout. This is how it should look like.
I approached this problem by 2 ways so far,
using JPanels and
using JLabels,
and none of them worked (only the first panel created is shown).
Here is the code for the JPanel approach:
int x=20, y=20;
JPanel [] panels = new JPanel[3];
JLabel animal = new JLabel(new ImageIcon(getClass().getResource("Pictures/animal.gif")));
JLabel map = new JLabel(new ImageIcon(getClass().getResource("Pictures/map.gif")));
JLabel mountain = new JLabel(new ImageIcon(getClass().getResource("Pictures/mountain.gif")));
for(int i=0;i<panels.length;i++)
{
if(i>0)
{
x+=x;
y+=y;
}
panels[i] = new JPanel(new GridLayout(2,2));
panels[i].setPreferredSize(new Dimension(x,y));
if(i==0)
panels[i].add(new JPanel());
else
panels[i].add(panels[i-1]);
panels[i].add(mountain);
panels[i].add(map);
panels[i].add(animal);
}
add(panels[2]);
One option is to create a class that will represent a panel divided into the grid with the images. The only issue would be the top left quadrant which would usually contain the nested panel, at some point you want this to contain just a blank panel. So maybe something like this (barring various optimizations):
class GridPanel extends JPanel{
JLabel mountain, map, animal;
public GridPanel(JPanel panel){
super();
setLayout(new GridLayout(2, 2));
animal = new JLabel(new ImageIcon(getClass().getResource("pictures/animal.gif")));
map = new JLabel(new ImageIcon(getClass().getResource("pictures/map.gif")));
mountain = new JLabel(new ImageIcon(getClass().getResource("pictures/mountain.gif")));
add(panel);
add(mountain);
add(map);
add(animal);
}
}
Notice that it accepts the panel that is to be displayed in the top left corner of the grid. This coud then be called with the panel specified. So at the point where you want to create the main panel:
JPanel grid = new GridPanel(new JPanel()); //initial
for(int i = 1; i <= 5; i++){
grid = new GridPanel(grid);
}
add(grid);
The initial grid is created with a blank JPanel. And every subsequent grid will contain the previous one as the top left panel. You have to resize your images and such and maybe even avoid loading the images multiple times etc. But that is another question. This example shows 5 nested panels.
Just to be clear, you should use ImageIO to load the images once and reuse the images. For example You can create a BufferedImage like this in your main class:
BufferedImage mointainImg = ImageIO.read(new File("pictures/mountain.gif"));
And when you want to create the JLabel you can do this:
mountain = new JLabel(new ImageIcon(mountainImg));
And the advantage is that you can manipulate the image a bit if you want.
One issue that you have is that the images are not scaled. To scale images, use Image.getScaledInstance(). Proper scaling would at least fix the problem of the visible images being cut off. It also might cause the other images to be shown as they might just be hiding behind the visible images because they are too big.

How to improve GUI by changing BoxLayout into a different LayoutManager?

I've a big problem with Swing in Java, I used BoxLayout for this but still it looks bad.
Any suggestions about my usage of layouts, or how to change it to look like in assumptions? (here are assumptions)
Container main = new Container();
Container left = new Container();// here goin buttons
Container right = new Container(); // here goin tabs + more buttons, textfields and other stuff
BoxLayout lewyL = new BoxLayout(left, BoxLayout.Y_AXIS);
left.setLayout(lewyL);
left.add(rastrowa); //radiobutton
left.add(wektorowa);//radiobutton
left.add(apDwuliniowa);//checkbox
left.add(wczytaj);//button
left.add(zapisz);//obutton
left.add(wyczysc);//button
BoxLayout prawyL = new BoxLayout(right, BoxLayout.Y_AXIS);
right.setLayout(prawyL);
right.add(zakladki);// tabs (mostly i use BoxLayout but for last one i need something more "complicated")
EDIT: I almost solve this problem, I need to move all elements to left (how it look like)but I have no idea how ;/ Here is constructor of this class.
JLabel label = new JLabel("O wektor");
JLabel labelA = new JLabel("a:");
JLabel labelB = new JLabel("b:");
JButton wykonaj = new JButton("Wykonaj");
JTextField a = new JTextField(5);
JTextField b = new JTextField(5);
add(label);
add(labelA);
add(a);
add(labelB);
add(b);
add(wykonaj);
There's nothing wrong with the way it looks (in my opinion), but if you want it to look a little better, why don't you convert the left panel (which is 6x1) into a 3x2 panel, with the checkboxes/radiobuttons on the left, and buttons on the right? Sounds like a job for GridLayout - one of my favorite classes...
JPanel leftPanel = new JPanel(new GridLayout(3,2));
leftPanel.add(rastrowa); //radiobutton
leftPanel.add(wczytaj); //button
leftPanel.add(wektorowa); //radiobutton
leftPanel.add(zapisz); //obutton
leftPanel.add(apDwuliniowa); //checkbox
leftPanel.add(wyczysc); //button
Note that the 3,2 defines the number of rows,columns. When adding panels, they are added to the grid from left-to-right, and top-to-bottom. GridLayout also auto-sizes the components, so all the buttons etc will be the same width and height, making it look more consistent.
The GridLayout documentation might be useful, and the Visual Guide to Layout Managers is a great place to see other layout managers that might work better for your different situations. I personally find BorderLayout and GridLayout to be the most useful, and cover about 95% of the situations I ever need for my GUIs.

Aligning components of a JScrollPane to the left?

Currently, I have a JScrollPane nested within a Panel having a BorderLayout. More importantly, within the JScrollPane, I intend to have multiple text areas listed vertically, with a width that does not exceed the JScrollPane's, and as much height as they need (with the scroll pane having only a vertical scroll bar).
My problem is that the text areas appear to be centered in the JScrollPane relative to the largest text area, and they are all one line.
Can anyone tell me, looking at my code, how I can get the text areas to wrap before the right side of the scroll pane, and how they can be left aligned?
private JPanel generateStateView(State state) {
//Create a new panel, and the info label.
JPanel container = new JPanel(new BorderLayout());
JLabel infoLabel = new JLabel("The state of " + state.getName() +
", with " + state.getElectoralVotes() + " Electoral Votes.");
container.add(infoLabel, BorderLayout.NORTH); //Put the label on top.
//Will scroll through polls.
JScrollPane pollViewer = new JScrollPane();
//This will be the scroll pane's "viewport".
JViewport pollViewport = new JViewport();
//And finally, this will actually hold the individual polls.
JPanel pollPanel = new JPanel(new GridLayout(
state.getPolls().size(), 1));
pollPanel.setAlignmentX(LEFT_ALIGNMENT);
//Iteratively add the polls to the container
for (Poll poll : state.getPolls()) {
//Holds individual polls
Box pollBox = Box.createHorizontalBox();
//Create the poll's panel and add it to the poll container.
pollBox.add(generatePollPanel(poll));
pollBox.add(Box.createHorizontalGlue()); //Fill to the right
pollBox.setAlignmentX(LEFT_ALIGNMENT);
pollPanel.add(pollBox); //Put the box into the pollPanel.
}
//Put the panel into the viewport.
pollViewport.add(pollPanel);
//Put the viewport "into" the scroll pane
pollViewer.setViewport(pollViewport);
//Put the pane into the state view.
container.add(pollViewer, BorderLayout.CENTER);
return container; //And give back the container.
}
/**
* Method: generatePollPanel
* Purpose: Generates a panel containing information on a particular poll.
* #param poll The poll to have information generated from.
* #return The JPanel.
*/
private JPanel generatePollPanel(Poll poll) {
//Create a new panel, then a text area to fill with the info.
JPanel pollPanel = new JPanel();
JTextArea pollInfo = new JTextArea("Conducted by " + poll.getAgency() +
" on day " + poll.getDate() + " for " + poll.getNumDays() +
" days, " + poll.getPercentVoteDem() +
"% voted Democrat, and " + poll.getPercentVoteRep() +
"% voted Republican.");
pollInfo.setEditable(false); //We don't want the user editing this.
pollPanel.add(pollInfo); //Throw the area in, and return the panel.
pollPanel.setAlignmentX(LEFT_ALIGNMENT);
return pollPanel;
}
/**
* Method: valueChanged
* Purpose: Handle the event of a different list item being selected.
* #param event The object containing information about this event.
*/
public void valueChanged(ListSelectionEvent event) {
//Instantiating JList with a type of ? seems to solve the issue of
//eclipse complaining about an unchecked cast (originally to
//JList<String>, so I should be able to cast child values directly
//to string later on anyways.
JList<?> stateList = (JList<?>) event.getSource();
//Account for keyboard and mouse actions
if (!event.getValueIsAdjusting()) {
State chosenState; //Keep track of the picked state.
try {
//Try to get the currently selected state
chosenState = db.findState((String) stateList.getSelectedValue());
}
catch (NoSuchElementException exception) {
//Somehow the picked state was not found, notify the user.
reportError("The selected state is not available.");
return;
}
//Find the container for the gui window.
Container container = getContentPane();
//Remove the empty state view container by generating a new one.
container.remove(currentStateView);
//Generate the state view and add that to the container.
currentStateView = generateStateView(chosenState);
container.add(currentStateView, BorderLayout.CENTER);
//Redraw everything.
revalidate();
}
}
Looks like a severe case of lost-in-nesting :-)
While it nesting can be a means to achieve layout requirements, it can be hard to find out what exactly goes wrong if it doesn't give the intended output. Also, there are general warning signals that something is basically wrong with the nesting
deep levels of containers with wildly varying managers
containers with only a single child
In your case, all except the top-level borderlayout have a single "netto" (that is, not intended to hack around a layout issue) child, with levels:
BorderLayout (the state view)
GridLayout (the panel that's the scrollPane's view)
BoxLayout (the panel that contains a single pollBox)
FlowLayout (the panel that contains the textArea)
The experienced problems:
text areas are single-line without ever wrapping
text areas are centered
are both due to the characteristics of the LayoutManagers, either partly (first) or completely (second):
The part that's not due the manager is that a textArea doesn't wrap by default, you have to explicitly configure it to do so. The part that's due to the manager (== FlowLayout) is that it always lays out its children according to their prefSize, nothing else. That boils down to have a fixed child size, no matter how much space is available for the parent. So even if the textArea were configured to wrap, it would stick to that initial size, no matter how wide its parent can get.
The inner FlowLayout aligns its children ... centered. So whatever alignment you set on the level of the BoxLayout doesn't have any effect. As doesn't the glue: it only takes all the excess space if the other child/ren have a maximum size in that dimension (which a panel with FlowLayout does not have, simply because it's that simple LayoutManager doesn't have the notion of max, only managers of type LayoutManager2 have)
Time to take a step back and review the inital requirement:
multiple text areas listed vertically, with a width that does not
exceed the JScrollPane's, and as much height as they need
The zeroth order solution is to
configure the textArea as needed
throw out all layout nesting (inside the scrollPane)
In code:
JComponent overview = new JPanel(new BorderLayout());
overview.add(new JLabel("All polls for state XY"), BorderLayout.NORTH);
JComponent pollPanel = new JPanel();
pollPanel.setLayout(new BoxLayout(pollPanel, BoxLayout.PAGE_AXIS));
for (int i = 0; i < 10; i++) {
pollPanel.add(generatePollTextArea(i)); //Put the box into the pollPanel.
}
pollPanel.add(Box.createVerticalGlue());
JScrollPane pollViewer = new JScrollPane(pollPanel);
overview.add(pollViewer);
showInFrame(overview, "layout text only");
protected JComponent generatePollTextArea(int i) {
String agency = "agency ";
// simulates different size text
for (int j = 0; j < i; j++) {
agency += i;
}
String text = "Conducted by " + agency +
" on day " + "today" + " for " + 20 +
" days, " + 99 +
"% voted Democrat, and " + 0.2 +
"% voted Republican.";
JTextArea pollInfo = new JTextArea(text);
// give it some reasonable initial width
// in terms of content
pollInfo.setColumns(20);
pollInfo.setLineWrap(true);
pollInfo.setWrapStyleWord(true);
pollInfo.setEditable(false); //We don't want the user editing this.
return pollInfo;
}
"Das Wort zum Dienstag": nesting layouts isn't a means of avoiding to learn the characteristics of the LayoutManagers used on the individual levels of the nested layout.
Firstly, I don't think Box is suppose to be used with anything other then the BoxLayout (I could be wrong, but that's how I read it)
Personally, I'd either use either GridBagLayout or VerticalLayout from SwingLabs.
In order to facilitate the horizontal restrictions, you will also want to take a look at the Scrollable interface, in particular getScrollableTracksViewportWidth

Categories