I am adding checkboxes on JPanel in FlowLayout the checkboxes are being added horizontally.
I want to add checkboxes vertically on the Panel. What is the possible solution?
I hope what you are trying to achieve is like this. For this please use Box layout.
package com.kcing.kailas.sample.client;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class Testing extends JFrame {
private JPanel jContentPane = null;
public Testing() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(61, 11, 81, 140);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
jContentPane.add(panel);
JCheckBox c1 = new JCheckBox("Check1");
panel.add(c1);
c1 = new JCheckBox("Check2");
panel.add(c1);
c1 = new JCheckBox("Check3");
panel.add(c1);
c1 = new JCheckBox("Check4");
panel.add(c1);
}
return jContentPane;
}
public static void main(String[] args) throws Exception {
Testing frame = new Testing();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
I used a BoxLayout and set its second parameter as BoxLayout.Y_AXIS and it worked for me:
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
As I stated in comment i would use a box layout for this.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout());
JButton button = new JButton("Button1");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
button = new JButton("Button2");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
button = new JButton("Button3");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
add(panel);
JPanel testPanel = new JPanel();
testPanel.setLayout(new BoxLayout(testPanel, BoxLayout.Y_AXIS));
/*add variables here and add them to testPanel
e,g`enter code here`
testPanel.add(nameLabel);
testPanel.add(textName);
*/
testPanel.setVisible(true);
Related
so I have my Main, and this is done inside of it.
JFrame CF = new JFrame();
CF.setLayout(new BorderLayout());
CF.add(new CarGUI(), BorderLayout.NORTH);
// CF.add(new CarGUI(), BorderLayout.SOUTH);
//' South FlowLayout ' here ^
CF.setSize(600,400);
CF.setVisible(true);
In my CarGUI class I have:
public class CarGUI extends JPanel {
private CarTaxManager manager;
private JLabel lpLabel;
private JTextField searchField;
private JButton searchButton;
public CarGUI(){
FlowLayout NorthLayout = new FlowLayout();
//this.setLayout(new FlowLayout());
this.setLayout(NorthLayout);
lpLabel = new JLabel("License Plate");
searchField = new JTextField(10);
searchButton = new JButton("Search");
add(lpLabel);
add(searchField);
add(searchButton);
}
So basically what has to happen here, is I need to make another flow layout, called 'SouthLayout', and in the main, I need to put it to that one. However, the flowlayout has to be done inside CarGUI. I can't seem to get this working.
EDIT:
What it has to look like eventually:
So I'll be needing two FlowLayouts in total. One for the top, and one at the bottom. Neither of them include the TextPane in the middle.
This all comes in a borderLayout in the main.
Thanks in advance!
Sounds like a good candidate for BorderLayout
I've modified your code to get a good start at implementing it, given what you have shown us:
public class CarGUI extends JPanel {
private CarTaxManager manager;
private JLabel lpLabel;
private JTextField searchField;
private JButton searchButton;
public CarGUI(){
setLayout(new BorderLayout());
JPanel north = new JPanel();
north .setLayout(new FlowLayout());
lpLabel = new JLabel("License Plate");
searchField = new JTextField(10);
searchButton = new JButton("Search");
north.add(lpLabel);
north.add(searchField);
north.add(searchButton);
add(north, BorderLayout.NORTH);
JPanel center = new JPanel();
center.setLayout(new FlowLayout());
//TODO add components to center
add(center, BorderLayout.CENTER);
JPanel south= new JPanel();
south.setLayout(new FlowLayout());
//TODO add components to south
add(south, BorderLayout.SOUTH);
}
I think a BorderLayout would work well. The big text field could be in the center and you could have the buttons and menus above and below it. Of course, A simple BoxLayout would also do the job fine. Here is a simple example on how to achieve this with a BorderLayout.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CarGUI extends JFrame {
public CarGUI() {
initGUI();
}
public void initGUI() {
setLayout(new BorderLayout());
setSize(600, 400);
initNorthGUI();
initCenterGUI();
initSouthGUI();
setVisible(true);
}
private void initNorthGUI() {
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(new JLabel("License Plate"));
northPanel.add(new JTextField(10));
northPanel.add(new JButton("Search"));
add(northPanel, BorderLayout.PAGE_START);
}
private void initCenterGUI() {
JLabel centerPanel = new JLabel("Center");
centerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
add(centerPanel, BorderLayout.CENTER);
}
private void initSouthGUI() {
JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout());
southPanel.add(new JButton("Some Button"));
southPanel.add(new JComboBox());
add(southPanel, BorderLayout.PAGE_END);
}
public static void main(String args[]) {
CarGUI c = new CarGUI();
}
}
Each individual 'JPanel', or any other container, can have its own layout manager. So if you want a different layout in part of your application, add a JPanel with the layout you need.
Problem - the given codes below is not displaying my JPanel(PageOne) and I am not sure why is it not displaying my JPanel(PageOne). Please Help.
I have added the JPanel(PageOne) to my panel which has a cardLayout();
I have set my JFrame to visible already.
PageOne.java
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PageOne extends JPanel {
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
} }
PageTwo.java
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PageTwo extends JPanel {
public PageTwo() {
JLabel label = new JLabel("Page 2");
JPanel panel = new JPanel();
panel.add(label);
}
}
DisplayUI.java
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DisplayUI {
public static void main(String[] args) {
new DisplayUI();
}
public DisplayUI() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
CardLayout cardLayout = new CardLayout();
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
panel.setLayout(cardLayout);
panel.add(new PageOne(), "1");
panel.add(new PageTwo(), "2");
cardLayout.show(panel,"1");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
You're not actually adding anything to PageOne or PageTwo panels...
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
// But nothing is actually added to "this"...
}
Unless you "really" need it, you can get rid of the second JPanel and add the label directly to PageOne (and the same thing goes for PageTwo)
public PageOne() {
JLabel label = new JLabel("Page 1");
add(label);
}
Or add the JPanel you create (which contains the label)
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
add(panel);
}
Remember, JPanel is type of Container, it can have child components.
Get the content pane of frame and than try adding :
Container container=frame.getContentPane();
container.add(panel);
Hope this helps you.
I have problem with tabs in the JPanel. I know how to make new tabs in Mainframe, but I don't know how to make tabs into JPanel which is located in Mainframe.
Here are the pictures:
I have program looking like this -
http://www.bildites.lv/viewer.php?file=vklfhvfdfpwpcxllfqv.png
But I want to make it look like this -
http://www.bildites.lv/viewer.php?file=bvbrp4qfx2krn9bkx30j.png
And Here is my code of the blue JPanel:
package gui;
import java.awt.Color;
import javax.swing.JPanel;
public class CallsPanel extends JPanel {
private MainFrame frame;
Color color = new Color(99, 184, 255); // steelblue
public CallsPanel(MainFrame frame) {
this.frame = frame;
this.setLocation(0, 0);
this.setSize(300, 380);
this.setLayout(null);
this.setBackground(color);
this.initContent();
}
// -------------------------------------------------------------------------
// Declare New Things
private void initContent() {
// Add New Things
}
// -------------------------------------------------------------------------
}
Thanks a lot to people that will help!
JTabbedPane tabPane = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel label1 = new JLabel("Tab 1");
JLabel label2 = new JLabel("Tab 2");
panel1.add(label1);
panel2.add(label2);
tabPane.add("Tab 1", panel1);
tabPane.add("Tab 2", panel2);
this.add(tabPane);
Play around with the size/color/shape of the tabPane and see what works for you. But this is the basic of a tabPane.
See this simple runnable example
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MyPanel extends JPanel {
JButton button = new JButton("Button");
JTabbedPane tabPane = new JTabbedPane();
public MyPanel(){
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
tabPane.add("Panel 1", panel1);
tabPane.add("Panel 2", panel2);
tabPane.setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout());
add(tabPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setSize(300, 300);
frame.setVisible(true);
}
});
}
}
I want to put JTabbedPane in the middle,and clicking any tab I want to change will reflect in both above and below panel of tabbedpane.
I tried it but it works only on the below panel.
How to overcome from this problem? Please help me.
Thanks in advance.
Here is my code:
jTabbedPane1 = new javax.swing.JTabbedPane();
jTabbedPane1.addTab("Daily Market", jScrollPane1);
jTabbedPane1.addTab("Weekly Market", jScrollPane2);
On assumption that you want to change something in the panels above and below your tabbed pane. Sample code changing text of a label in top and bottom panel below:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestJTabbedPane extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private void init(){
this.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
final JLabel topLabel = new JLabel("North");
topPanel.add(topLabel);
this.add(topPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel firstTabCont = new JPanel();
firstTabCont.add(new JLabel("First"));
tabbedPane.addTab("First", firstTabCont);
JPanel secondTabCont = new JPanel();
secondTabCont.add(new JLabel("Second"));
tabbedPane.addTab("Second", secondTabCont);
this.add(tabbedPane, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
final JLabel bottomLabel = new JLabel("South");
bottomPanel.add(bottomLabel);
this.add(bottomPanel, BorderLayout.SOUTH);
tabbedPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent evt) {
JTabbedPane pane = (JTabbedPane)evt.getSource();
int selectedIndex = pane.getSelectedIndex();
if(selectedIndex == 0){
topLabel.setText("");
topLabel.setText("Hi");
bottomLabel.setText("");
bottomLabel.setText("Bye");
} else {
topLabel.setText("");
topLabel.setText("Bye");
bottomLabel.setText("");
bottomLabel.setText("Hi");
}
}
});
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestJTabbedPane().init();
}
}
I'm trying to set the divider location of a JSplitPane but it seems not to work.
Here's an SSCCE:
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class JSplitProblem extends JFrame {
public JSplitProblem(){
JPanel upperPanel = new JPanel();
upperPanel.setLayout(new BoxLayout(upperPanel, BoxLayout.X_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
JPanel red = new JPanel();
red.setBackground(Color.red);
leftPanel.add(red);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
JPanel blue = new JPanel();
blue.setBackground(Color.blue);
rightPanel.add(blue);
upperPanel.add(leftPanel);
upperPanel.add(rightPanel);
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.black);
JSplitPane mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel,bottomPanel);
mainSplittedPane.setOneTouchExpandable(true);
mainSplittedPane.setDividerLocation(0.5);
this.add(mainSplittedPane);
this.setSize(800,600);
this.setResizable(true);
this.setVisible(true);
}
public static void main(String[] args) {
new JSplitProblem();
}
}
I would like the black bottom panel to lay on a 50% of the whole area by default. What am I doing wrong?
If you want both halves of the split pane to share in the split pane's extra or removed space, set the resize weight to 0.5: (Tutorial)
JSplitPane mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel,bottomPanel);
mainSplittedPane.setOneTouchExpandable(true);
mainSplittedPane.setResizeWeight(0.5);
nothing complicated in this case, with rules
1) PrefferedSize must returns Childs not as I wrong to set in my case too :-), then my answer isn't #kleopatra resist too
2) put everything about rezize, size, whatever for JSplitPane into invokeLater()
.
.
import java.awt.*;
import javax.swing.*;
public class JSplitProblem extends JFrame {
private static final long serialVersionUID = 1L;
private JSplitPane mainSplittedPane;
public JSplitProblem() {
JPanel upperPanel = new JPanel();
upperPanel.setLayout(new BoxLayout(upperPanel, BoxLayout.X_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
JPanel red = new JPanel();
red.setBackground(Color.red);
leftPanel.add(red);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
JPanel blue = new JPanel();
blue.setBackground(Color.blue);
rightPanel.add(blue);
upperPanel.add(leftPanel);
upperPanel.add(rightPanel);
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.black);
mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel, bottomPanel);
mainSplittedPane.setOneTouchExpandable(true);
mainSplittedPane.setDividerLocation(0.5);
add(mainSplittedPane);
setPreferredSize(new Dimension(400, 300));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(true);
setVisible(true);
pack();
restoreDefaults();
}
private void restoreDefaults() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
mainSplittedPane.setDividerLocation(mainSplittedPane.getSize().height /2);
//mainSplittedPane.setDividerLocation(mainSplittedPane.getSize().width /2);
}
});
}
public static void main(String[] args) {
JSplitProblem jSplitProblem = new JSplitProblem();
}
}
I'm not sure, but I think you should try to pack() your frame. And if that doesn't work, try to reset the divider location after you packed the frame.
Just add the code below, and that will be fairly enough.
mainSplittedPane.setResizeWeight(0.5);