Related
I have old Java NetBenas project that contains UI classes derived from JPanel. Code editor does not contain Design button in these classes. Design button appears only in newly created JPanel fom. Is it possible somehow tell NetBeans to open old forms in Design mode?
Form that can't be opened in Design mode:
public class About extends javax.swing.JPanel /*JFrame*/ {
private ClassLoader cl = getClass().getClassLoader();
public ImageIcon logo = new ImageIcon(
cl.getResource("icons/logo.gif"));
public ImageIcon ball = new ImageIcon(
cl.getResource("icons/ball.gif"));
private JPanel centerPanel;
private JPanel topPanel;
private JLabel productName = new JLabel("<html><font face=\"verdana\" size=10>"+
"SocketTest",logo,JLabel.CENTER);
private JTextArea readme = new JTextArea();
private JScrollPane jsp;
String html="<html><font face=\"verdana\" size=\"2\">";
private JLabel versionText = new JLabel(html+"Version",ball,JLabel.LEFT);
private JLabel version = new JLabel(html+": 3.0.0", JLabel.LEFT);
private JLabel licenceText = new JLabel(html+"Licence",ball,JLabel.LEFT);
private JLabel licence = new JLabel(html+": GNU Lesser General Public License", JLabel.LEFT);
private JLabel authorText = new JLabel(html+"Author",ball,JLabel.LEFT);
private JLabel author = new JLabel(html+": Akshathkumar Shetty", JLabel.LEFT);
private JLabel copyrightText = new JLabel(html+"Copyright © 2003-2008 Akshathkumar Shetty",ball,JLabel.LEFT);
private JLabel websiteText = new JLabel(html+"Website",ball,JLabel.LEFT);
private JLabel website = new JLabel(html+": http://sockettest.sourceforge.net/", JLabel.LEFT);
private JLabel readmeText = new JLabel(html+"ReadMe",ball,JLabel.LEFT);
private GridBagConstraints gbc = new GridBagConstraints();
/** Creates a new instance of About */
public About() {
//Container cp = getContentPane();
Container cp = this;
topPanel = new JPanel();
topPanel.setLayout(new GridBagLayout());
gbc.insets = new Insets( 2, 2, 2, 2 );
gbc.weighty = 0.0;
gbc.weightx = 0.0;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 3;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
topPanel.add(productName, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 1;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(versionText, gbc);
gbc.gridx = 1;
topPanel.add(version, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 2;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(licenceText, gbc);
gbc.gridx = 1;
topPanel.add(licence, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;//1.0
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(new JLabel(), gbc);
gbc.gridy = 3;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(authorText, gbc);
gbc.gridx = 1;
topPanel.add(author, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 4;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(websiteText, gbc);
gbc.gridx = 1;
topPanel.add(website, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 5;
gbc.gridx = 0;
gbc.weightx = 0.0;
gbc.gridwidth = 2;
topPanel.add(copyrightText, gbc);
gbc.gridwidth = 1;
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 6;
gbc.gridx = 0;
gbc.weightx = 0.0;
gbc.gridwidth = 1;
topPanel.add(readmeText, gbc);
gbc.gridx = 1;
topPanel.add(new JLabel(" "), gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
centerPanel = new JPanel();
readme.setText("Loading... readme");
try {
String cont = Util.readFile("readme.txt",(Object)About.this);
readme.setText(cont);
} catch (IOException e){
System.err.println("Error reading readme.txt "+e);
readme.append("\r\nFailed : "+e.getMessage());
}
readme.setEditable(false);
readme.setLineWrap(true);
readme.setWrapStyleWord(true);
jsp = new JScrollPane(readme);
centerPanel.setLayout(new BorderLayout());
centerPanel.add(jsp);
centerPanel.setBorder(BorderFactory.createEmptyBorder(0,9,0,9));
cp.setLayout(new BorderLayout(0,10));
cp.add(topPanel,BorderLayout.NORTH);
cp.add(centerPanel,BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
}
}
Using Java, I am creating a form with GridBagLayout. the GUI is inside a JPanel, which is inside a scrollPane which is added to a JFrame.
I want the JPanel with the GUI to be a fixed size to prevent the form being stretched, however, I would like the JScrollPanel to be resizable for the user's preference. (Similar to MS Paint, with the canvas being my JPanel). The reason I want to prevent the form from being stretched is because I want it to print and from the way I have it now, the printed form prints pretty much the way I want it, but alters unfavorably if the frame as been resized prior to printing.
Perhaps my problem is better to be solved in the way I print, but the problem is that I have never dealt with this level of printing before and was unable to find similar questions/tutorials to help me solve my problem. The direction I am going may be "the poor man's way" of doing it, but its the only one that makes sense to me right now, I'm a fairly novice Java programmer.
FrontierMain.java
public class FrontierMain {
public FrontierMain(){
JFrame frame = new JFrame();
frame.setTitle("Frontier Insulation Labor Record Tool");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
LaborRecordPanel recordPanel = new LaborRecordPanel();
frame.add(recordPanel.scrollPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
FrontierMain fm = new FrontierMain();
}
});
System.out.println("Hello, World");
}
}
LaborRecordPanel.java
public class LaborRecordPanel implements
Printable, ActionListener {
private Color shade = new Color(201,201,201); // color for shaded cells
private JLabel dateSpace[] = new JLabel[7];
private JLabel grandTotalSpace = new JLabel();
private JLabel locAndDescSpace = new JLabel();
private JLabel personnelSpace[] = new JLabel[10]; // empty cells
private JLabel calendarGridLines[] = new JLabel[300]; //empty labels for gridlines
private ImageIcon logoIcon = new ImageIcon("Images/fici_logo1.jpg");
private JLabel logoLabel = new JLabel(logoIcon);
private JLabel authorizedBy = new JLabel("AUTHORIZED BY:");
private JLabel toCertify = new JLabel("THIS IS TO CERTIFY THAT THE ABOVE LABOR HAS BEEN PERFORMED.");
private JLabel laborRecordNO = new JLabel("NO.");
private JLabel nameOfJob = new JLabel("NAME OF JOB:");
private JLabel customerPO = new JLabel("CUSTOMER PO #:");
private JLabel contractNO = new JLabel("CONTRACT NO.");
private JLabel weekEnding = new JLabel("WEEK ENDING");
private JLabel personnelList = new JLabel("<html>LIST SUPERVISION &<br> CRAFT LABOR BELOW:</html>");
private JLabel locAndDescriptionLabel = new JLabel("LOCATION AND DESCRIPTION:");
private JLabel personnelTitle = new JLabel("TITLE");
private JLabel supt = new JLabel("SUPT.");
private JLabel foreman = new JLabel("FOREMAN");
private JLabel[] mechanic= new JLabel[8];
private JLabel calendarTitle = new JLabel("NUMBER OF HOURS WORKED # SITE");
private JLabel dayHeading = new JLabel("DAY");
private JLabel dateHeading = new JLabel("DATE");
private JLabel[] ot2 = new JLabel[10];
private JLabel[] ot1 = new JLabel[10];
private JLabel[] st = new JLabel[10];
private JLabel mon = new JLabel("MON");
private JLabel tues = new JLabel("TUES");
private JLabel wed = new JLabel("WED");
private JLabel thur = new JLabel("THUR");
private JLabel fri = new JLabel("FRI");
private JLabel sat = new JLabel("SAT");
private JLabel sun = new JLabel("SUN");
private JLabel totalHours = new JLabel("<html>TOTAL<br> HOURS</html>");
private JLabel ratePerHour = new JLabel("<html>RATE<br> PER<br> HOUR</html>");
private JLabel totalAmount = new JLabel("<html>TOTAL<br> AMOUNT</html>");
private JLabel grandTotal = new JLabel("TOTAL");
JPanel rp = new JPanel();
JScrollPane scrollPane = new JScrollPane(rp);
LaborRecordPanel(){
rp.setPreferredSize(new Dimension(1295,1830 ));
rp.setMinimumSize(new Dimension(1295,1830 ));
rp.setMaximumSize(new Dimension(1295,1830 ));
scrollPane.setPreferredSize(new Dimension(900,700 ));
scrollPane.getVerticalScrollBar().setUnitIncrement(16); //increase the scroll speed
for (int i = 0; i <= 7; i++) mechanic[i] = new JLabel("MECHANIC"); // create mechanic labels
for (int i = 0; i <= 9; i++) //create labels for work time
{
ot2[i] = new JLabel("OT-2");
ot1[i] = new JLabel("OT-1");
st[i] = new JLabel("S.T.");
}
//create empty labels for gridlines
for (int i = 0; i <= 9; i++) personnelSpace[i] = new JLabel();
for (int i = 0; i <= 6; i++) dateSpace[i] = new JLabel();
for (int i = 0; i <= 299; i++) calendarGridLines[i] = new JLabel();
GridBagLayout gridbag = new GridBagLayout();
rp.setBackground(Color.WHITE);
rp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
GridBagConstraints gbc = new GridBagConstraints();
rp.setLayout(gridbag);
//gbc.insets = new Insets(5, 5, 5, 5);
//row 0////////////////////////////////////////////////////////////
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 10;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridheight = 1;
gbc.gridwidth = 1;
laborRecordNO.setHorizontalAlignment(JLabel.CENTER);
laborRecordNO.setFont(new Font("Dialog", Font.PLAIN, 18));
gridbag.setConstraints(laborRecordNO, gbc);
rp.add(laborRecordNO, gbc);
//row 1////////////////////////////////////////////////////////////
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 13;
gridbag.setConstraints(logoLabel, gbc);
rp.add(logoLabel, gbc);
//row 2////////////////////////////////////////////////////////////
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridheight = 1;
gbc.gridwidth = 1;
nameOfJob.setFont(nameOfJob.getFont().deriveFont(18.0f));
gridbag.setConstraints(nameOfJob, gbc);
rp.add(nameOfJob, gbc);
gbc.gridx = 6;
gbc.gridy = 2;
gbc.gridheight = 1;
gbc.gridwidth = 3;
contractNO.setFont(contractNO.getFont().deriveFont(18.0f));
gridbag.setConstraints(contractNO, gbc);
rp.add(contractNO, gbc);
//row 3////////////////////////////////////////////////////////////
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridheight = 1;
gbc.gridwidth = 1;
customerPO.setFont(customerPO.getFont().deriveFont(18.0f));
gridbag.setConstraints(customerPO, gbc);
rp.add(customerPO, gbc);
gbc.gridx = 6;
gbc.gridy = 3;
gbc.gridheight = 1;
gbc.gridwidth = 3;
weekEnding.setFont(weekEnding.getFont().deriveFont(18.0f));
gridbag.setConstraints(weekEnding, gbc);
rp.add(weekEnding, gbc);
//row 4////////////////////////////////////////////////////////////
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridheight = 3;
gbc.gridwidth = 1;
personnelList.setHorizontalAlignment(JLabel.CENTER);
personnelList.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
personnelList.setFont(personnelList.getFont().deriveFont(18.0f));
gridbag.setConstraints(personnelList, gbc);
rp.add(personnelList, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridheight = 3;
gbc.gridwidth = 1;
personnelTitle.setHorizontalAlignment(JLabel.CENTER);
personnelTitle.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.BLACK));
personnelTitle.setFont(personnelTitle.getFont().deriveFont(18.0f));
gridbag.setConstraints(personnelTitle, gbc);
rp.add(personnelTitle, gbc);
gbc.gridx = 2;
gbc.gridy = 4;
gbc.gridwidth = 8;
gbc.gridheight = 1;
calendarTitle.setHorizontalAlignment(JLabel.CENTER);
calendarTitle.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, Color.BLACK));
calendarTitle.setFont(calendarTitle.getFont().deriveFont(18.0f));
gridbag.setConstraints(calendarTitle, gbc);
rp.add(calendarTitle, gbc);
gbc.gridx = 10;
gbc.gridy = 4;
gbc.gridheight = 3;
gbc.gridwidth = 1;
totalHours.setHorizontalAlignment(JLabel.CENTER);
totalHours.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
totalHours.setFont(totalHours.getFont().deriveFont(18.0f));
gridbag.setConstraints(totalHours, gbc);
rp.add(totalHours, gbc);
gbc.gridx = 11;
gbc.gridy = 4;
gbc.gridheight = 3;
gbc.gridwidth = 1;
ratePerHour.setHorizontalAlignment(JLabel.CENTER);
ratePerHour.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.BLACK));
ratePerHour.setFont(ratePerHour.getFont().deriveFont(18.0f));
gridbag.setConstraints(ratePerHour, gbc);
rp.add(ratePerHour, gbc);
gbc.gridx = 12;
gbc.gridy = 4;
gbc.gridheight = 3;
gbc.gridwidth = 1;
totalAmount.setHorizontalAlignment(JLabel.CENTER);
totalAmount.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
totalAmount.setFont(totalAmount.getFont().deriveFont(18.0f));
gridbag.setConstraints(totalAmount, gbc);
rp.add(totalAmount, gbc);
//row 5//////////////////////////////////////////////////////////////////
gbc.gridx = 2;
gbc.gridy = 5;
gbc.gridheight = 1;
gbc.gridwidth = 1;
dayHeading.setHorizontalAlignment(JLabel.CENTER);
dayHeading.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
dayHeading.setFont(dayHeading.getFont().deriveFont(18.0f));
gridbag.setConstraints(dayHeading, gbc);
rp.add(dayHeading, gbc);
gbc.gridx = 3;
gbc.gridy = 5;
gbc.gridheight = 1;
gbc.gridwidth = 1;
mon.setHorizontalAlignment(JLabel.CENTER);
mon.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
mon.setFont(mon.getFont().deriveFont(18.0f));
gridbag.setConstraints(mon, gbc);
rp.add(mon, gbc);
gbc.gridx = 4;
gbc.gridy = 5;
gbc.gridheight = 1;
gbc.gridwidth = 1;
tues.setHorizontalAlignment(JLabel.CENTER);
tues.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
tues.setFont(tues.getFont().deriveFont(18.0f));
gridbag.setConstraints(tues, gbc);
rp.add(tues, gbc);
gbc.gridx = 5;
gbc.gridy = 5;
gbc.gridheight = 1;
gbc.gridwidth = 1;
wed.setHorizontalAlignment(JLabel.CENTER);
wed.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
wed.setFont(wed.getFont().deriveFont(18.0f));
gridbag.setConstraints(wed, gbc);
rp.add(wed, gbc);
gbc.gridx = 6;
gbc.gridy = 5;
gbc.gridheight = 1;
gbc.gridwidth = 1;
thur.setHorizontalAlignment(JLabel.CENTER);
thur.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
thur.setFont(thur.getFont().deriveFont(18.0f));
gridbag.setConstraints(thur, gbc);
rp.add(thur, gbc);
gbc.gridx = 7;
gbc.gridy = 5;
gbc.gridheight = 1;
gbc.gridwidth = 1;
fri.setHorizontalAlignment(JLabel.CENTER);
fri.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
fri.setFont(fri.getFont().deriveFont(18.0f));
gridbag.setConstraints(fri, gbc);
rp.add(fri, gbc);
gbc.gridx = 8;
gbc.gridy = 5;
gbc.gridheight = 1;
gbc.gridwidth = 1;
sat.setHorizontalAlignment(JLabel.CENTER);
sat.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
sat.setFont(sat.getFont().deriveFont(18.0f));
gridbag.setConstraints(sat, gbc);
rp.add(sat, gbc);
gbc.gridx = 9;
gbc.gridy = 5;
gbc.gridheight = 1;
gbc.gridwidth = 1;
sun.setHorizontalAlignment(JLabel.CENTER);
sun.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
sun.setFont(sun.getFont().deriveFont(18.0f));
gridbag.setConstraints(sun, gbc);
rp.add(sun, gbc);
//row 6//////////////////////////////////////////////////////////////
gbc.gridx = 2;
gbc.gridy = 6;
gbc.gridheight = 1;
gbc.gridwidth = 1;
dateHeading.setHorizontalAlignment(JLabel.CENTER);
dateHeading.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
dateHeading.setFont(dateHeading.getFont().deriveFont(18.0f));
gridbag.setConstraints(dateHeading, gbc);
rp.add(dateHeading, gbc);
int dateSpaceIndex = 3;
boolean flip = true;
for (int k = 0; k <= 6; k++)//create gridlines for day area
{
gbc.gridx = dateSpaceIndex;
gbc.gridy = 6;
gbc.gridheight = 1;
gbc.gridwidth = 1;
if(flip) dateSpace[k].setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
else dateSpace[k].setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
gridbag.setConstraints(dateSpace[k], gbc);
rp.add(dateSpace[k], gbc);
dateSpaceIndex++;
flip = !flip;
}
//row 7/////////////////////////////////////////////////////////////
gbc.gridx = 1;
gbc.gridy = 7;
gbc.gridheight = 3;
gbc.gridwidth = 1;
supt.setHorizontalAlignment(JLabel.CENTER);
supt.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
supt.setFont(supt.getFont().deriveFont(18.0f));
gridbag.setConstraints(supt, gbc);
rp.add(supt, gbc);
//row 10///////////////////////////////////////////////////////////
gbc.gridx = 1;
gbc.gridy = 10;
gbc.gridheight = 3;
gbc.gridwidth = 1;
foreman.setHorizontalAlignment(JLabel.CENTER);
foreman.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
foreman.setFont(foreman.getFont().deriveFont(18.0f));
gridbag.setConstraints(foreman, gbc);
rp.add(foreman, gbc);
//row 13-36 plus 7-12 time worked labels//////////////////////////
for (int r = 0; r <= 7; r++)
{
gbc.gridx = 1;
gbc.gridy = 13 + (3*r);
gbc.gridheight = 3;
gbc.gridwidth = 1;
mechanic[r].setHorizontalAlignment(JLabel.CENTER);
mechanic[r].setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
mechanic[r].setFont(mechanic[r].getFont().deriveFont(18.0f));
gridbag.setConstraints(mechanic[r], gbc);
rp.add(mechanic[r], gbc);
}
for (int c = 0; c <= 9; c++){
gbc.gridx = 2;
gbc.gridy = 7 + (3*c);
gbc.gridheight = 1;
gbc.gridwidth = 1;
ot2[c].setHorizontalAlignment(JLabel.CENTER);
ot2[c].setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
ot2[c].setFont(ot2[c].getFont().deriveFont(18.0f));
gridbag.setConstraints(ot2[c], gbc);
rp.add(ot2[c], gbc);
gbc.gridx = 2;
gbc.gridy = 8 + (3*c);
gbc.gridheight = 1;
gbc.gridwidth = 1;
ot1[c].setHorizontalAlignment(JLabel.CENTER);
ot1[c].setOpaque(true);
ot1[c].setBackground(shade);
ot1[c].setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
ot1[c].setFont(ot1[c].getFont().deriveFont(18.0f));
gridbag.setConstraints(ot1[c], gbc);
rp.add(ot1[c], gbc);
gbc.gridx = 2;
gbc.gridy = 9 + (3*c);
gbc.gridheight = 1;
gbc.gridwidth = 1;
st[c].setHorizontalAlignment(JLabel.CENTER);
st[c].setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
st[c].setFont(st[c].getFont().deriveFont(18.0f));
gridbag.setConstraints(st[c], gbc);
rp.add(st[c], gbc);
}
//row 37/////////////////////////////////////////////////////////////////
gbc.gridx = 0;
gbc.gridy = 37;
gbc.gridheight = 1;
gbc.gridwidth = 1;
locAndDescriptionLabel.setFont(locAndDescriptionLabel.getFont().deriveFont(18.0f));
gridbag.setConstraints(locAndDescriptionLabel, gbc);
rp.add(locAndDescriptionLabel);
gbc.gridx = 11;
gbc.gridy = 37;
gbc.gridheight = 1;
gbc.gridwidth = 1;
grandTotal.setHorizontalAlignment(JLabel.CENTER);
grandTotal.setOpaque(true);
grandTotal.setBackground(Color.BLACK);
grandTotal.setForeground(Color.WHITE);
grandTotal.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
grandTotal.setFont(grandTotal.getFont().deriveFont(18.0f));
gridbag.setConstraints(grandTotal, gbc);
rp.add(grandTotal);
gbc.gridx = 12;
gbc.gridy = 37;
gbc.gridwidth = 1;
gbc.gridheight = 1;
grandTotalSpace.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
gridbag.setConstraints(grandTotalSpace, gbc);
rp.add(grandTotalSpace);
//row 38////////////////////////////////////////////////////////////////
gbc.gridx = 0;
gbc.gridy = 38;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.ipady = 80;
locAndDescSpace.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
gridbag.setConstraints(locAndDescSpace, gbc);
rp.add(locAndDescSpace);
//row 39////////////////////////////////////////////////////////////////
gbc.ipady = 0; //reset to default
gbc.gridx = 0;
gbc.gridy = 39;
gbc.gridheight = 1;
gbc.gridwidth = 2;
toCertify.setFont(toCertify.getFont().deriveFont(18.0f));
gridbag.setConstraints(toCertify, gbc);
rp.add(toCertify);
//row 40///////////////////////////////////////////////////////////////
gbc.gridx = 0;
gbc.gridy = 40;
gbc.gridheight = 1;
gbc.gridwidth = 3;
authorizedBy.setFont(authorizedBy.getFont().deriveFont(18.0f));
gridbag.setConstraints(authorizedBy, gbc);
rp.add(authorizedBy);
for (int r = 0; r <= 9; r++)//gridlines for personnel space
{
gbc.gridx = 0;
gbc.gridy = 7 + (3*r);
gbc.gridheight = 3;
gbc.gridwidth = 1;
personnelSpace[r].setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
gridbag.setConstraints(personnelSpace[r], gbc);
rp.add(personnelSpace[r]);
}
//create calendar grid lines
int yPointer = 7;
int xPointer = 3;
int shadePtr = 8;
for (int j = 0; j <= 299; j++)
{
gbc.gridx = xPointer;
gbc.gridy = yPointer;
gbc.gridheight = 1;
gbc.gridwidth = 1;
calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
if (yPointer == shadePtr){ //if row number = shadePtr then color the cell
calendarGridLines[j].setOpaque(true);
calendarGridLines[j].setBackground(shade);
}
gridbag.setConstraints(calendarGridLines[j], gbc);
rp.add(calendarGridLines[j]);
xPointer++; //go to next cell in row
j++; //use the next jlabel
gbc.gridx = xPointer;
gbc.gridy = yPointer;
gbc.gridheight = 1;
gbc.gridwidth = 1;
calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
if (yPointer == shadePtr){ //if row number = shadePtr then color the cell
calendarGridLines[j].setOpaque(true);
calendarGridLines[j].setBackground(shade);
}
gridbag.setConstraints(calendarGridLines[j], gbc);
rp.add(calendarGridLines[j]);
xPointer++; //go to next cell in row
if(xPointer == 13) //if end of column then go to next row and reset column pointer to 3 and increment shade pointer by 3
{
yPointer++; //go down a row
xPointer = 3;
if((j % 3) == 0) {
shadePtr = yPointer;
}
}
}
JButton printTest = new JButton("PrintTest");
printTest.addActionListener(this);
gbc.gridx = 0;
gbc.gridy = 41;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gridbag.setConstraints(printTest, gbc);
rp.add(printTest);
}
#Override
public void actionPerformed(ActionEvent e) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog()){
try{
printJob.print();
}
catch(Exception ex){
throw new RuntimeException(ex);
}
}
}
#Override
public int print(Graphics g, PageFormat pf, int index)
throws PrinterException {
Graphics2D g2 = (Graphics2D)g;
if (index >= 1){
return Printable.NO_SUCH_PAGE;
}
else {
AffineTransform originalTransform = g2.getTransform();
double scaleX = pf.getImageableWidth() / rp.getWidth();
double scaleY = pf.getImageableHeight() / rp.getHeight();
// Maintain aspect ratio
double scale = Math.min(scaleX, scaleY);
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.scale(scale, scale);
rp.printAll(g2);
g2.setTransform(originalTransform);
return Printable.PAGE_EXISTS;
}
}
}
I put my main GUI panel inside another JPanel, which sits inside the JScrollPane. This prevents my main GUI from resizing to the frame size; instead the "outer" JPanel is what gets resized.
On your JPanel you can use both :
setSize( w, h)
setPreferredSize()
Or maybe add a component listener to handle manually the "repaint / invalidate" on resize events
JPanel component = new JPanel();
component.addComponentListener(new ComponentListener()
{
public void componentResized(ComponentEvent evt) {
Component c = (Component)evt.getSource();
//........
}
});
I have JPanel inside vertical JSplitPane. JPanel contains jlabels and jtextfields. When I shrink height of JPanel by moving JSplitPane's divider All components in jpanel are resized themselves. How to tell them not to resize when height of parent jpanel shrinks.
I cannot size minSize for JPanel because it makes impossible for JSplitPane to move divider.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setDividerSize(10);
splitPane.setOneTouchExpandable(true);
JPanel searchPanel = createSearchPanel();
splitPane.setDividerLocation(searchPanel.getPreferredSize().height + 5);
splitPane.setTopComponent(searchPanel);
As you see there is searchPanel. Let's see it:
JPanel searchPanel = new JPanel(new GridBagLayout()) {
Dimension minSize = new Dimension(200, 0);
#Override
public Dimension getMinimumSize() {
return minSize;
}
};
searchPanel.setBorder(BorderFactory.createTitledBorder("Search query"));
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
new Insets(5, 2, 5, 3), 0, 0);
searchPanel.add(eventLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
searchPanel.add(eventLabelField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
searchPanel.add(timestampLabel, gbc);
gbc.gridx = 1;
searchPanel.add(timestampStartField, gbc);
gbc.gridx = 2;
searchPanel.add(timestammpToLabel, gbc);
gbc.gridx = 3;
searchPanel.add(timestampEndField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
searchPanel.add(locationLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 3;
searchPanel.add(locationField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
searchPanel.add(durationMagnitudeLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
searchPanel.add(durationMagnitudeMinField, gbc);
gbc.gridx = 2;
searchPanel.add(durationToLabel, gbc);
gbc.gridx = 3;
searchPanel.add(durationMagnitudeMaxField, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.weighty = 1.0;
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.insets = new Insets(15, 0, 0, 0);
searchPanel.add(searchButton, gbc);
When I move up vertical divider the height of searchPanel shrinks and components look like:
You see that jtextFields got much smaller than they were after I moved divider up
Please, help me here.
I have changed your code. Use weightx property of GridBagConstraint with GridBagConstraints.HORIZONTAL fill, it helps components to fill their cell properly.
Example code:
JPanel searchPanel = new JPanel(new GridBagLayout()) {
Dimension minSize = new Dimension(200, 0);
#Override
public Dimension getMinimumSize() {
return minSize;
}
};
searchPanel.setBorder(BorderFactory.createTitledBorder("Search query"));
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
new Insets(5, 2, 5, 3), 0, 0);
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.weighty = 1.0;
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.insets = new Insets(15, 0, 0, 0);
gbc.weightx = 0;
searchPanel.add(new JButton("b"), gbc);
return searchPanel;
Watch docs How to use GridBagLayout
Results:
Edit: Sorry, I'm using awful names for labels)
Add a dummy JPanel to the bottom and set weightY=1 for the JPanel. ALl the rest component should have weightY=0. Thus on height increasing all the additional pixels are targeted to the dummy JPanel.
I have to make this for school:
This is the code I have so far:
import javax.swing.*;
import java.awt.*;
public class AddressBookGui1 extends JFrame {
public AddressBookGui1(){
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
JLabel label;
JButton button;
JTextField textField;
JTextArea textArea = new JTextArea(10, 20);
gbc.weightx = 1;
label = new JLabel("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 0;
add(textField ,gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
add(textField, gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
add(textField, gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
add(label ,gbc);
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 3;
add(textArea, gbc);
gbc.weightx = 1;
button = new JButton("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
add(button ,gbc);
gbc.weightx = 1;
button = new JButton("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 3;
gbc.gridy = 4;
add(button ,gbc);
}
public static void main(String[] args){
AddressBookGui1 frame = new AddressBookGui1();
frame.setTitle("Address Book");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
(I still need to deal with padding and insets. I've gotten those to work in a much simpler program so I think I have a handle on that stuff)
I've tried the GridBagLayout Oracle tutorial and I'm not sure what I'm doing wrong. Can someone help me make it look more like it is supposed to? Specifically to make the text fields and text area span over 2 cells.
Few things I noticed about your code.
Do not use setSize() of JFrame. This will cause abnormal behavior.
Instead, let the frame size itself according to the size of its
components. If you want the frame to be bigger, adjust not the size
of the frame but the components inside it. You can either
setpreferredSize or override the getpreferredsize of the component if
you really want to adjust is size since GridBagLayout is one of those layout managers that respects the
preferredSize of the component. Use pack() to remove the unecessary
space.
Do not extend a JFrame, make your UI class to have a main panel and
add all the components there. provide a getter for that panel (e.g.
getUI()) for extracting the UI of that class.
Always reinstantiate the GridBagConstraints object whenever it is
going to be applied to another component. This way it is more
readable.
Use insets to put padding around the component.
Do not reuse same reference to different components;
Use Initial Thread
This is not standard but it I find it really helpful when working
with GridBagLayout, in setting the constraints of gbc, make it in
alphabetical order.
To solve your problem, here is the modified code with the things I pointed out about applied.
public class AddressBook {
private JPanel pnlMain;
public AddressBook() {
pnlMain = new JPanel();
pnlMain.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel lblName = new JLabel("Name");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblName, gbc);
JTextField txtName = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(txtName, gbc);
JLabel lblPhone = new JLabel("Phone");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblPhone, gbc);
JTextField txtPhone = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(txtPhone, gbc);
JLabel lblEmail = new JLabel("Email");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblEmail, gbc);
JTextField txtEmail = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.insets = new Insets(5, 0, 0, 10);
pnlMain.add(txtEmail, gbc);
JLabel lblAddress = new JLabel("Address");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblAddress, gbc);
JTextArea txtAreaAddress = new JTextArea(10, 20);
JScrollPane pane = new JScrollPane(txtAreaAddress);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 3;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(pane, gbc);
JButton btnSave = new JButton("Save");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
gbc.insets = new Insets(10, 10, 10, 0);
gbc.weightx = 1;
pnlMain.add(btnSave, gbc);
JButton btnCancel = new JButton("Cancel");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 1;
gbc.gridx = 3;
gbc.gridy = 4;
gbc.insets = new Insets(10, 0, 10, 10);
gbc.weightx = 1;
pnlMain.add(btnCancel, gbc);
}
public JPanel getUI(){
return pnlMain;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Address Book");
frame.getContentPane().add(new AddressBook().getUI());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
gbc.gridwidth is the parameter that allows a component to span more than one column. For example, if you have 3 columns and 4 rows and you want a label to occupy the complete top row, then you need to assign the first cell for the label. and set gbc.gridwidth = 3;
my JScroll pane contains a Jlist, and is parented by a JPanel that has the gridbag layour, but it doesn't want to fit right it ends up really small and acts like its gridx is -1 and y is zero, when it should have a x of 0 and y of 1
code:
public class StartGui
{
private static JFrame frame = new JFrame("");
private static JPanel panel = new JPanel();
private static JMenuBar menu = new JMenuBar();
private static DefaultListModel<String> listModel = new DefaultListModel<String>();
private static JList<String> Targets = new JList<String>(listModel);
private static JButton Start = new JButton("Start");
private static JButton Stop = new JButton("Stop");
private static JButton Configure = new JButton("Configure");
private static JButton AddRecipiants = new JButton("Add Recipiants");
private static JProgressBar Progress = new JProgressBar();
private static JLabel RLable = new JLabel("Recipiants:");
private static JScrollPane lsp;
private static GridBagLayout gbl = new GridBagLayout();
private static GridBagConstraints gbc = new GridBagConstraints();
private static JFrame emails = new JFrame();
private static JPanel panel2 = new JPanel();
private static JTextArea emailA = new JTextArea("type email here");
private static JButton done = new JButton("OK");
public static void main(String[] args)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menu);
frame.setResizable(false);
frame.setBounds(0, 0, 500, 400);
panel.setLayout(gbl);
listModel.addElement("TestEmailAddress#fake.com");
lsp = new JScrollPane(Targets);
emails.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
emails.setResizable(false);
emails.setBounds(0, 0, 200, 300);
panel2.setLayout(gbl);
emails.add(panel2);
gbc.gridx = 0;
gbc.gridy = 17;
gbc.gridwidth = 20;
gbc.gridheight = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(5,70,5,70);
gbl.setConstraints(done, gbc);
panel2.add(done);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 20;
gbc.gridheight = 17;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(10,10,0,10);
gbl.setConstraints(emailA, gbc);
panel2.add(emailA);
emails.add(panel2);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 10;
gbc.gridheight = 19;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5,5,5,5);
gbl.setConstraints(Targets, gbc);
panel.add(lsp);
gbc.gridx = 10;
gbc.gridy = 11;
gbc.gridwidth = 5;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25,5,25,5);
gbl.setConstraints(Start, gbc);
panel.add(Start);
gbc.gridx = 15;
gbc.gridy = 11;
gbc.gridwidth = 5;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25,5,25,5);
gbl.setConstraints(Stop, gbc);
panel.add(Stop);
gbc.gridx = 10;
gbc.gridy = 4;
gbc.gridwidth = 10;
gbc.gridheight = 7;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(35,5,40,5);
gbl.setConstraints(Configure, gbc);
panel.add(Configure);
gbc.gridx = 10;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25,5,20,5);
gbl.setConstraints(AddRecipiants, gbc);
panel.add(AddRecipiants);
Progress.setMaximum(100);
Progress.setString("0%");
Progress.setValue(0);
gbc.gridx = 10;
gbc.gridy = 16;
gbc.gridwidth = 10;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25,5,15,5);
gbl.setConstraints(Progress, gbc);
panel.add(Progress);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5,45,0,45);
gbl.setConstraints(RLable, gbc);
panel.add(RLable);
frame.add(panel);
frame.setVisible(true);
}
The JScrollPane preferred size isn't typically very large, and is generally determine from the Scrollable interface (and other factors)
You can affect the JList's default size using the following methods...
JList#setVisibleRowCount
JList#setPrototypeCellValue
The first will effect the number of rows that are visible in the view port and the second will effect the height and width of that row.
You are also NOT passing in constraints to the layout manager for the scroll pane (you are trying to use the JList) which isn't going to work...
You should be doing this...
panel.add(lsp, gbc);
And I would greatly discourage this...
gbl.setConstraints(Targets, gbc);
(I should add, I would discourage the above generally, and recommend using add(Component, Object) - But that's me)
Updated
My suggestions, vs your default code...
public class BadLayout10 {
public static void main(String[] args) {
new BadLayout10();
}
public BadLayout10() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
new StartGui();
}
});
}
public class StartGui {
private JFrame frame = new JFrame("");
private JPanel panel = new JPanel();
private JMenuBar menu = new JMenuBar();
private DefaultListModel<String> listModel = new DefaultListModel<String>();
private JList<String> Targets = new JList<String>(listModel);
private JButton Start = new JButton("Start");
private JButton Stop = new JButton("Stop");
private JButton Configure = new JButton("Configure");
private JButton AddRecipiants = new JButton("Add Recipiants");
private JProgressBar Progress = new JProgressBar();
private JLabel RLable = new JLabel("Recipiants:");
private JScrollPane lsp;
private GridBagLayout gbl = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private JFrame emails = new JFrame();
private JPanel panel2 = new JPanel();
private JTextArea emailA = new JTextArea("type email here");
private JButton done = new JButton("OK");
public StartGui() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menu);
frame.setResizable(false);
frame.setBounds(0, 0, 500, 400);
panel.setLayout(gbl);
listModel.addElement("TestEmailAddress#fake.com");
Targets.setVisibleRowCount(10);
Targets.setPrototypeCellValue("This is a really long test string");
lsp = new JScrollPane(Targets);
emails.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
emails.setResizable(false);
emails.setBounds(0, 0, 200, 300);
panel2.setLayout(gbl);
emails.add(panel2);
gbc.gridx = 0;
gbc.gridy = 17;
gbc.gridwidth = 20;
gbc.gridheight = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(5, 70, 5, 70);
gbl.setConstraints(done, gbc);
panel2.add(done);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 20;
gbc.gridheight = 17;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(10, 10, 0, 10);
gbl.setConstraints(emailA, gbc);
panel2.add(emailA);
emails.add(panel2);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 10;
gbc.gridheight = 19;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
// gbl.setConstraints(Targets, gbc);
panel.add(lsp, gbc);
gbc.gridx = 10;
gbc.gridy = 11;
gbc.gridwidth = 5;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25, 5, 25, 5);
gbl.setConstraints(Start, gbc);
panel.add(Start);
gbc.gridx = 15;
gbc.gridy = 11;
gbc.gridwidth = 5;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25, 5, 25, 5);
gbl.setConstraints(Stop, gbc);
panel.add(Stop);
gbc.gridx = 10;
gbc.gridy = 4;
gbc.gridwidth = 10;
gbc.gridheight = 7;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(35, 5, 40, 5);
gbl.setConstraints(Configure, gbc);
panel.add(Configure);
gbc.gridx = 10;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25, 5, 20, 5);
gbl.setConstraints(AddRecipiants, gbc);
panel.add(AddRecipiants);
Progress.setMaximum(100);
Progress.setString("0%");
Progress.setValue(0);
gbc.gridx = 10;
gbc.gridy = 16;
gbc.gridwidth = 10;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25, 5, 15, 5);
gbl.setConstraints(Progress, gbc);
panel.add(Progress);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 45, 0, 45);
gbl.setConstraints(RLable, gbc);
panel.add(RLable);
frame.add(panel);
frame.setVisible(true);
}
}
}