0

I am having an issue where it seem like my JPanel is taking half of my screen no matter what, even though it doesn't need to.

I am using the acm package to make a graphical simulation, with sliders to adjust the parameters of a simulation

import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import acm.gui.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Test extends GraphicsProgram{

sliderBox X_MIN = new sliderBox("X MIN", 10, 300, 50);

public void init() {

    JPanel myPanel = new JPanel();
    JLabel myLabel = new JLabel("Test");
    TableLayout myLayout = new TableLayout(5, 1);
    myPanel.setVisible(true);
    myPanel.setLayout(myLayout);
    myPanel.add(myLabel);
    myPanel.add(X_MIN.myPanel);
    add(myPanel);
    println(myPanel.getPreferredSize());

}


public void run() {

    resize(1500, 900);
    add(new GRect(10, 10, 60, 50));

}
}

Though I can't upload pictures on my post due to me being new, the resulting output is a window where half of the screen is the white canvas from the acm.graphics and the second half of the screen is an unnecessarily big gray panel that doesn't optimize the containment of the few components that are actually in the panel

Thanks in advance

**Edit here is my sliderBox class

import java.awt.*;
import javax.swing.*;
import acm.gui.*;

public class sliderBox {

JPanel myPanel;
JLabel nameLabel;
JLabel minLabel;
JSlider mySlider;
JLabel maxLabel;
JLabel sReadout;
int imin;
int imax;
double dmin;
double dmax;

public sliderBox (String name, Integer min, Integer max, Integer iniValue) {

    myPanel = new JPanel();
    nameLabel = new JLabel(name);
    minLabel = new JLabel(min.toString());
    mySlider = new JSlider(min, max, iniValue);
    maxLabel = new JLabel(max.toString());
    sReadout = new JLabel(iniValue.toString());
    sReadout.setForeground(Color.BLUE);
    myPanel.setLayout(new TableLayout(1, 5));
    myPanel.add(nameLabel, "width = 80");
    myPanel.add(minLabel, "width = 20");
    myPanel.add(mySlider, "width = 120");
    myPanel.add(maxLabel, "width = 60" );
    myPanel.add(sReadout, "width = 80");
    imin = min;
    imax = max;

}


}
AlexCh
  • 1
  • 1
  • What is `TableLayout` width and height? – Sam Orozco Nov 03 '18 at 06:13
  • I am not sure how to get those values, but the values i get in the console from the getPreferredSize method indicate (W=25, H=16) – AlexCh Nov 03 '18 at 06:29
  • `here is my sliderBox class` - class names should start with an upper case character. Follow Java conventions. `my JPanel is taking half of my screen` - TableLayout is not a standard layout so I don't know what it does. I'm guessing each component is getting half the space of the frame. Why don't you use a `BorderLayout`. It allows you to have one component displayed at its preferred size and the other will take the remaining space in the frame. Read the section from the Swing tutorial on [How to Use BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) – camickr Nov 03 '18 at 15:30
  • I have tried using `BorderLayout` and `GridLayout` for the JPanel on the main class, but the grey screen is still covering half of the frame screen. [link](https://i.imgur.com/NJYpcYK.png) – AlexCh Nov 03 '18 at 16:11

0 Answers0