I have created a set of buttons that I want to be at the top of every page of my application.
Rather than having to recreate the setup in every class, is it possible to create it once and include it in every class, similar to a reusable view in Android.
My code for the button setup is below:
public Buttons() {
setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(10, 11, 89, 23);
add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
btnNewButton_1.setBounds(101, 11, 89, 23);
add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
btnNewButton_2.setBounds(192, 11, 89, 23);
add(btnNewButton_2);
textField_2 = new JTextField();
textField_2.setBounds(104, 42, 86, 20);
add(textField_2);
textField_2.setColumns(10);
System.out.println("HELLO");
}
My code to create the class is below:
public static void gui(){
frame = new JFrame("name");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLayout(new BorderLayout());
frame.add(new Buttons(), BorderLayout.WEST);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setBounds(200, 0, 500, 500);
}
I have tried adding the following line to this method to add a second set of buttons:
frame.add(new Extras(), BorderLayout.CENTER);
but it only adds the second object to the display.
Create a class (or method) that creates a JPanel which holds all those buttons you want. Then, just add a new instance of that JPanel to your GUI. Pseudo-code:
public JPanel createButtons() {
JPanel panel = new JPanel();
// add buttons to panel
return panel;
}
public void gui() {
frame = new JFrame("name");
frame.setLayout(new BorderLayout());
frame.add(createButtons(), BorderLayout.WEST);
// ...
}
Related
Hi I am trying to make a GUI using Javax to look something like this.
But currently it looks like this
This is my code
class Intro extends JFrame implements ActionListener
{
JFrame frame = new JFrame();
JButton ok;
JLabel background;
JLabel demo;
public Intro()
{
frame.setTitle("Let us start");
frame.setSize(600,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JLabel(new ImageIcon("welcome.gif")));
frame.setLayout(new FlowLayout());
background = new JLabel();
frame.add(background);
frame.setSize(500,400);
ok = new JButton("OK");
demo = new JLabel("CSIT 121 Demo System", SwingConstants.RIGHT);
frame.add(demo);
ok.setHorizontalTextPosition(JButton.CENTER);
ok.setVerticalTextPosition(JButton.BOTTOM);
frame.add(ok);
}
}
What do i need to modify?
You should use another layout. Here is the correct example:
public class Intro extends JFrame {
JFrame frame = new JFrame();
JButton ok;
JLabel background;
JLabel demo;
public Intro() {
frame.setTitle("Let us start");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel contentPane = new JLabel(new ImageIcon("welcome.gif"));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setContentPane(contentPane);
frame.setLayout(new BorderLayout());
background = new JLabel();
frame.add(background); // why you need it??? it has no visual effect here
frame.setSize(500, 400);
ok = new JButton("OK");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(ok);
frame.add(buttonPanel, BorderLayout.PAGE_END);
demo = new JLabel("CSIT 121 Demo System"); // probably you need to change foreground of label to fit your background image
frame.add(demo, BorderLayout.LINE_END);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Intro::new);
}
}
I was working on a java swing gui project for my course. When I was doing that, I found that I had too much information in a panel and it was not able to show everything. As a result, I wanted to add a Jscrollpane and I did some research about how to use this function but it didn't seem to work for my project, and I had no reason why even after I tried almost everything I could find on google.
Here is my code:
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(10, 1100));
musiclist.setBounds(0, 0, 190, 1000);
musiclist.setLayout(null);
listpanel.setLayout(null);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
listpanel.add(selectButton);
c.add(musiclist, BorderLayout.WEST);
frame.setVisible(true);
musiclist.setVisible(true);
The problem now is that the scroll bar never shows up even I set the vertical to always show. I am just new to java, so any help will be helpful and thank you all for your time!
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(100, 1100));
musiclist.setBounds(0, 0, 190, 1000);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
JList mlist=new JList();
DefaultListModel lmodel=new DefaultListModel();
lmodel.addElement("Song 1");
lmodel.addElement("Song 2");
lmodel.addElement("Song 3");
mlist.setModel(lmodel);
listpanel.setLayout(new BorderLayout());
listpanel.add(mlist, BorderLayout.CENTER);
listpanel.add(selectButton, BorderLayout.SOUTH);
c.add(musiclist, BorderLayout.WEST);
musiclist.setVisible(true);
frame.setVisible(true);
I try to set background color to red for JFrame while keeping white color to JPanel.
But it doesnt work with setLayout somehow
private void buildGraphics(){
JFrame frame = new JFrame();
setTitle("Application");
setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
frame.setBackground(Color.red);
JPanel panel = new JPanel();
panel.setBounds(50, 50, 500, 70);
panel.setBackground(Color.white);
panel.setBorder(BorderFactory.createTitledBorder("Click to choose..."));
panel.add(button1);
panel.add(button2);
panel.add(button3);
getContentPane().add(panel);
}
You already have a frame (your class is extending JFrame, so it IS a JFrame) and buildGraphics() has to build the existing frame you don't have to create a new one:
private void buildGraphics() {
this.setTitle("Application");
this.setBounds(100, 100, 600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.setBackground(Color.red);
JPanel panel = new JPanel();
panel.setBounds(50, 50, 500, 70);
panel.setBackground(Color.white);
panel.setBorder(BorderFactory.createTitledBorder("Click to choose..."));
panel.add(button1);
panel.add(button2);
panel.add(button3);
this.getContentPane().add(panel);
}
Ok, I now created another JPanel and put my first one inside to control background color
JFrame frame = new JFrame();
setTitle(" Application");
setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JPanel panelMain = new JPanel();
panelMain.setBounds(5, 5, 595, 395);
panelMain.setBackground(Color.red);
getContentPane().add(panelMain);
JPanel panel = new JPanel();
panel.setBounds(50, 50, 500, 70);
panel.setBackground(Color.white);
panel.setBorder(BorderFactory.createTitledBorder("What would you like to do?"));
panel.add(button1);
panel.add(button2);
panel.add(button3);
panelMain.add(panel);
I am a beginner and I was practicing JRadioButtons. I realised that I can't see my JRadioButtons if I'll not set my layout as 'FlowLayout()'. I want to set the location of the buttons by myself.
I posted my code below, can anyone help me what am I doing wrong?
Thanks!
private JFrame frame;
private JPanel panel;
private JRadioButton btn1, btn2;
public JBButtons() {
form();
radioButtons();
frame.add(panel);
frame.setVisible(true);
}
public void form(){
frame = new JFrame("Frame");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
//panel.setLayout(new FlowLayout());
}
public void radioButtons() {
ButtonGroup group = new ButtonGroup();
btn1 = new JRadioButton("btn1");
btn1.setSelected(true);
btn1.setLocation(50, 50);
btn2 = new JRadioButton("btn2");
btn2.setLocation(50, 70);
group.add(btn1);
group.add(btn2);
panel.add(btn1);
panel.add(btn2);
}
public static void main(String[] args) {
new JBButtons();
}
The issue with absolute positioning or null layout is - it requires you to set the sizes of all your components, otherwise they will stay their default zero-size and won't show up. Better use a layout manager - https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Add this part of the code when adding the RadioButton to the panel, use the property .setBounds(x, y, width, heigth).
panel.add(btn1);
btn1.setBounds(90, 59, 93, 23);
panel.add(btn2);
btn2.setBounds(180, 60, 93, 23);
I am having one main JPanel in which I had used several other panels. I want to add a scroll pane to my panel so that when child panel go out of frame they should be scrollable, but I am unable to achieve this.
How to achieve correct scrolling functionality for panels within a panel?
Code inside my child panel:
public class Page13111SubPanel extends JPanel {
/**
* Create the panel.
*/
public Page13111SubPanel() {
// setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
setSize(1000,130);
setLayout(new GridLayout(3, 3, 40, 35));
JLabel label= new JLabel();
label.setText("Vehicle Companies:asdddddddddddddddd1");
label.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label);
JLabel label1= new JLabel();
label1.setText("Vehicle Companies:asdddddddddddddddd2");
label1.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label1);
JLabel label2= new JLabel();
label2.setText("Vehicle Companies:asdddddddddddddddd3");
label2.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label2);
JLabel label3= new JLabel();
label3.setText("Vehicle Companies:asdddddddddddddddd4");
label3.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label3);
// JLabel label3= new JLabel();
// label3.setText("Vehicle Companies:asdddddddddddddddd4");
// label3.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
// add(label3);
JLabel label4= new JLabel();
label4.setText("Vehicle Companies:asdddddddddddddddd5");
label4.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label4);
JLabel label5= new JLabel();
label5.setText("Vehicle Companies:asdddddddddddddddd6");
label5.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label5);
JLabel label6= new JLabel();
label6.setText("Vehicle Companies:asdddddddddddddddd7");
label6.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label6);
}
}
Code inside my main panel:
public class Page1311Test extends JPanel {
// private JTable table;
public JScrollPane pane=null;
public JPanel panel=null;
public JButton back=null;
/**
* Create the panel.
*/
public Page1311Test() {
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(7,1,1,40));
Page13111SubPanel[] panel1=new Page13111SubPanel[7];
panel1[0]=new Page13111SubPanel();
panel.add(panel1[0]);
panel1[1]=new Page13111SubPanel();
panel.add(panel1[1]);
panel1[2]=new Page13111SubPanel();
panel.add(panel1[2]);
panel1[3]=new Page13111SubPanel();
panel.add(panel1[3]);
panel1[4]=new Page13111SubPanel();
panel.add(panel1[4]);
panel1[5]=new Page13111SubPanel();
panel.add(panel1[5]);
panel1[6]=new Page13111SubPanel();
panel.add(panel1[6]);
pane=new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(pane);
}
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.setLocation(0, 0);
frame.add(new Page1311Test());
frame.setVisible(true);
}
}
Changing the main panel from FlowLayout (the default) to a layout that uses the child components of the panel to fill the available space (used GridLayout here) solves the problem. E.G.
import java.awt.*;
import javax.swing.*;
public class Page1311Test extends JPanel {
public JScrollPane pane = null;
public JPanel panel = null;
/**
* Create the panel.
*/
public Page1311Test() {
setLayout(new GridLayout());
//JPanel panel = new JPanel(); //shadowed class attribute
panel = new JPanel(new GridLayout(0, 1, 1, 40));
for (int ii = 0; ii < 17; ii++) {
panel.add(new Page13111SubPanel(ii));
}
pane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(pane);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.setLocation(0, 0);
frame.add(new Page1311Test());
frame.pack();
frame.setVisible(true);
}
}
class Page13111SubPanel extends JPanel {
/**
* Create the panel.
*/
public Page13111SubPanel(int num) {
//setSize(1000, 130); // Don't set sizes using 'magic numbers'
setLayout(new GridLayout(3, 3, 40, 35));
add(new JLabel(num + " Vehicle Companies:asdddddddddddddddd1"));
add(new JLabel("Vehicle Make:"));
add(new JLabel("Vehicle Model"));
add(new JLabel("Vehicle Number"));
add(new JLabel("Vehicle Driver:"));
add(new JLabel("Vehicle Wheels"));
add(new JLabel("Vehicle Air Con"));
}
}
I am guessing the problem lies in your GridLayout and setSize calls. When you call, essentially your layout manager is going to overwrite you setSize call.
In the following example, I set the preferred and minimum sizes and the scroll pane works fine.
JFrame frame = new JFrame("Grids of Grids");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
for(int i = 0; i<9; i++){
JPanel localGrid = new JPanel();
localGrid.setMinimumSize(new Dimension(250, 250));
localGrid.setPreferredSize(new Dimension(250, 250));
localGrid.setLayout(new GridLayout(5, 5));
for(int j = 0; j<25; j++){
localGrid.add(new JLabel("" + i + ":: " + j));
}
panel.add(localGrid);
}
JScrollPane scroll = new JScrollPane(panel);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Otherwise, your scrollable area will actuall be pretty small because the layouts will try to fit it into the available space.
I think your problem is that the main Panel is not scrollable, therefore you cannot scroll at all.
Try using a JScrollPane there and take a look at this tutorial to get more information about JScrollPanes. There are also lots of information about other GUI-Objects like Frames and Panels, so I think you can find a solution for your problem!