BoxLayout Left Alignment - java

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.

Related

how to make the text field next to the label in GUI

import java.awt.*;
import javax.swing.*;
public class guiAs {
public static void main (String args [] )
{
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension (300,300));
frame.setTitle("Calculator");
JPanel panel = new JPanel();
// JLabel label = new JLabel();
//creating num1
JPanel panel2 = new JPanel();
JLabel lable1 = new JLabel("Number 1 ");
JTextField tf1 = new JTextField();
//creating num2
JLabel lable2 = new JLabel("Number 2 ");
JTextField tf2 = new JTextField();
//creating result
JPanel panel3 = new JPanel();
JLabel lable3 = new JLabel("Result: ");
JTextField tf3 = new JTextField(10);
//creating button
JButton Add= new JButton("Add");
JButton Subtract = new JButton("Subtract");
JButton Multiply = new JButton("Multiply");
JButton Division = new JButton("Division");
//creating num1
panel2.add(lable1);
panel2.add(tf1);
//creating num2
panel2.add(lable2);
panel2.add(tf2);
//creating result
panel3.add(lable3);
panel3.add(tf3);
//creating buttons
panel.add(Add);
panel.add(Subtract);
panel.add(Multiply);
panel.add(Division);
// frame.getContentPane().add(BorderLayout.WEST, panel3);
//creating Box Layout for num1 and num2
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
frame.add(panel2);
//creating Box Layout for buttons
BoxLayout layout1 = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(layout1);
frame.setLayout(new FlowLayout());
frame.add(panel);
// Add.setAlignmentX(Component.RIGHT_ALIGNMENT);
// panel.add(Add);
// frame.getContentPane().add(BorderLayout.SOUTH, panel);
//creating Border Layout for num1 and num2
frame.getContentPane().add(BorderLayout.WEST, panel2);
//creating Border Layout for Buttons
frame.getContentPane().add(BorderLayout.EAST, panel);
//creating Box Layout for Result
frame.getContentPane().add(BorderLayout.SOUTH, panel3);
frame.setVisible(true);
}
}
this is my code, and I have no idea to make the text field next to the numbers. i try several times but does not comes together. whenever i add something the panels moves
its work only with the result but for the number doesn't work
here i used Box Layout for buttons and number 1 and number 2:
You were very close to solving it.
Replace this line
JPanel panel2 = new JPanel();
With this line
JPanel panel2 = new JPanel(new GridLayout(2, 2));
Then, get rid of these 3 lines
BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2);
frame.setLayout(new FlowLayout());
Now your code looks like this

Add a gridLayout(2,2) in CENTER

I want to create a gridLayout with 4 buttons at the center of my Layout, and a button to PAGE_END,PAGE_START,LINE_END,LINE_START. My code does show the last buttons I told you, but not the grid button ones.
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Hello World!");
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel p = new JPanel(new BorderLayout());
GridLayout grid = new GridLayout(2,2);
p.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton bg1 = new JButton("Button 1");
p.add(bg1, c);
JButton bg2 = new JButton("Button 2");
p.add(bg2, c);
JButton bg3 = new JButton("Button 3");
p.add(bg3, c);
JButton bg4 = new JButton("Button 4");
p.add(bg4, c);
frame.setLayout(new BorderLayout());
JButton b1 = new JButton("TOP");
JButton b2 = new JButton("LEFT");
JButton b3 = new JButton("RIGHT");
JButton b4 = new JButton("BOTTOM");
frame.add(b1,BorderLayout.PAGE_START);
frame.add(b2,BorderLayout.LINE_START);
frame.add(b3,BorderLayout.LINE_END);
frame.add(b4,BorderLayout.PAGE_END);
}
}
You have created the panel p, but you haven't added it to your frame.
Add this line in your code:
frame.add(p,BorderLayout.CENTER);
Also, you must use this line, if you want the buttons in a grid:
p.setLayout(grid);
instead of p.setLayout(new GridBagLayout());

How do i display the radiobutton and label in the same row?

