Java GUI JScrollBar How to set the length? - java

I have the following GUI codded up but I would like to increase the length of the scroll bar on the right side.
Any Idea How to do this?
// test class that implements the gui stuff.
public class testing
{
//variables
private JFrame f = new JFrame("GUI TEST");
private JPanel p = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3 = new JPanel();
private JPanel p4 = new JPanel();
private JPanel p5 = new JPanel();
private JPanel p6 = new JPanel();
private JPanel p7 = new JPanel();
private JPanel p8 = new JPanel();
private JPanel p9 = new JPanel();
private JPanel p10 = new JPanel();
private JPanel p11 = new JPanel();
private JButton b1 = new JButton("Button");
private JTextField tf1 = new JTextField(" ");
private JTextField tf2 = new JTextField(" ");
private JTextField tf3 = new JTextField(" ");
private JTextArea ta1 = new JTextArea(10,45);
private JLabel label1 = new JLabel("Label 1");
private JLabel label2 = new JLabel("Label 2");
private JLabel label3 = new JLabel("Label 3");
private JLabel label4 = new JLabel("Label 4");
private JScrollBar sb1 = new JScrollBar();
//class constructor
public testing()
{
gui();
}
public void gui()
{
//change length of scroll bar
f.setVisible(true);
f.setSize(600,300);
p.add(label1);
p.add(tf1);
p2.add(label2);
p2.add(tf2);
p3.add(label3);
p3.add(tf3);
p4.add(sb1);
p4.add(label4);
p5.add(ta1);
p6.add(b1);
p4.setBackground(Color.GRAY);
p9.setBackground(Color.GRAY);
p10.setBackground(Color.GRAY);
p11.setBackground(Color.GRAY);
p9.add(p);
p9.add(p2);
p9.add(p3);
p10.add(p5);
p11.add(p6);
//adds panels to frames
f.add(p4, BorderLayout.EAST);
f.add(p9, BorderLayout.NORTH);
f.add(p10, BorderLayout.CENTER);
f.add(p11, BorderLayout.PAGE_END);
}
public static void main(String[] args)
{
new testing();
}

Ordinarilly, you'd simply add your JTextArea to a JScrollPane, which handles the resizing behavior for you.
f.add(new JScrollPane(ta1), BorderLayout.CENTER);
For demonstration purposes, you can override the getPreferredSize() method of the JScrollBar to see the effect.
private JScrollBar sb1 = new JScrollBar(){
#Override
public Dimension getPreferredSize() {
return new Dimension(
super.getPreferredSize().width, ta1.getPreferredSize().height);
}
};
In addition,
Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
Use the appropriate constructor to establish the desired initial size of text components.
Use an appropriate layout to get the desired resizing behavior.
As tested:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Testing {
//variables
private JFrame f = new JFrame("GUI TEST");
private JPanel p = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3 = new JPanel();
private JPanel p4 = new JPanel();
private JPanel p5 = new JPanel();
private JPanel p6 = new JPanel();
private JPanel p9 = new JPanel();
private JPanel p10 = new JPanel();
private JPanel p11 = new JPanel();
private JButton b1 = new JButton("Button");
private JTextField tf1 = new JTextField(12);
private JTextField tf2 = new JTextField(12);
private JTextField tf3 = new JTextField(12);
private JTextArea ta1 = new JTextArea(10, 45);
private JLabel label1 = new JLabel("Label 1");
private JLabel label2 = new JLabel("Label 2");
private JLabel label3 = new JLabel("Label 3");
private JLabel label4 = new JLabel("Label 4");
private JScrollBar sb1 = new JScrollBar(){
#Override
public Dimension getPreferredSize() {
return new Dimension(super.getPreferredSize().width, ta1.getPreferredSize().height);
}
};
//class constructor
public Testing() {
gui();
}
public void gui() {
p.add(label1);
p.add(tf1);
p2.add(label2);
p2.add(tf2);
p3.add(label3);
p3.add(tf3);
p4.add(sb1);
p4.add(label4);
p5.add(ta1);
p6.add(b1);
p4.setBackground(Color.GRAY);
p9.setBackground(Color.GRAY);
p10.setBackground(Color.GRAY);
p11.setBackground(Color.GRAY);
p9.add(p);
p9.add(p2);
p9.add(p3);
p10.add(p5);
p11.add(p6);
//adds panels to frames
f.add(p4, BorderLayout.EAST);
f.add(p9, BorderLayout.NORTH);
f.add(p10, BorderLayout.CENTER);
f.add(p11, BorderLayout.PAGE_END);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Testing();
});
}
}

