Setting the size of JPanel that contains JRadioButton - java

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

Related

Adding components, like buttons, onto others, like JTextAreas, in code is not appearing when I run

Below is a picture of the basic interface I want created. Everything works fine except for the following:-
The JScrollPane isn't appearing when I try inserting lots of text
in the InformationDisplay JTextArea and I don't get why
The buttons I inserted in the unitButtonPanel in the availableUnits JTextArea isn't appearing at all
This is the part in my code that tackles point #1
InfoPanel with informationDisplay TextArea and JScrollPane
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
JTextArea informationDisplay= new JTextArea();
JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollbar1.setSize( 20, 20 );
informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));
infoPanel.add(scrollbar1);
add(infoPanel, BorderLayout.SOUTH);
This is the part in my code that tackles point #2
Adding a textPanel 3 rows, 1 column to encapsulate 3 TextAreas
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3, 1, 4, 4));
TextArea1 "availableUnits" 2 rows and 2 columns to encapsulate 4 buttons
JTextArea availableUnits = new JTextArea(2,2);
//availableUnits.setPreferredSize(new Dimension(200, 200));
JPanel unitButtonPanel = new JPanel();
unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
JButton ambulanceUnit = new JButton("Ambulance");
JButton diseaseControlUnit = new JButton ("diseaseControlUnit");
JButton gasControlUnit = new JButton("gasControlUnit");
JButton fireTruck = new JButton("fireTruck");
unitButtonPanel.add(ambulanceUnit);
unitButtonPanel.add(diseaseControlUnit);
unitButtonPanel.add(gasControlUnit);
unitButtonPanel.add(fireTruck);
availableUnits.add(unitButtonPanel);
textPanel.add(availableUnits);
This is the full code ready for compilation:-
import java.awt.*;
import javax.swing.*;
public class GameView extends JFrame {
private JPanel buttonPanel;
private JPanel infoPanel;
private JTextArea informationDisplay;
private JTextArea availableUnits;
private JPanel unitButtonPanel;
private JPanel disasterPanel;
private JTextArea disasterDisplay;
private JButton ambulanceUnit;
private JButton diseaseControlUnit;
private JButton gasControlUnit;
private JButton fireTruck;
public static void main(String[] args) {
new GameView();
}
public GameView() {
setSize(1000, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
int xPos = (dim.width/2) - (this.getWidth()/2);
int yPos = (dim.height/2) - (this.getHeight()/2);
this.setLocation(xPos,yPos);
JPanel title = new JPanel();
JLabel label1 = new JLabel("Invaded City");
title.add(label1);
add(title, BorderLayout.NORTH);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(10, 10, 2, 2));
for (int i = 0; i < 100; i++) {
JButton mapButton = new JButton();
addButton(mapButton);
}
add(buttonPanel, BorderLayout.CENTER);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(3, 1, 4, 4));
availableUnits = new JTextArea(2,2);
//availableUnits.setPreferredSize(new Dimension(200, 200));
unitButtonPanel = new JPanel();
unitButtonPanel.setLayout(new GridLayout(2, 2, 2, 2));
ambulanceUnit = new JButton("Ambulance");
diseaseControlUnit = new JButton ("diseaseControlUnit");
gasControlUnit = new JButton("gasControlUnit");
fireTruck = new JButton("fireTruck");
unitButtonPanel.add(ambulanceUnit);
unitButtonPanel.add(diseaseControlUnit);
unitButtonPanel.add(gasControlUnit);
unitButtonPanel.add(fireTruck);
availableUnits.add(unitButtonPanel);
textPanel.add(availableUnits);
JTextArea respondingUnits = new JTextArea(2,2);
//respondingUnits.setPreferredSize(new Dimension(200, 200));
textPanel.add(respondingUnits);
JTextArea treatingUnits = new JTextArea(2,2);
//treatingUnits.setPreferredSize(new Dimension(200, 200));
textPanel.add(treatingUnits);
add(textPanel, BorderLayout.EAST);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BorderLayout());
JTextArea informationDisplay= new JTextArea();
JScrollPane scrollbar1 = new JScrollPane(informationDisplay, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollbar1.setSize( 20, 20 );
informationDisplay.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
informationDisplay.setText("The \n" + "It\n "+ "The\n" +"A\n" + "About\n" + "\n" + "Cats\n" + "Cats\n" + "Cats\n" + "Cats\n");
informationDisplay.setPreferredSize(new Dimension(getWidth(), 200));
infoPanel.add(scrollbar1);
add(infoPanel, BorderLayout.SOUTH);
disasterPanel = new JPanel();
disasterPanel.setLayout(new BorderLayout());
disasterDisplay = new JTextArea();
disasterDisplay.setPreferredSize(new Dimension(getWidth(), 200));
disasterPanel.add(disasterDisplay);
add(disasterPanel, BorderLayout.WEST);
pack();
setVisible(true);
}
public void addButton(JButton b) {
buttonPanel.add(b);
}
}
Output:-

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();
}
}

