My plan is to get a similiar output, but for some reason, I am only getting the south panel...
My logic is to have 1 Main panel with North Center South.
In the North I will puth the Jlabel and Textfield and align it to the right.
In the Center I wil leave it empty
In the South I will Add a BoxLayout y-axis in the first row another panel with centered boxlayout
Another BoxLayout in the second row of the South BoxLayour row, I will add another Boxlayout and align it to the left.
Here is my code:
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setSize(new Dimension(500,600));
JPanel MainPanel = new JPanel();
frame.add(MainPanel);
JPanel NorthPanel = new JPanel(); //upper panel to add boxx layout and inside it 2 panls
JPanel ToPanel = new JPanel(); //inside north
JPanel SubjectPanel = new JPanel(); //inside north
NorthPanel.setLayout(new BoxLayout(NorthPanel, BoxLayout.Y_AXIS));
MainPanel.add(NorthPanel, BorderLayout.NORTH);
JLabel SubjectLabel = new JLabel("Subject"); SubjectLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
JTextField SubjectTextField = new JTextField(20); SubjectTextField.setAlignmentX(Component.RIGHT_ALIGNMENT);
JLabel ToLabel = new JLabel("To"); ToLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
JTextField ToTextField = new JTextField(20); ToTextField.setAlignmentX(Component.RIGHT_ALIGNMENT);
ToPanel.add(ToLabel);
ToPanel.add(ToTextField);
ToPanel.add(SubjectLabel);
ToPanel.add(SubjectTextField);
NorthPanel.add(ToPanel);
JPanel CenterPanel = new JPanel(); //Center panel blank
MainPanel.add(CenterPanel, BorderLayout.CENTER);
///
JPanel SouthPanel = new JPanel();
NorthPanel.setLayout(new BoxLayout(NorthPanel, BoxLayout.Y_AXIS));
JPanel FontPanels = new JPanel();
FontPanels.setLayout(new BoxLayout(FontPanels, BoxLayout.PAGE_AXIS));
FontPanels.add(new JButton("Bold"));
FontPanels.add(new JButton("Italic"));
FontPanels.add(new JButton("Underlined"));
FontPanels.add(new JButton("Undo"));
FontPanels.add(new JButton("Redo"));
FontPanels.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel OptionPanel = new JPanel();
OptionPanel.setLayout(new BoxLayout(OptionPanel, BoxLayout.PAGE_AXIS));
FontPanels.setLayout(new BoxLayout(FontPanels, BoxLayout.PAGE_AXIS));
OptionPanel.add(new JButton("Send"));
OptionPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
SouthPanel.add(FontPanels);
SouthPanel.add(OptionPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
You haven't added anything to the JFrame - so naturally it is displaying a blank JFrame.
For each component to appear, you'll need to do frame.add(component);
I leave the layout manager of the frame up to you.
Related
I am making a Java application with tabbed pane, I want some panes to have the same panel layout and structure, I don't want to clutter my code by writing the same code over and over again, so I created a method that returns a JPanel with a structure I want the pane to have.
I am initialising new variables and taking them to the method . My problem is that after I create a panel I can not do anything else in it because it doesn't show up. I can not add labels etc, etc (although if I add the label in the method it does show).
My question is it possible to somehow change the code I've written to make it possible to change it after the panel is returned?
JPanel panel2 = panel2(); // this code bit is in the constructor
JPanel mainPanel = new JPanel(); //Variables needed to create a panel
JPanel LeftPanel = new JPanel();
JPanel RightPanel = new JPanel();
JSplitPane splitPaneH = new JSplitPane();
JPanel panelTop = new JPanel();
JPanel panelBottom = new JPanel();
private JPanel panel2() {
JPanel newPanel = new JPanel();
CreateAPanel(newPanel, LeftPanel,RightPanel,splitPaneH, panelTop,panelBottom);
JLabel label = new JLabel ("lalala");
LeftPanel.add(label,BorderLayout.CENTER);
return newPanel;
}
private JPanel CreateAPanel(JPanel mainPanel, JPanel LeftPanel,JPanel RightPanel, JSplitPane splitPaneH, JPanel panelTop, JPanel panelBottom){
mainPanel.setPreferredSize(new Dimension(1100, 630));
mainPanel.setLayout(new BorderLayout());
LeftPanel = new JPanel();
RightPanel = new JPanel();
splitPaneH = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
panelTop = new JPanel();
panelBottom = new JPanel();
splitPaneH.setTopComponent(panelTop);
splitPaneH.setBottomComponent(panelBottom);
splitPaneH.setDividerLocation(300);
splitPaneH.setPreferredSize(new Dimension(800,630));
mainPanel.add(LeftPanel, BorderLayout.WEST);
mainPanel.add(RightPanel,BorderLayout.EAST);
LeftPanel.setBackground(Color.RED);
LeftPanel.setPreferredSize(new Dimension (300,630));
RightPanel.add(splitPaneH);
return mainPanel;
}
you do not use your return value...
your method CreateAPanel(...) creates the desired panel but you just don't use it
you should adjust your method panel2() in like this:
private JPanel panel2()
{
//JPanel newPanel = new JPanel(); don't create a new panel!
//CreateAPanel(newPanel, LeftPanel,RightPanel,splitPaneH, panelTop,panelBottom);
//instead do this:
JPanel newPanel = CreateAPanel(newPanel, LeftPanel,RightPanel,splitPaneH, panelTop,panelBottom);
JLabel label = new JLabel ("lalala");
LeftPanel.add(label,BorderLayout.CENTER);
return newPanel;
}
It's totally possible to add components to the Panel object afterwards. The only mistake that you have made is that "inside the method body you create new JPanel instances to replace with original param references" so when the method returns there is no effect on the original objects. I suggest doing something different as this:
private JPanel[] CreateAPanel(JPanel mainPanel)
{
mainPanel.setPreferredSize(new Dimension(1100, 630));
mainPanel.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane splitPaneH = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JPanel panelTop = new JPanel();
JPanel panelBottom = new JPanel();
splitPaneH.setTopComponent(panelTop);
splitPaneH.setBottomComponent(panelBottom);
splitPaneH.setDividerLocation(300);
splitPaneH.setPreferredSize(new Dimension(800,630));
mainPanel.add(leftPanel, BorderLayout.WEST);
mainPanel.add(rightPanel,BorderLayout.EAST);
leftPanel.setBackground(Color.RED);
leftPanel.setPreferredSize(new Dimension (300,630));
rightPanel.add(splitPaneH);
return new JPanel[]{mainPanel, leftPanel, rightPanel, panelTop, panelBottom};
}
If you want to change or add some more components inside result JPanel you get you can set names to all your components when you create them:
JPanel newPanel = new JPanel();
newPanel .setName("leftPanel");
resultPanel.add(newPanel, BorderLayout.WEST);
Then when you get resultPanel you can get it's components:
Component[] componentList = resultPanel.getContentPane().getComponents();
JPanel leftPanel = null;
for (Component component: componentList) {
if (Objects.equals(component.getName(), "leftPanel")) {
leftPanel = (JPanel) component;
}
}
if (leftPanel != null) {
// do something
}
I have main JPanel which is Borderlayout with added 4 JPANELS: NORTH(Green), WEST(Red), CENTER(Gray), SOUTH(Blue). I want to reduce width size of WEST(Red) Jpanel, or increase width size of Center(Grey) Jpanel.
Screenshot:
Here is my code:
frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// Main paneel
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10) );
frame.getContentPane().add(panel);
//West panel;
JPanel panelWest = new JPanel(new GridLayout(14,0,0,2));
panelWest.setPreferredSize(new Dimension(300, 300));
panelWest.setBorder( BorderFactory.createEmptyBorder(100,0,0,0) );
panel.add(panelWest, BorderLayout.WEST);
panelWest.setBackground(Color.red);
for (int i = 0; i < MAX_TABLES; i++) {
buttonsTables[i] = new JButton(tables[i]);
buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
panelWest.add(buttonsTables[i]);
panelWest.add(Box.createVerticalStrut(10));
}
//South panel;
JPanel southPanel = new JPanel(); // Donji layout za dugmice
southPanel.setBorder( BorderFactory.createEmptyBorder(20,0,0,0) );
panel.add(southPanel, BorderLayout.SOUTH);
southPanel.setBackground(Color.BLUE);
JButton buttonDodaj = new JButton("Dodaj");
southPanel.add(buttonDodaj);
JButton buttonIzmeni = new JButton("Izmeni");
southPanel.add(buttonIzmeni);
JButton butonObrisi = new JButton("Obrisi");
southPanel.add(butonObrisi);
//North panel;
JPanel northPanel = new JPanel(); // Donji layout za dugmice
northPanel.setBorder( BorderFactory.createEmptyBorder(0,10,0,0) );
panel.add(northPanel, BorderLayout.NORTH);
northPanel.setBackground(Color.green);
JButton buttonImport = new JButton("Importuj fajl");
buttonImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
importActionPerformed(evt);
}
});
northPanel.add(buttonImport, BorderLayout.WEST);
JButton ButtonRecord = new JButton("Snimi fajl");
northPanel.add(ButtonRecord, BorderLayout.WEST);
// Central panel
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.setBackground(Color.GRAY);
panel.add(centerPanel, BorderLayout.CENTER);
I want to reduce width size of WEST(Red) Jpanel
panelWest.setBorder( BorderFactory.createEmptyBorder(100,0,0,0) );
So why is the width of your Border so large?
A Border is for "extra" space around the components.
So the width of your panel is the width of the buttons plus the width of the border.
Edit:
panelWest.setPreferredSize(new Dimension(300, 300));
Don't hardcode a preferred size. The layout manager will calculate the size based on the above logic. Get rid of that statement.
Edit 2:
// buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
Get rid of any logic that attempts to control the size of a component. The point of using layout managers is to let the layout manager do the size calcualtions.
So for your buttons panel you need to nest panels to prevent the buttons from taking all the space.
You can do something like:
JPanel wrapper = new JPanel();
wrapper.add(buttonsPanel);
...
//panel.add(panelWest, BorderLayout.WEST);
panel.add(wrapper, BorderLayout.WEST);
By default a JPanel uses a FlowLayout which will respect the preferred size of any component added to it.
Another option is to use a GridBagLayout with the wrapper panel. By default the panel will then be displayed in the "center" of the available space. So it will be vertically centered and you won't need the EmptyBorder.
I have following source code...Can someone please give me an advice how to add jscrollpane onto jframe? I tried several time to add it to jframe but without any progress. It is not even showing.
public class Form3 {
JFrame jframe = new JFrame("Etiket print.");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
Color myBlue1Color = new Color(168, 175, 247);
Color myBlue2Color = new Color(139, 146, 255);
public Form3(){
jframe.setMinimumSize(new Dimension(1280, 1000));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setAutoscrolls(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//---------------------------------Header
panel1 = createSquareJPanel(Color.YELLOW, 600,200);
panel3 = createSquareJPanel(Color.GREEN, 400,200);
panel4 = createSquareJPanel(Color.white, 280,200);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
{
for (int i=0; i<25; i++){
JPanel harnessPanel= new JPanel();
harnessPanel.setMinimumSize(new Dimension(1280, 70));
harnessPanel.setMaximumSize(new Dimension(1280, 70));
harnessPanel.setPreferredSize(new Dimension(1280, 70));
if(i%2==0) {
harnessPanel.setBackground(myBlue1Color);
}
else {
harnessPanel.setBackground(myBlue2Color);
}
panel2.add(harnessPanel);
harnessPanel.repaint();
harnessPanel.validate();
}
panel2.repaint();
panel2.validate();
}
jframe.add(scrollFrame);
jframe.add(container);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
}
private JPanel createSquareJPanel(Color color, int size1, int size2)
{
JPanel tempPanel = new JPanel();
tempPanel.setBackground(color);
tempPanel.setMinimumSize(new Dimension(size1, size2));
tempPanel.setMaximumSize(new Dimension(size1, size2));
tempPanel.setPreferredSize(new Dimension(size1, size2));
return tempPanel;
}
public static void main (String args[]){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Form3 myF=new Form3();
}
});
};
}
picture of my app:
actual state:
JFrame uses a BorderLayout by default. It's default position (if you don't specify one) is CENTER.
BorderLayout will only allow one component to occupy any of it's 5 available positions.
So when you do...
jframe.add(scrollFrame);
jframe.add(container);
It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn't actually remove it, but the result is just about the same).
Try supplying a position constraint. For example...
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.SOUTH);
See How to use BorderLayout for more details
I want to place two labels in the middle of the center of a window. I get it working with 1 label and the following code:
Screenshot: http://abload.de/img/scr1g6u0f.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("center1");
centerPanel.add(label, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Now I want another label next to the first label. I tried to use a flowlabel, but they are placed at the top of the BorderLayout.CENTER
Screenshot: http://abload.de/img/scr2a3u26.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Thanks!
Use a GridBagLayout without constraints:
JPanel centerPanel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel);
I have following source code...Can someone please give me an advice how to add jscrollpane onto jframe? I tried several time to add it to jframe but without any progress. It is not even showing.
public class Form3 {
JFrame jframe = new JFrame("Etiket print.");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
Color myBlue1Color = new Color(168, 175, 247);
Color myBlue2Color = new Color(139, 146, 255);
public Form3(){
jframe.setMinimumSize(new Dimension(1280, 1000));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setAutoscrolls(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//---------------------------------Header
panel1 = createSquareJPanel(Color.YELLOW, 600,200);
panel3 = createSquareJPanel(Color.GREEN, 400,200);
panel4 = createSquareJPanel(Color.white, 280,200);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
{
for (int i=0; i<25; i++){
JPanel harnessPanel= new JPanel();
harnessPanel.setMinimumSize(new Dimension(1280, 70));
harnessPanel.setMaximumSize(new Dimension(1280, 70));
harnessPanel.setPreferredSize(new Dimension(1280, 70));
if(i%2==0) {
harnessPanel.setBackground(myBlue1Color);
}
else {
harnessPanel.setBackground(myBlue2Color);
}
panel2.add(harnessPanel);
harnessPanel.repaint();
harnessPanel.validate();
}
panel2.repaint();
panel2.validate();
}
jframe.add(scrollFrame);
jframe.add(container);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
}
private JPanel createSquareJPanel(Color color, int size1, int size2)
{
JPanel tempPanel = new JPanel();
tempPanel.setBackground(color);
tempPanel.setMinimumSize(new Dimension(size1, size2));
tempPanel.setMaximumSize(new Dimension(size1, size2));
tempPanel.setPreferredSize(new Dimension(size1, size2));
return tempPanel;
}
public static void main (String args[]){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Form3 myF=new Form3();
}
});
};
}
picture of my app:
actual state:
JFrame uses a BorderLayout by default. It's default position (if you don't specify one) is CENTER.
BorderLayout will only allow one component to occupy any of it's 5 available positions.
So when you do...
jframe.add(scrollFrame);
jframe.add(container);
It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn't actually remove it, but the result is just about the same).
Try supplying a position constraint. For example...
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.SOUTH);
See How to use BorderLayout for more details