Related

How can I switch JFrames by clicking a JButton in Java?

I am writing a program and am unable to figure this out but I have a JButton called nextDay and I need it set up so once I click it, it switches to the JFrame day2 as the program starts out on day1. Any help is appreciated.
Here is my code. I have a separate Main method which I wont include as it shouldn't need to be changed
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SoldierSimTest extends JFrame {
private final JButton decision1;
private final JButton decision2;
private final JButton decision3;
private final JButton decision4;
private final JTextField situation;
private JFrame day1;
private JFrame day2;
private JFrame day3;
private JFrame day4;
private JFrame day5;
private JFrame day6;
private JFrame day7;
private final JButton nextDay;
private final JButton exitGame;
private final JButton newGame;
public SoldierSimTest() {
decision1 = new JButton("Storm it");
decision2 = new JButton("Sneak around the flank");
decision3 = new JButton("Sneak up and grenade spam 'em");
decision4 = new JButton("Just dont");
situation = new JTextField("You and your squad are ordered to take
an enemy fort. How will you do so?");
situation.setEditable(false);
nextDay = new JButton("Next Day");
exitGame = new JButton("Exit Game");
newGame = new JButton("New Game");
JPanel decisionsPanel = new JPanel();
decisionsPanel.setLayout(new GridLayout(2, 2));
decisionsPanel.add(decision1);
decisionsPanel.add(decision2);
decisionsPanel.add(decision3);
decisionsPanel.add(decision4);
JPanel optionsPanel = new JPanel();
optionsPanel.setLayout(new GridLayout(1, 3));
optionsPanel.add(newGame);
optionsPanel.add(exitGame);
optionsPanel.add(nextDay);
JPanel situationsPanel = new JPanel();
optionsPanel.setLayout(new GridLayout(1, 1));
situationsPanel.add(situation);
Container contentPane = getContentPane();
contentPane.add(decisionsPanel, "South");
contentPane.add(optionsPanel, "North");
contentPane.add(situationsPanel, "Center");
}
}
You can use card layout to switch panels. I used Jpanels as it seems to be the best option to use. For this example I used 2 panels.
public class SoldierSimTest extends JFrame
{
private final JButton decision1;
private final JButton decision2;
private final JButton decision3;
private final JButton decision4;
private final JTextField situation;
private JPanel day1Panel = new JPanel();
private JPanel day2Panel = new JPanel();
private final JButton nextDay;
private final JButton exitGame;
private final JButton newGame;
final static String DAY1 = "Day1";
final static String DAY2 = "Day2";
public SoldierSimTest()
{
decision1 = new JButton("Storm it");
decision2 = new JButton("Sneak around the flank");
decision3 = new JButton("Sneak up and grenade spam 'em");
decision4 = new JButton("Just dont");
situation = new JTextField("You and your squad are ordered to take an enemy fort. How will you do so?");
situation.setEditable(false);
JPanel decisionsPanel = new JPanel();
decisionsPanel.setLayout(new GridLayout(2, 2));
decisionsPanel.add(decision1);
decisionsPanel.add(decision2);
decisionsPanel.add(decision3);
decisionsPanel.add(decision4);
JPanel situationsPanel = new JPanel();
situationsPanel.add(situation);
day1Panel.setLayout(new GridLayout(2, 1));
day1Panel.add(situationsPanel);
day1Panel.add(decisionsPanel);
JPanel cards = new JPanel(new CardLayout());
cards.add(day1Panel, DAY1);
cards.add(day2Panel, DAY2);
nextDay = new JButton("Next Day");
exitGame = new JButton("Exit Game");
newGame = new JButton("New Game");
nextDay.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, DAY2);
}
});
JPanel optionsPanel = new JPanel();
optionsPanel.setLayout(new GridLayout(1, 3));
optionsPanel.add(newGame);
optionsPanel.add(exitGame);
optionsPanel.add(nextDay);
Container contentPane = getContentPane();
contentPane.add(optionsPanel, "North");
contentPane.add(cards, "Center");
}
}
The best way to do this if you don't necessarily need a new JFrame is the way #pavithraCS described. What I want to add is that normally, when you want a new "window" with different components to appear, you don't use a new JFrame because that opens a new window. Instead, using a new JPanel is more useful because you can stack them without having to switch to another window.
I hope this helps for the future.