Add Jpanel to a Jscrollbar

I'm trying to add a Jpanel to a JscrollBAr but when i run java it's not working. It's a RadioButton with a question. Can you help me please ?
this is my class jpanel :
public class yesnoPanel extends JPanel {
private JPanel quest;
private JPanel answ;
public yesnoPanel(String q){
JPanel pan = new JPanel();
JRadioButton yes = new JRadioButton("yes");
JRadioButton no = new JRadioButton("no");
ButtonGroup bg = new ButtonGroup();
JTextArea question = new JTextArea(1,20);
question.setText(q);
JPanel pan2 = new JPanel();
pan2.add(question);
bg.add(yes);
bg.add(no);
pan.add(yes);
pan.add(no);
this.quest=pan2;
this.answ=pan;
}
I can show them when i use JFrame but not with Jscrollbar
This is my main with the jscrollbar :
public static void main(String[] args) {
JFrame fram = new JFrame();
yesnoPanel question = new yesnoPanel("are you ok");
JPanel q = question.getQuestion();
JPanel a = question.getAnsw();
JPanel all = new JPanel();
all.add(q);
all.add(a);
yesnoPanel question2 = new yesnoPanel("sure ?");
JPanel q2 = question2.getQuestion();
q2.setSize(200,500);
JPanel a2 = question2.getAnsw();
JPanel all2 = new JPanel();
all2.add(q2,BorderLayout.WEST);
all2.add(a2);
JScrollPane scroll = new JScrollPane();
scroll.setPreferredSize(new Dimension(100,100));
scroll.setViewportView(all);
scroll.setVisible(true);
}
thanks to all
Replace your last few lines in main method with this to make it work.
JPanel master = new JPanel();
master.add(all);
master.add(all2);
JScrollPane scroll = new JScrollPane(master);
scroll.setPreferredSize(new Dimension(100,100));
scroll.setViewportView(all);
scroll.setVisible(true);
fram.add(scroll);
fram.pack();
fram.setVisible(true);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BoxLayout Left Alignment

I'm trying to left align the class and neutral buttons so they are in line with the left most card button. For some reason, setAlignmentX only shifts the buttons half way. Here is the code. Is there away to align the buttons?
private String[] listEntries = {"a","a","a","a","a"};
private JButton remove = new JButton("Remove");
private JList list;
private JButton b1 = new JButton("Class");
private JButton b2 = new JButton("Neutral");
private JPanel page = new JPanel(new CardLayout());
private DefaultListModel listModel = new DefaultListModel();
public Main () {
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.PAGE_AXIS));
leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.PAGE_AXIS));
rightPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
leftPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel title = new JLabel("Deck Constructor", SwingConstants.CENTER);
title.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
//Set up Deck List
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(-1);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(150, 80));
JLabel listTitle = new JLabel("List");
listTitle.setLabelFor(list);
listScroller.setAlignmentX(LEFT_ALIGNMENT);
rightPanel.add(listTitle);
rightPanel.add(listScroller);
rightPanel.add(remove);
//Set up Card Selection
JPanel buttonPanel = new JPanel();
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
b1.setActionCommand("Class");
b2.setActionCommand("Neutral");
b1.addActionListener(this);
b2.addActionListener(this);
buttonPanel.add(b1);
buttonPanel.add(b2);
JPanel classCards = new JPanel(new GridLayout(2,3, 10, 10));
JButton card1 = new JButton("Card 1");
card1.addActionListener(this);
card1.setActionCommand("addCard");
JButton card2 = new JButton("Card 2");
JButton card3 = new JButton("Card 3");
JButton card4 = new JButton("Card 4");
JButton card5 = new JButton("Card 5");
JButton card6 = new JButton("Card 6");
classCards.add(card1);
classCards.add(card2);
classCards.add(card3);
classCards.add(card4);
classCards.add(card5);
classCards.add(card6);
JPanel neutral = new JPanel();
neutral.setBackground(Color.BLUE);
page.add(classCards, "Class");
page.add(neutral, "Neutral");
leftPanel.add(buttonPanel);
leftPanel.add(page);
setPreferredSize(new Dimension(640,640/12*9));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(leftPanel,BorderLayout.CENTER);
getContentPane().add(rightPanel,BorderLayout.EAST);
getContentPane().add(title,BorderLayout.NORTH);
pack();
setVisible(true);
}
It is not perfect solution, but you can use for example:
If you want to keep buttons default size:
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,1,2));
and delete:
buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
If you want to fill buttonPanel with buttons:
JPanel buttonPanel = new JPanel(new GridLayout(1,2,2,2));
buttonPanel.setBorder(new EmptyBorder(2,1,2,1));
In (FlowLayout.LEADING,1,2) and in EmptyBorder(2,1,2,1))the 1,2 values are added to match buttonPanel and classCard hgap and vgap.

Java GUI JScrollBar How to set the length?

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();
});
}
}

Categories