private String[] gender = {"Male","Female"};
private JComboBox jco = new JComboBox();
private JRadioButton[] jrbGender;
private ButtonGroup buttonGroup = new ButtonGroup();
private JButton jbtAdd = new JButton("Create");
private JButton jbtRetrieve = new JButton("Retrieve");
private JButton jbtUpdate = new JButton("Update");
private JButton jbtDelete = new JButton("Delete");
public RegistrationForMembership(){
JPanel jp1 = new JPanel(new GridLayout(6,2));
JPanel jp2 = new JPanel(new FlowLayout());
JPanel jp3 = new JPanel(new GridLayout(1,1));
jco = new JComboBox(membership);
jrbGender = new JRadioButton[gender.length];
add(jp1);
jp1.add(new JLabel("Member ID"));
jp1.add(jtfID);
jp1.add(new JLabel("Member Name"));
jp1.add(jtfName);
jp1.add(new JLabel("Member IC"));
jp1.add(jtfIC);
jp1.add(new JLabel("Address"));
jp1.add(jtfAddress);
jp1.add(new JLabel("Gender"));
for(int i =0; i<gender.length;++i){
jrbGender[i] = new JRadioButton(gender[i]);
buttonGroup.add(jrbGender[i]);
jp1.add(jrbGender[i]);
}
add(jp1);
The one of the radio button will go to the next line , how do i let the radio button on the same line with the label ?
Add the JRadioButtons to a new JPanel and add that one to jp1.
JPanel radios = new JPanel();
for (int i = 0; i < gender.length; ++i){
jrbGender[i] = new JRadioButton(gender[i]);
buttonGroup.add(jrbGender[i]);
radios.add(jrbGender[i]);
}
jp1.add(radios);
Also, it looks like jp1 should have 5 rows, not 6.
JPanel jp1 = new JPanel(new GridLayout(5,2));

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

CardLayout Manager Questions/Confusion

I'm currently working on a program which I've asked questions involving previously.
I've made a good amount of progress since then, and now I'm ready to begin on the infoPanel portion of the client program. I've read through the documentation and other questions, but I'm a still a bit confused. How do I setup the panel to, y'know, work? How do I link it with the JList(If that's even possible to use a JList instead of a combo box), and then how do I establish which selection references which card?
Thank you so much for any assistance!
Source:
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class ClientApp extends JFrame
{
public static void main(String[] args)
{
new ClientApp();
}
public ClientApp()
{
this.setSize(320,380);
this.setTitle("Honeydukes Candy Order");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
JPanel infoPanel = new JPanel(new CardLayout());
JPanel invntryPanel = new JPanel();
String[] candy = {"Acid Pops", "Bat's Blood Soup",
"Bertie Bott's Every Flavour Beans",
"Blood-flavoured Lollipops",
"Cauldron Cakes", "Charm Choc",
"Chocoballs", "Chocolate Cauldrons",
"Chocolate Frogs", "Chocolate Skeletons",
"Chocolate Wands", "Choco-Loco", "Cockroach Clusters",
"Nougat", "Crystallised Pineapple",
"Drooble's Best Blowing Gum", "Exploding Bonbons",
"Toffees", "Fizzing Whizzbees",
"Fudge Flies", "Ice Mice",
"Jelly Slugs", "Liquourice Wands",
"Pepper Imps", "Peppermint Toads",
"Pink Coconut Ice", "Pixie Puffs",
"Pumpkin Fizz", "Salt Water Taffy",
"Shock-o-Choc", "Skeletal Sweets",
"Splindle's Lick'O'Rish Spiders",
"Sugar Quills", "Sugared Butterfly Wings",
"Toothflossing Stringmints", "Tooth-Splintering Strongmints",
"Treacle Fudge", "Chocolates", "Wizochoc"};
JList candyList = new JList(candy);
candyList.setVisibleRowCount(18);
candyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll = new JScrollPane(candyList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
invntryPanel.add(scroll);
JPanel startCard = new JPanel();
JPanel acidPopsCard = new JPanel();
JPanel batsBloodSoupCard = new JPanel();
JPanel bertieBottsCard = new JPanel();
JPanel bloodPopsCard = new JPanel();
JPanel cauldronCakesCard = new JPanel();
JPanel charmChocCard = new JPanel();
JPanel chocoballsCard = new JPanel();
JPanel chocCauldronsCard = new JPanel();
JPanel chocFrogsCard = new JPanel();
JPanel chocSkeleCard = new JPanel();
JPanel chocWands = new JPanel();
JPanel chocolocoCard = new JPanel();
JPanel roachClustersCard = new JPanel();
JPanel nougatCard = new JPanel();
JPanel crystalPineappleCard = new JPanel();
JPanel droobleGumCard = new JPanel();
JPanel explodeBonbonsCard = new JPanel();
JPanel toffeesCard = new JPanel();
JPanel fizzWhizCard = new JPanel();
JPanel fudgeFliesCard = new JPanel();
JPanel iceMiceCard = new JPanel();
JPanel jellySlugsCard = new JPanel();
JPanel liquorWandsCard = new JPanel();
JPanel pepImpsCard = new JPanel();
JPanel pinkCocoIceCard = new JPanel();
JPanel pixiePuffsCard = new JPanel();
JPanel pumpkFizzCard = new JPanel();
JPanel saltTaffeyCard = new JPanel();
JPanel shockChocCard = new JPanel();
JPanel skeleSweetsCard = new JPanel();
JPanel spindleSpidersCard = new JPanel();
JPanel sugarQuillsCard = new JPanel();
JPanel sugarWingsCard = new JPanel();
JPanel flossMintsCard = new JPanel();
JPanel splintMintsCard = new JPanel();
JPanel treacleFudgeCard = new JPanel();
JPanel chocolatesCard = new JPanel();
JPanel wizochocCard = new JPanel();
infoPanel.add(startCard);
infoPanel.add(acidPopsCard);
infoPanel.add(batsBloodSoupCard);
infoPanel.add(bertieBottsCard);
infoPanel.add(bloodPopsCard);
infoPanel.add(cauldronCakesCard);
infoPanel.add(charmChocCard);
infoPanel.add(chocoballsCard);
infoPanel.add(chocCauldronsCard);
infoPanel.add(chocFrogsCard);
infoPanel.add(chocSkeleCard);
infoPanel.add(chocWands);
infoPanel.add(chocolocoCard);
infoPanel.add(roachClustersCard);
infoPanel.add(nougatCard);
infoPanel.add(crystalPineappleCard);
infoPanel.add(droobleGumCard);
infoPanel.add(explodeBonbonsCard);
infoPanel.add(toffeesCard);
infoPanel.add(fizzWhizCard);
infoPanel.add(fudgeFliesCard);
infoPanel.add(iceMiceCard);
infoPanel.add(jellySlugsCard);
infoPanel.add(liquorWandsCard);
infoPanel.add(pepImpsCard);
infoPanel.add(pinkCocoIceCard);
infoPanel.add(pixiePuffsCard);
infoPanel.add(pumpkFizzCard);
infoPanel.add(saltTaffeyCard);
infoPanel.add(shockChocCard);
infoPanel.add(skeleSweetsCard);
infoPanel.add(spindleSpidersCard);
infoPanel.add(sugarQuillsCard);
infoPanel.add(sugarWingsCard);
infoPanel.add(flossMintsCard);
infoPanel.add(splintMintsCard);
infoPanel.add(treacleFudgeCard);
infoPanel.add(chocolatesCard);
infoPanel.add(wizochocCard);
this.add(invntryPanel, BorderLayout.WEST);
this.add(infoPanel, BorderLayout.EAST);
this.setVisible(true);
}
}
I would add a ListSelectionListener to the JList, and inside of that listener change the card displayed by the CardLayout.
You really need to read the tutorial on how to use CardLayout first though as there you'd see that your add method is wrong. Consider using the Strings held by the JList as the constant that you'd use when adding components to the CardLayout-using panel.

Categories