Java: Layout Button/Label Placement

I'm trying to make a GUI pizza menu but I'm having trouble with the placements of the buttons/labels
public class PizzaGUI extends JFrame {
private JRadioButton rdoSmall;
private JRadioButton rdoMedium;
private JRadioButton rdoLarge;
private JRadioButton rdoExtraLarge;
private JLabel lblSize;
private ButtonGroup grpSize;
JPanel panelSize;
private JCheckBox chkPepperoni;
private JCheckBox chkMushrooms;
private JCheckBox chkOlives;
private JCheckBox chkPineapple;
private JLabel lblToppings;
JPanel panelToppings;
private JRadioButton rdoSoda;
private JRadioButton rdoTea;
private JRadioButton rdoBottledWater;
private JRadioButton rdoTapWater;
private JLabel lblDrinks;
private ButtonGroup grpDrinks;
JPanel panelDrinks;
JPanel container;
JButton calculateTotal;
JLabel order;
PizzaGUI()
{
super("Pizza Menu");
setSize(600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createPanel();
add(container);
setVisible(true);
}
public void createPanel()
{
panelSize = new JPanel(new GridLayout(5, 1));
panelToppings = new JPanel(new GridLayout(5, 1));
panelDrinks = new JPanel(new GridLayout(5, 1));
container = new JPanel();
//Calculate Total
calculateTotal = new JButton("Calculate");
calculateTotal.setPreferredSize(new Dimension(95,45));
order = new JLabel("Your Order:");
//Pizza Sizes
lblSize = new JLabel("Choose a size:");
rdoSmall = new JRadioButton("Small ($7)");
rdoMedium = new JRadioButton("Medium ($9)");
rdoLarge = new JRadioButton("Large ($11)");
rdoExtraLarge = new JRadioButton("Extra Large ($14)");
//Toppings
lblToppings = new JLabel("Choose toppings ($1 Each):");
chkPepperoni = new JCheckBox("Pepperoni");
chkMushrooms = new JCheckBox("Mushrooms");
chkOlives = new JCheckBox("Olives");
chkPineapple = new JCheckBox("Pineapple");
//Drinks
lblDrinks = new JLabel("Choose a drink:");
rdoSoda = new JRadioButton("Soda ($2.00)");
rdoTea = new JRadioButton("Tea ($1.50)");
rdoBottledWater = new JRadioButton("Bottled Water ($1.25)");
rdoTapWater = new JRadioButton("Tap Water (No charge)");
//Add pizza sizes to button group
grpSize = new ButtonGroup();
grpSize.add(rdoSmall);
grpSize.add(rdoMedium);
grpSize.add(rdoLarge);
grpSize.add(rdoExtraLarge);
//Add drinks to button group
grpDrinks = new ButtonGroup();
grpDrinks.add(rdoSoda);
grpDrinks.add(rdoTea);
grpDrinks.add(rdoBottledWater);
grpDrinks.add(rdoTapWater);
//Add to panel
panelSize.add(lblSize);
panelSize.add(rdoSmall);
panelSize.add(rdoMedium);
panelSize.add(rdoLarge);
panelSize.add(rdoExtraLarge);
panelToppings.add(lblToppings);
panelToppings.add(chkPepperoni);
panelToppings.add(chkMushrooms);
panelToppings.add(chkOlives);
panelToppings.add(chkPineapple);
panelDrinks.add(lblDrinks);
panelDrinks.add(rdoSoda);
panelDrinks.add(rdoTea);
panelDrinks.add(rdoBottledWater);
panelDrinks.add(rdoTapWater);
container.add(panelSize);
container.add(Box.createHorizontalStrut(40));
container.add(panelToppings);
container.add(Box.createHorizontalStrut(40));
container.add(panelDrinks);
container.add(calculateTotal);
container.add(Box.createVerticalStrut(100));
container.add(order);
}
}
This the output
Menu
I have three separate grid layout panels for each type of menu and then I add it into one main panel which gets added to the frame. I would like to get the "Your Order:" placed in the middle left like the red one shown in the image. I know I can set the layout to null which would allow me to enter coordinates and it would solve my issue, however I read that it is bad practice to do it like that and I'm trying to effectively learn how to use layouts correctly. I don't even know if I'm on the right track, any examples would help. Thanks
May be this can help you.
You can add a seperate panel and then create a empty border for that panel.
container.add(panelSize);
container.add(Box.createHorizontalStrut(40));
container.add(panelToppings);
container.add(Box.createHorizontalStrut(40));
container.add(panelDrinks);
container.add(calculateTotal);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 200, 10, 700));
panel.add(order, BorderLayout.EAST);
container.add(panel);
Your modified code it do what you want :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class PizzaGUI extends JFrame {
private JRadioButton rdoSmall;
private JRadioButton rdoMedium;
private JRadioButton rdoLarge;
private JRadioButton rdoExtraLarge;
private JLabel lblSize;
private ButtonGroup grpSize;
JPanel panelSize;
private JCheckBox chkPepperoni;
private JCheckBox chkMushrooms;
private JCheckBox chkOlives;
private JCheckBox chkPineapple;
private JLabel lblToppings;
JPanel panelToppings;
private JRadioButton rdoSoda;
private JRadioButton rdoTea;
private JRadioButton rdoBottledWater;
private JRadioButton rdoTapWater;
private JLabel lblDrinks;
private ButtonGroup grpDrinks;
JPanel panelDrinks;
JPanel container;
JButton calculateTotal;
JLabel order;
PizzaGUI()
{
super("Pizza Menu");
setSize(600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createPanel();
add(container);
setVisible(true);
}
public void createPanel()
{
panelSize = new JPanel(new GridLayout(5, 1));
panelToppings = new JPanel(new GridLayout(5, 1));
panelDrinks = new JPanel(new GridLayout(5, 1));
container = new JPanel(new GridLayout(2,1));
JPanel container2 = new JPanel();
JPanel placedOrderPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
//Calculate Total
calculateTotal = new JButton("Calculate");
calculateTotal.setPreferredSize(new Dimension(95,45));
order = new JLabel("Your Order:");
placedOrderPanel.add(new JLabel("Your Order placed"));
//Pizza Sizes
lblSize = new JLabel("Choose a size:");
rdoSmall = new JRadioButton("Small ($7)");
rdoMedium = new JRadioButton("Medium ($9)");
rdoLarge = new JRadioButton("Large ($11)");
rdoExtraLarge = new JRadioButton("Extra Large ($14)");
//Toppings
lblToppings = new JLabel("Choose toppings ($1 Each):");
chkPepperoni = new JCheckBox("Pepperoni");
chkMushrooms = new JCheckBox("Mushrooms");
chkOlives = new JCheckBox("Olives");
chkPineapple = new JCheckBox("Pineapple");
//Drinks
lblDrinks = new JLabel("Choose a drink:");
rdoSoda = new JRadioButton("Soda ($2.00)");
rdoTea = new JRadioButton("Tea ($1.50)");
rdoBottledWater = new JRadioButton("Bottled Water ($1.25)");
rdoTapWater = new JRadioButton("Tap Water (No charge)");
//Add pizza sizes to button group
grpSize = new ButtonGroup();
grpSize.add(rdoSmall);
grpSize.add(rdoMedium);
grpSize.add(rdoLarge);
grpSize.add(rdoExtraLarge);
//Add drinks to button group
grpDrinks = new ButtonGroup();
grpDrinks.add(rdoSoda);
grpDrinks.add(rdoTea);
grpDrinks.add(rdoBottledWater);
grpDrinks.add(rdoTapWater);
//Add to panel
panelSize.add(lblSize);
panelSize.add(rdoSmall);
panelSize.add(rdoMedium);
panelSize.add(rdoLarge);
panelSize.add(rdoExtraLarge);
panelToppings.add(lblToppings);
panelToppings.add(chkPepperoni);
panelToppings.add(chkMushrooms);
panelToppings.add(chkOlives);
panelToppings.add(chkPineapple);
panelDrinks.add(lblDrinks);
panelDrinks.add(rdoSoda);
panelDrinks.add(rdoTea);
panelDrinks.add(rdoBottledWater);
panelDrinks.add(rdoTapWater);
container2.add(panelSize);
container2.add(Box.createHorizontalStrut(40));
container2.add(panelToppings);
container2.add(Box.createHorizontalStrut(40));
container2.add(panelDrinks);
container2.add(calculateTotal);
container2.add(Box.createVerticalStrut(100));
container2.add(order);
container.add(container2);
container.add(placedOrderPanel);
}
public static void main(String args[]) {
new PizzaGUI();
}
}

Setting the size of JPanel that contains JRadioButton

I have some problem , i want my jpanel to look like this
but after codING i had this view and i didnt find the solution ,
i need some helps please.
i put the radio button inside jScrollPan
//listmateriel is a list (vector) of radio button ----------
//imagebox is jpanel contains jradio buttons and the image ---------
//gridlayoutPanel2 is Jpanel contains the two text fields and jlabel and a JComboBox
package tpjava;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
i.......
public class Frame extends JFrame implements ActionListener {
private static final int Haut = 550;
private static final int Larg = 720;
private final JPanel utilisateur;
private final JButton recherche;
private final JRadioButton radioButton,radioButton2;
private Vector listmateriel;
private final JLabel label;
public Frame()
{
super("Gestion des materiels");
setSize(Larg,Haut);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane menu = new JTabbedPane();
JPanel emprunt = new JPanel();
emprunt.setPreferredSize(new Dimension(Larg, Haut ));
emprunt.setLayout(new BoxLayout(emprunt, BoxLayout.Y_AXIS));
emprunt.setBorder(BorderFactory.createEmptyBorder(20,20, 20, 20));
JPanel emp1 = new JPanel();
emp1.setPreferredSize(new Dimension(Larg-40, 200 ));
emp1.setLayout(new BorderLayout());
JLabel title2 = new JLabel("Nouvel emprunt:");
title2.setBorder(BorderFactory.createEmptyBorder(20,20,0,0));
JPanel box2 = new JPanel();
box2.setPreferredSize(new Dimension(Larg, 40 ));
box2.setLayout(new BoxLayout(box2, BoxLayout.X_AXIS));
box2.add(title2);
box2.add(Box.createHorizontalStrut(650));
emprunt.add(box2);
JLabel label3 = new JLabel("test");
JButton dispo = new JButton("Disponibilité");
dispo.addActionListener((ActionListener) this);
dispo.setPreferredSize(new Dimension(200, 30));
JPanel box1 = new JPanel();
box1.setLayout(new BoxLayout(box1, BoxLayout.X_AXIS));
box1.add(dispo);
box1.add(Box.createHorizontalStrut(450));
box1.add(label3);
emp1.add(box1,BorderLayout.SOUTH);
listmateriel = new Vector();
for(int k =0 ;k <10 ; k++)
listmateriel .add(new JRadioButton(res.getString("nom"+k)));
......
ImageIcon newIcon = new ImageIcon(bi);
JLabel picture = new JLabel(newIcon);
picture.setBorder(BorderFactory.createEmptyBorder(0,450,0,0));
JPanel jplRadio = new JPanel();
jplRadio.setLayout(new GridLayout(0, 1));
for(int i = 0 ; i < listmateriel.size() ;i++)
{
jplRadio.add((Component) listmateriel.elementAt(i));
}
JPanel imagebox = new JPanel();
imagebox.setLayout(new BorderLayout());
JScrollPane scroll3 = new JScrollPane(jplRadio);
scroll3.setPreferredSize(new Dimension(200, 30));
imagebox.add(scroll3, BorderLayout.WEST);
imagebox.add(picture, BorderLayout.CENTER);
emp1.add(imagebox,BorderLayout.CENTER);
JPanel box3 = new JPanel();
box3.setLayout(new BoxLayout(box3, BoxLayout.X_AXIS));
JLabel title3 = new JLabel("Remplir les champs suivants:");
box3.add(title3);
box3.add(Box.createHorizontalStrut(650));
JPanel gridlayoutPanel2 = new JPanel();
........
JPanel util5 = new JPanel();
util5.setLayout(new GridLayout(0,1));
util5.setBorder(BorderFactory.createLineBorder(Color.black));
JLabel resul2 = new JLabel("bsfgbsdg");
JScrollPane scroll2 = new JScrollPane();
util5.add(resul2);
emprunt.add(emp1);
emprunt.add(box3);
emprunt.add(gridlayoutPanel2);
emprunt.add(util5);
menu.add("emprunts",emprunt);
//---------------------
getContentPane().add(menu);
pack();
setVisible(true);
}
}
GridLayout(0, 1) instead of BoxLayout is the solution thank you #trashgod

One of my Panels is not showing

What's wrong with my code? The panel named 'leftPanel' is not showing up when i run the program whereas 'flightPanel
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TravelAgentSystem {
public static void main(String[] args){
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.setTitle("Main Menu");
mainFrame.setVisible(true);
mainFrame.setSize(500,500);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel flightPanel = new JPanel();
flightPanel.setLayout(new GridLayout(2,2));
JButton timeb, priceb, hotelb, exitb;
timeb = new JButton("Time");
priceb = new JButton ("Price");
hotelb = new JButton ("Hotels Menu");
exitb = new JButton ("Exit Program");
class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton)event.getSource();
String buttonText = clickedButton.getText();
if (buttonText.equals("Exit Program")) {System.exit(0);}
}
}
buttonHandler handler = new buttonHandler();
exitb.addActionListener(handler);
flightPanel.add(timeb);
flightPanel.add(priceb);
flightPanel.add(hotelb);
flightPanel.add(exitb);
mainFrame.add(flightPanel, BorderLayout.CENTER);
JPanel travelPanel = new JPanel();
travelPanel.setLayout(new GridLayout(2,2));
travelPanel.setVisible(true);
JPanel timePanel = new JPanel();
timePanel = new JPanel();
timePanel.setLayout(new GridLayout(2,1));
timePanel.setVisible(true);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(2,1));
leftPanel.setVisible(true);
JLabel Lfrom, Lto, LDeparture;
Lfrom = new JLabel("From");
Lto = new JLabel("To");
LDeparture = new JLabel("Departure Date (DD/MM/YY)");
String[] fromOptions = {"East Midlands","Birmingham","Heathrow","Manchester"};
String[] toOptions = {"New York", "Dahab", "Rome", "Sydney", "Tokyo"};
JComboBox fromDest = new JComboBox(fromOptions);
JComboBox toDest = new JComboBox(toOptions);
JPanel datePanel = new JPanel();
datePanel.setLayout(new FlowLayout()); // So i can fix size of dateField
JTextField dateField = new JTextField();
dateField.setPreferredSize(new Dimension(100,20));
datePanel.add(dateField);
travelPanel.add(Lfrom);
travelPanel.add(fromDest);
travelPanel.add(Lto);
travelPanel.add(toDest);
timePanel.add(LDeparture);
timePanel.add(datePanel);
leftPanel.add(travelPanel);
leftPanel.add(timePanel);
mainFrame.add(leftPanel, BorderLayout.EAST);
}
}
Call mainFrame.setVisible(true); in last. I.e,
mainFrame.add(leftPanel, BorderLayout.EAST);
mainFrame.setVisible(true);
Here's what I get :

