I'm trying to create (hand-coded) a GUI similair to the GUI shown below, however, only an empty frame shows.
Mock GUI:
I've used various layouts and SWING/AWT components to create the GUI and 4 JPanels which contain:
mainPanel: Contains all the panels in it.
listPanel: Contains the JTables, JLabels and the two JButtons
infoPanel: Contains the JLabels, JCheckBox and JTextBoxes.
addPanel: Contains the JLists and JButton
This is what I coded so far:
import java.awt.*;
import javax.swing.*;
import javax.swing.JTable;
public class GUI extends JFrame {
public void buildGui() {
JFrame frame = new JFrame("Hotel TV Scheduler");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(3,3));
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(2,2));
JPanel addPanel = new JPanel();
addPanel.setLayout(new FlowLayout());
mainPanel.add(listPanel, BorderLayout.LINE_START);
mainPanel.add(infoPanel, BorderLayout.LINE_END);
mainPanel.add(addPanel, BorderLayout.PAGE_END);
JTable chOneTable = new JTable();
JTable chTwoTable = new JTable();
JTable listTable = new JTable();
JLabel ch1Label = new JLabel("Channel 1");
JLabel ch2Label = new JLabel("Channel 2");
JLabel listLabel = new JLabel("List");
JButton rmvChOneButton = new JButton("Remove Channel");
JButton rmvChTwoButton = new JButton("Remove Channel");
listPanel.add(ch1Label);
listPanel.add(ch2Label);
listPanel.add(listLabel);
listPanel.add(chOneTable);
listPanel.add(chTwoTable);
listPanel.add(listTable);
listPanel.add(rmvChOneButton);
listPanel.add(rmvChTwoButton);
JLabel titleLabel = new JLabel("Title");
JLabel genreLabel = new JLabel("Genre");
JLabel durationLabel = new JLabel("Duration");
JLabel actorLabel = new JLabel("Actor");
JLabel directorLabel = new JLabel("Director");
JLabel rentableLabel = new JLabel("Rentable");
JLabel synLabel = new JLabel("Synopsis");
JTextField txtTitle = new JTextField();
JTextField txtGenre = new JTextField();
JTextField txtDuration = new JTextField();
JTextField txtActor = new JTextField();
JTextField txtDirector = new JTextField();
JTextField txtSynopsis = new JTextField();
JCheckBox rentCB = new JCheckBox();
infoPanel.add(titleLabel);
infoPanel.add(txtTitle);
infoPanel.add(genreLabel);
infoPanel.add(txtGenre);
infoPanel.add(durationLabel);
infoPanel.add(txtDuration);
infoPanel.add(actorLabel);
infoPanel.add(txtActor);
infoPanel.add(directorLabel);
infoPanel.add(txtDirector);
infoPanel.add(rentableLabel);
infoPanel.add(rentCB);
infoPanel.add(synLabel);
infoPanel.add(txtSynopsis);
JButton btnAddProg = new JButton("Add Program");
JList channelList = new JList();
JList timeList = new JList();
addPanel.add(btnAddProg);
addPanel.add(channelList);
addPanel.add(timeList);
frame.setVisible(true);
}
}
Anyone can tell me why only an empty frame is showing up ?
Thanks and Regards,
Brian
Yep just checked, you'll see something if you actually add the mainPanel to the frame, (looks nothing like the mock though!)
frame.setContentPane(mainPanel);
frame.pack();
You've not added mainPanel to the frame
Related
I'm trying to learn swing and work through the task we have been given. I can't see why the code doesn't display the JButtons in the SOUTH section as there is no issue when displaying the textfields, combobox and labels in the CENTER section.
I used the same format to add components to my CENTER section as I did in the SOUTH and EAST but only the center displays anything.
public class ProductListGUI{
JMenu menu;
JMenuItem about,importData,inventory,export;
ProductListGUI(){
JFrame f = new JFrame("Assignment 2");
JPanel p1 = new JPanel();
JList<String> list = new JList<>();
list.setBounds(600,0,200,600);
JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel p3 = new JPanel();
p1.setLayout(null);
p1.setBounds(0,0,600,500);
p1.setBackground(new Color(230,230,230));
scrollPane.setLayout(null);
scrollPane.setBounds(600,0,200,500);
p3.setLayout(null);
p3.setBounds(0,500,800,100);
p3.setBackground(new Color(230,230,230));
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3;
l1=new JLabel("ProductID");
l1.setBounds(10,100,200,30);
t1=new JTextField();
t1.setBounds(100,100,200,30);
l2=new JLabel("Name");
l2.setBounds(10,150,200,30);
t2=new JTextField();
t2.setBounds(100,150,200,30);
l3=new JLabel("Quantity");
l3.setBounds(10,250,200,30);
t3=new JTextField();
t3.setBounds(100,250,200,30);
p1.setBorder(BorderFactory.createTitledBorder("Product Details"));
JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
checkBox.setBounds(10,300,250,50);
l4 = new JLabel("Item Type");
l4.setBounds(10,200,200,30);
String[] itemType = {"Select type","Homeware","Hobby","Garden"};
JComboBox dropdown = new JComboBox(itemType);
dropdown.setBounds(100,200,120,20);
p1.add(t1);p1.add(l1);p1.add(t2);p1.add(l2);p1.add(t3);p1.add(l3);p1.add(l4);p1.add(dropdown);p1.add(checkBox);
JButton b1 = new JButton("New Item");
b1.setBounds(200,550,80,20);
JButton b2 = new JButton("Save");
b2.setBounds(300,550,80,20);
JButton b3 = new JButton("Delete Selected");
b3.setBounds(600,550,80,20);
b3.setEnabled(false);
p3.add(b1);p3.add(b2);p3.add(b3);
JMenuBar mb = new JMenuBar();
menu = new JMenu("Actions");
about = new JMenuItem("About");
importData = new JMenuItem("Import Data");
inventory = new JMenuItem("Inventory");
export = new JMenuItem("Export to CSV");
menu.add(about);menu.add(importData);menu.add(inventory);menu.add(export);
mb.add(menu);
f.getContentPane().add(p1,BorderLayout.CENTER);
f.getContentPane().add(scrollPane,BorderLayout.EAST);
f.getContentPane().add(p3,BorderLayout.SOUTH);
f.setJMenuBar(mb);
f.setSize(800,600);
f.setLayout(new BorderLayout());
f.setVisible(true);
}
These code changes should help at least a little bit.
Things that had to change:
Each JPanel should have a layout. Setting the layout to null (as per TG's comment) is not good.
The setBounds() methods for the buttons were removed.
The f.setLayout(new BorderLayout()); was moved to the top of the code where Components were being added to the JFrame before the layout was set.
import javax.swing.*;
import java.awt.*;
public class ProductListGUI {
JMenu menu;
JMenuItem about,importData,inventory,export;
ProductListGUI(){
JFrame f = new JFrame("Assignment 2");
JPanel panel1 = new JPanel();
JList<String> list = new JList<>();
list.setBounds(600,0,200,600);
JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel panel3 = new JPanel();
panel1.setBounds(0,0,600,500);
panel1.setBackground(new Color(230,230,230));
scrollPane.setBounds(600,0,200,500);
panel3.setBounds(0,0,800,100);
panel3.setBackground(new Color(230,230,230));
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3;
l1=new JLabel("ProductID");
l1.setBounds(10,100,200,30);
t1=new JTextField();
t1.setBounds(100,100,200,30);
l2=new JLabel("Name");
l2.setBounds(10,150,200,30);
t2=new JTextField();
t2.setBounds(100,150,200,30);
l3=new JLabel("Quantity");
l3.setBounds(10,250,200,30);
t3=new JTextField();
t3.setBounds(100,250,200,30);
panel1.setBorder(BorderFactory.createTitledBorder("Product Details"));
JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
checkBox.setBounds(10,300,250,50);
l4 = new JLabel("Item Type");
l4.setBounds(10,200,200,30);
String[] itemType = {"Select type","Homeware","Hobby","Garden"};
JComboBox dropdown = new JComboBox(itemType);
dropdown.setBounds(100,200,120,20);
panel1.add(t1);
panel1.add(l1);
panel1.add(t2);
panel1.add(l2);
panel1.add(t3);
panel1.add(l3);
panel1.add(l4);
panel1.add(dropdown);
panel1.add(checkBox);
JButton b1 = new JButton("New Item");
JButton b2 = new JButton("Save");
JButton b3 = new JButton("Delete Selected");
b3.setEnabled(false);
panel3.add(b1);
panel3.add(b2);
panel3.add(b3);
JMenuBar mb = new JMenuBar();
menu = new JMenu("Actions");
about = new JMenuItem("About");
importData = new JMenuItem("Import Data");
inventory = new JMenuItem("Inventory");
export = new JMenuItem("Export to CSV");
menu.add(about);
menu.add(importData);
menu.add(inventory);
menu.add(export);
mb.add(menu);
f.setLayout(new BorderLayout());
f.getContentPane().add(panel1,BorderLayout.CENTER);
f.getContentPane().add(scrollPane,BorderLayout.EAST);
f.getContentPane().add(panel3,BorderLayout.SOUTH);
f.setJMenuBar(mb);
f.setSize(800,600);
f.setVisible(true);
}
public static void main(String[] args) {
ProductListGUI gui = new ProductListGUI();
}
}
Here is a very simple BorderLayout example that might help in the future, or not.
import javax.swing.*;
import java.awt.*;
public class TestGui {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
frame.setLayout(new BorderLayout());
frame.setSize(800,600);
frame.getContentPane().add(createJPanel("CENTER", Color.RED), BorderLayout.CENTER);
frame.getContentPane().add(createJPanel("NORTH", Color.CYAN), BorderLayout.NORTH);
frame.getContentPane().add(createJPanel("EAST", Color.LIGHT_GRAY), BorderLayout.EAST);
frame.getContentPane().add(createJPanel("SOUTH", Color.GREEN), BorderLayout.SOUTH);
frame.getContentPane().add(createJPanel("WEST", Color.YELLOW), BorderLayout.WEST);
frame.setVisible(true);
}
private static JPanel createJPanel(String title, Color color) {
JPanel jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());
jPanel.add(new JLabel(title), BorderLayout.CENTER);
jPanel.setBackground(color);
return jPanel;
}
}
I am setting up a UI for a blackjack helper program and while I know its not the most beautiful way to do things, it makes sense.
The layering for what seems to be the upper layers is not working properly. Any suggestions?
The left should have four layers, as should the middle and the right side should have two layers between the keypad and the enter buttons. Image is attached below.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
// main method, runs the program
class BlackjackHelper
{
public static void main(String\[\] args)
{
Frame21 game = new Frame21();
//pop window with rules of game
}
}
// JFrame Construction
class Frame21 extends JFrame
{
// create needed components of program
JLabel questionDisplay = new JLabel("What is your first card?");
JLabel actionDisplay = new JLabel("Enter your first card");
JLabel dealerCardText = new JLabel("Dealer's Card:");
JLabel dealerCardDisplay = new JLabel("N/A");
JLabel handOneText = new JLabel("Hand One:");
JLabel handOneDisplay = new JLabel("N/A");
JLabel handTwoText = new JLabel("Hand Two:");
JLabel handTwoDisplay = new JLabel("N/A");
JLabel statsText = new JLabel("Win %:");
JLabel statsDisplay = new JLabel("N/A");
JButton aceButton = new JButton("A");
JButton twoButton = new JButton("2");
JButton threeButton = new JButton("3");
JButton fourButton = new JButton("4");
JButton fiveButton = new JButton("5");
JButton sixButton = new JButton("6");
JButton sevenButton = new JButton("7");
JButton eightButton = new JButton("8");
JButton nineButton = new JButton("9");
JButton tenButton = new JButton("10");
JButton faceButton = new JButton("F");
JButton clearButton = new JButton("C");
JButton standButton = new JButton("Stand");
JButton hitButton = new JButton("Hit");
JButton doubleButton = new JButton("Double");
JButton splitButton = new JButton("Split");
JButton winButton = new JButton("Win");
JButton loseButton = new JButton("Lose");
JButton resetButton = new JButton("Reset All");
JButton enterButton = new JButton("Enter");
public Frame21()
{
// JFrame - the main area of the program
JFrame frame = new JFrame("Blackjack Helper");
// JPanel right - the rightside of the program
JPanel rightSide = new JPanel();
JPanel rightNorthSide = new JPanel();
rightNorthSide.setLayout(new GridLayout(3,4));
rightNorthSide.add(aceButton);
rightNorthSide.add(twoButton);
rightNorthSide.add(threeButton);
rightNorthSide.add(fourButton);
rightNorthSide.add(fiveButton);
rightNorthSide.add(sixButton);
rightNorthSide.add(sevenButton);
rightNorthSide.add(eightButton);
rightNorthSide.add(nineButton);
rightNorthSide.add(tenButton);
rightNorthSide.add(faceButton);
rightNorthSide.add(clearButton);
JPanel rightSouthSide = new JPanel();
rightSouthSide.add(resetButton, BorderLayout.WEST);
rightSouthSide.add(enterButton, BorderLayout.EAST);
rightSide.add(rightNorthSide, BorderLayout.NORTH);
rightSide.add(rightSouthSide, BorderLayout.SOUTH);
frame.add(rightSide, BorderLayout.EAST);
// JPanel Center - the center of the program
JPanel center = new JPanel();
JPanel centerNorth = new JPanel();
centerNorth.add(questionDisplay, BorderLayout.NORTH);
centerNorth.add(actionDisplay, BorderLayout.SOUTH);
JPanel centerSouth = new JPanel();
JPanel centerSouthNorth = new JPanel();
centerSouthNorth.add(dealerCardText, BorderLayout.WEST);
centerSouthNorth.add(dealerCardDisplay, BorderLayout.EAST);
JPanel centerSouthSouth = new JPanel();
JPanel centerSouthSouthWest = new JPanel();
centerSouthSouthWest.add(handOneText, BorderLayout.NORTH);
centerSouthSouthWest.add(handOneDisplay, BorderLayout.SOUTH);
JPanel centerSouthSouthEast = new JPanel();
centerSouthSouthEast.add(handTwoText, BorderLayout.NORTH);
centerSouthSouthEast.add(handTwoDisplay, BorderLayout.SOUTH);
centerSouthSouth.add(centerSouthSouthWest, BorderLayout.WEST);
centerSouthSouth.add(centerSouthSouthEast, BorderLayout.EAST);
centerSouth.add(centerSouthNorth, BorderLayout.NORTH);
centerSouth.add(centerSouthSouth, BorderLayout.SOUTH);
center.add(centerNorth, BorderLayout.NORTH);
center.add(centerSouth, BorderLayout.SOUTH);
frame.add(center, BorderLayout.CENTER);
// JPanel left - the center of the program
JPanel left = new JPanel();
JPanel leftNorth = new JPanel();
JPanel leftNorthNorth = new JPanel();
JPanel leftNorthSouth = new JPanel();
JPanel leftSouth = new JPanel();
JPanel leftSouthNorth = new JPanel();
JPanel leftSouthSouth = new JPanel();
leftNorthNorth.add(standButton, BorderLayout.WEST);
leftNorthNorth.add(hitButton, BorderLayout.EAST);
leftNorthSouth.add(doubleButton, BorderLayout.WEST);
leftNorthSouth.add(splitButton, BorderLayout.EAST);
leftNorth.add(leftNorthNorth, BorderLayout.NORTH);
leftNorth.add(leftNorthSouth, BorderLayout.SOUTH);
leftSouthNorth.add(statsText, BorderLayout.WEST);
leftSouthNorth.add(statsDisplay, BorderLayout.EAST);
leftSouthSouth.add(winButton, BorderLayout.WEST);
leftSouthSouth.add(loseButton, BorderLayout.EAST);
leftSouth.add(leftSouthNorth, BorderLayout.NORTH);
leftSouth.add(leftSouthSouth, BorderLayout.SOUTH);
left.add(leftNorth, BorderLayout.NORTH);
left.add(leftSouth, BorderLayout.SOUTH);
frame.add(left, BorderLayout.WEST);
frame.setSize(1600, 200);
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
The layering for what seems to be the upper layers is not working properly. Any suggestions? The left should have four layers.
A BorderLayout does not "layer".
Only a single component can be added to each of the 5 areas of the BorderLayout.
So, yes, you can create a JPanel and add multiple components to that panel, and then add the panel to an area of the BorderLayout.
JPanel rightSouthSide = new JPanel();
rightSouthSide.add(resetButton, BorderLayout.WEST);
rightSouthSide.add(enterButton, BorderLayout.EAST);
However, the above code is incorrect. The default layout manager of a JPanel is the FlowLayout. So specifying BorderLayout constraints does nothing (and is very confusing).
If by "layers" you mean "rows", then you need to use a panel with a different layout manage. Maybe you can use a BoxLayout to add rows of panels.
Overall, your approach to creating panels with different components is valid, the problem is you also need to use the appropriate layout manager for each of your child panels.
Read the Swing tutorial on Layout Managers for more information about how each of the layout managers work.
This is my first post so please forgive me if I am not following the rules correctly.
I am trying to do something relatively simple in Java. I want to make a JTextArea scrollable. I am aware this has been asked before (Java :Add scroll into text area). However, when I follow this example by adding JTextArea to JScrollPane my JTextArea does not become scrollable. What is it that I am missing?
Here is my code:
import ...;
public class MyControlPanel extends JPanel {
//Declare variables
private JComboBox accountsBox;
private String[] accType = {"Current Account", "Savings Account"};
private JLabel selAccType, initDeposit, logLabel, simLabel;
private JTextField depositText;
private JTextArea log;
private JScrollPane scroll;
private JButton createAccount, start, stop;
private JPanel panel1, panel2, panel3, panel4, panel5, panel6;
private Timer timer;
private MyAccount theAccount;
private Random randNum1, randNum2;
private DecimalFormat df;
//Constructor
public MyControlPanel() {
//Create instances:
selAccType = new JLabel("Please select account type: "); //JLabel
initDeposit = new JLabel("Input initial deposit: ");
logLabel = new JLabel("Log:");
simLabel = new JLabel();
accountsBox = new JComboBox(accType); //JComboBox
depositText = new JTextField("0"); // JTextField
log = new JTextArea(); //JTextArea
scroll = new JScrollPane(log); //JScrollPane
createAccount = new JButton("Create Account"); //JButton
start = new JButton("Start");
stop = new JButton("Stop");
panel1 = new JPanel(); //JPanel
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
panel6 = new JPanel();
timer = new Timer(); //Timer
df = new DecimalFormat("#.00");
//Add ActionListeners
createAccount.addActionListener(new ActionListener() {...});
start.addActionListener(new ActionListener() {...});
stop.addActionListener(new ActionListener() {...});
//Set JTextField size
depositText.setColumns(5);
//Set JTextArea size
log.setPreferredSize(new Dimension(780, 150));
//Set panel size
panel1.setPreferredSize(new Dimension(500, 500));
panel2.setPreferredSize(new Dimension(800, 50));
panel3.setPreferredSize(new Dimension(500, 50));
panel4.setPreferredSize(new Dimension(500, 50));
panel5.setPreferredSize(new Dimension(800, 25));
panel6.setPreferredSize(new Dimension(800, 200));
//Set layout in panel5 to align left
panel6.setLayout(new FlowLayout(FlowLayout.LEFT));
//Add components to each panel
addPanels();
//Place objects in the framed window
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.add(panel4);
this.add(panel5);
this.add(panel6);
}
public void addPanels() {...}
public void removePanels() {...}
}
You need to change:
log.setPreferredSize(new Dimension(780, 150));
to:
scroll.setPreferredSize(new Dimension(780, 150));
I am having a problem with BorderLayout, that was set to the green JPanel side. It does not display elements on the EAST in a row order. Do I have to combine this with GridBagLayout ? Could someone advice me how should I tackle this problem?
Basically the problem is of displaying objects inside green area below when I am using
Current layout:
My aim is to achieve this layout:
public class GUILayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jbO = new JButton("CSIS0396");
JButton jbl = new JButton("Final");
JButton jb2 = new JButton("2010");
JButton jb3 = new JButton("Exam");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JButton object_btn = new JButton("Object");
JButton oriented_btn = new JButton("Oriented");
JButton programming_btn = new JButton("Programming");
JButton and_btn = new JButton("and");
JButton java_btn = new JButton("Java");
BorderLayout layout = new BorderLayout();
panel.setLayout(layout);
panel2.setLayout(layout);
panel.add(BorderLayout.CENTER,object_btn);
panel.add(BorderLayout.WEST,oriented_btn);
panel.add(BorderLayout.WEST,programming_btn);
panel.add(BorderLayout.WEST,and_btn);
panel.add(BorderLayout.WEST,java_btn);
panel2.add(BorderLayout.NORTH, jbO);
panel2.add(BorderLayout.SOUTH, jb2);
panel2.add(BorderLayout.WEST, jbl);
panel2.add(BorderLayout.EAST, jb3);
panel.setBackground(Color.GREEN);
panel2.setBackground(Color.RED);
jf.getContentPane().add(panel);
jf.getContentPane().add(panel2, BorderLayout.EAST);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 300);
jf.setVisible(true);
}
}
don't share same layout for multiple component and use box layout for left panel to positioning buttons
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUILayout {
public static void main(String[] args) {
JFrame jf = new JFrame();
JButton jbO = new JButton("CSIS0396");
JButton jbl = new JButton("Final");
JButton jb2 = new JButton("2010");
JButton jb3 = new JButton("Exam");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JButton object_btn = new JButton("Object");
JButton oriented_btn = new JButton("Oriented");
JButton programming_btn = new JButton("Programming");
JButton and_btn = new JButton("and");
JButton java_btn = new JButton("Java");
BorderLayout layout = new BorderLayout();
panel2.setLayout(layout);
panel.setLayout( new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(object_btn);
panel.add(oriented_btn);
panel.add(programming_btn);
panel.add(and_btn);
panel.add(java_btn);
panel2.add(BorderLayout.NORTH, jbO);
panel2.add(BorderLayout.SOUTH, jb2);
panel2.add(BorderLayout.WEST, jbl);
panel2.add(BorderLayout.EAST, jb3);
panel.setBackground(Color.GREEN);
panel2.setBackground(Color.RED);
jf.setLayout(new BorderLayout());
jf.getContentPane().add(panel ,BorderLayout.WEST);
jf.getContentPane().add(panel2, BorderLayout.EAST);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 300);
jf.setVisible(true);
}
}
The first problem is that you're having your panels share a layout. You must use a new BorderLayout for each of them.
The second problem is that a BorderLayout can only have one component in each constraint position. When you try to add multiple components to the WEST position, each one replaces the one that was previously in that position:
panel.add(BorderLayout.WEST,oriented_btn);
// Implicitly removes oriented_btn from panel
panel.add(BorderLayout.WEST,programming_btn);
// Implicitly removes programming_btn from panel
panel.add(BorderLayout.WEST,and_btn);
// Implicitly removes and_btn from panel
panel.add(BorderLayout.WEST,java_btn);
The solution is to put them in their own container, such as a Box or a JPanel with a GridLayout:
Box box = Box.createVerticalBox();
// Or:
//JComponent box = new JPanel(new GridLayout(0, 1));
box.add(oriented_btn);
box.add(programming_btn);
box.add(and_btn);
box.add(java_btn);
panel.add(BorderLayout.WEST, box);
I'm trying to set the size of a gridlayout jpanel. Here is the code:
JFrame myFrame = new JFrame();
myFrame.setLayout(new FlowLayout());
myFrame.setLocation(400, 100);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel jlMins = new JLabel("Number of minutes for tutoring session (should be a positive decimal number): 0.0");
JLabel jlEarnings = new JLabel("Earnings in dollars and cents received (should be positive decimal number): 0.0");
jtfMins = new JTextField(20);
jtfEarnings = new JTextField(20);
JPanel jpMins = new JPanel(new BorderLayout());
JPanel jpEarnings = new JPanel(new BorderLayout());
jpMins.setPreferredSize(new Dimension(300,50));
jpEarnings.setPreferredSize(new Dimension(300,50));
jpMins.add(jlMins,BorderLayout.NORTH);
jpMins.add(jtfMins,BorderLayout.CENTER);
jpEarnings.add(jlEarnings,BorderLayout.NORTH);
jpEarnings.add(jtfEarnings,BorderLayout.CENTER);
JButton jbQuit = new JButton("Quit");
JButton jbEnter = new JButton("Enter");
JButton jbReport = new JButton("Run Report");
jbQuit.setActionCommand("quit");
jbEnter.setActionCommand("enter");
jbReport.setActionCommand("report");
jbQuit.addActionListener(this);
jbEnter.addActionListener(this);
jbReport.addActionListener(this);
JPanel jpButtons = new JPanel(new GridLayout(4,1,0,20));
jpButtons.setSize(new Dimension(50,150));
jpButtons.add(jbEnter);
jpButtons.add(jbReport);
jpButtons.add(jbQuit);
JPanel jpNorth = new JPanel(new BorderLayout());
jpNorth.add(jpMins,BorderLayout.NORTH);
jpNorth.add(jpEarnings,BorderLayout.CENTER);
jpNorth.add(jpButtons,BorderLayout.SOUTH);
jtaReports = new JTextArea();
jtaReports.setColumns(40);
jtaReports.setRows(10);
jtaReports.setLineWrap(true);
JScrollPane jspReports = new JScrollPane(jtaReports);
jspReports.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel jpSouth = new JPanel();
jpSouth.setPreferredSize(new Dimension(350,200));
jpSouth.add(jspReports);
JPanel jpMain = new JPanel(new BorderLayout());
jpMain.add(jpNorth,BorderLayout.NORTH);
jpMain.add(jpSouth,BorderLayout.SOUTH);
jpMain.setPreferredSize(new Dimension(500,500));
myFrame.setContentPane(jpMain);
myFrame.pack();
myFrame.setVisible(true);
The panel name is jpButtons. Of the above code I'm talking mainly about this section:
JButton jbQuit = new JButton("Quit");
JButton jbEnter = new JButton("Enter");
JButton jbReport = new JButton("Run Report");
jbQuit.setActionCommand("quit");
jbEnter.setActionCommand("enter");
jbReport.setActionCommand("report");
jbQuit.addActionListener(this);
jbEnter.addActionListener(this);
jbReport.addActionListener(this);
JPanel jpButtons = new JPanel(new GridLayout(4,1,0,20));
jpButtons.setSize(new Dimension(50,150));
jpButtons.add(jbEnter);
jpButtons.add(jbReport);
jpButtons.add(jbQuit);
How exactly does setSize, and setPreferredSize work or how to get them to work properly on jpanel, components, etc.
scaling and positioning is handled by the layout manager; let it do its job.