Content in JPanel won't appear

There is no errors but when I Run it the content that I added into the JPanel won't appear, only the one not inside the JPanel appear.
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame
{
public static void main(String arg[])
{
SimpleGUI f = new SimpleGUI("GUI components");
f.setSize(600,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
SimpleGUI(String s)
{
setTitle(s);
setLayout(new GridLayout(3,2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel ("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel ("Enter age:");
JTextField tf2= new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold",true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5,20);
JList list = new JList(new Object[] {"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play",ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
}
}
I've updated your code. Have a look at this version:
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame {
public static void main(String arg[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SimpleGUI f = new SimpleGUI("GUI components");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public SimpleGUI(String s) {
setTitle(s);
setLayout(new GridLayout(3, 2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel("Enter age:");
JTextField tf2 = new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold", true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5, 20);
JList list = new JList(new Object[]{"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play", ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
/**
* Need to add the following lines
*/
this.add(p1);
this.add(p2);
this.add(p3);
this.pack();
this.setVisible(true);
}
}
A couple of pointers:
You need to add your components to your JFrame for them to actually show up.
Any updates to the user interface must happen on the event dispatch thread. Consequently you would notice that I've added a SwingUtilites.invokeLater() to the main. Have a look at this article to understand "Threading with Swing"
Where you you add your panels to the frame? Also, forgotten my java "rules and regulations": do you need to call "super()"?

Categories