How to add 2 columns of checkboxes to Jpanel - java

I want to add several (about 8) checkboxes in 2 columns below the string and the textbox and above the buttons "Aceptar" and "Cancelar"
I have tried to do a Jpanel inside another Jpanel, but i does not work since the subpanel does not align where i want to (or i dont know how to align it)
JLabel texto_titulo_qliksense = new JLabel();
texto_titulo_qliksense.setText("Nombre del cuadro de mando :");
texto_titulo_qliksense.setBounds(20, 50, 700, 25);
JCheckBox checkBox1 = new JCheckBox("Stream 1");
checkBox1.setBounds(75,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Stream 2");
checkBox2.setBounds(10,150, 50,50);
textField = new JTextField(80);
textField.setBounds(200,53,220,20);
JLabel texto3 = new JLabel();
texto3.setText("Usuario: " + usuario);
JButton aceptar = new JButton();
aceptar.setText("Aceptar");
aceptar.setActionCommand(comandoAceptar);
aceptar.setBounds(90, 250, 100, 30);
aceptar.addActionListener(this);
JButton cancelar = new JButton();
cancelar.setText("Cancelar");
cancelar.setActionCommand(comandoCancelar);
cancelar.setBounds(250, 250, 100, 30);
cancelar.addActionListener(this);
panel = new JPanel();
panel.setBorder(null);
subpanel = new JPanel();
subpanel.setBorder(BorderFactory.createTitledBorder("Subpanel 1"));
subpanel.setBounds(250, 450, 250, 250);
subpanel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
subpanel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
subpanel.setVisible(true);
panel.setBounds(250, 100, 1000, 1000);

its a very small program, wont be easier just to use absolute positioning?
Easier than this?
JPanel ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(40,4,40,4));
JPanel pageStartPanel = new JPanel(new FlowLayout());
ui.add(pageStartPanel, BorderLayout.PAGE_START);
pageStartPanel.add(new JLabel("Nombre del cuadro de mando:"));
pageStartPanel.add(new JTextField(12));
JPanel checkPanel = new JPanel(new GridLayout(0,2));
ui.add(checkPanel, BorderLayout.CENTER);
for (int ii=1; ii<9; ii++) {
checkPanel.add(new JCheckBox("Checkbox " + ii));
}
JOptionPane.showConfirmDialog(
null,
ui,
"Exportar CdM de Qliksense",
JOptionPane.OK_CANCEL_OPTION);

Related

Why after I added a JComboBox into one of the JPanels, all JPanels were not rendered

When the JComboBox, cmbox was not added to a JPanel, two panels, p1 & p2 could be rendered. You may comment out the combo box portion to see the result. But after I added the combo box into one of the panels, all panels were not rendered.
My code is like the following:
import java.awt.*;
import javax.swing.*;
public class TestCombo {
public static void main(String[] args) {
JFrame frame = new JFrame("康樂彩歌");
frame.setVisible(true);
frame.setBounds(0, 0, 1368, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.CYAN);
JRadioButton rb1 = new JRadioButton("加簡譜", false);
rb1.setFont(new Font("新細明體", Font.PLAIN, 20));
JRadioButton rb2 = new JRadioButton("加人聲", false);
rb2.setFont(new Font("新細明體", Font.PLAIN, 20));
rb1.setBounds(450, 180, 50, 50);
rb2.setBounds(500, 180, 50, 50);
JButton btPlay = new JButton("PLAY");
btPlay.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
btPlay.setBounds(100, 20, 100, 20);//x axis, y axis, width, height
JButton btStop = new JButton("STOP");
btStop.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
btStop.setBounds(140, 20, 100, 20);//x axis, y axis, width, height
//p1.add(cmbox);
p1.add(rb1);
p1.add(rb2);
p1.add(btPlay);
p1.add(btStop);
p1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setBorder(BorderFactory.createLineBorder(Color.red));
JComboBox cmbox = new JComboBox(); //The JComboBox to be added to a JPanel
cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));
cmbox.addItem("紫竹調");
cmbox.addItem("走一同去郊遊");
cmbox.addItem("大野狼");
cmbox.addItem("歸來吧蘇連多");
cmbox.addItem("追尋");
cmbox.addItem("三輪車");
cmbox.addItem("我家門前有小河");
cmbox.addItem("漁家樂");
cmbox.addItem("嚕啦啦");
cmbox.addItem("踏雪尋梅");
p2.add(cmbox);
frame.add(p1, BorderLayout.PAGE_START);
frame.add(p2, BorderLayout.CENTER);
}
}
Implementing the changes as detailed below the code, solves the problem.
import java.awt.*;
import javax.swing.*;
public class TestCombo {
public static void main(String[] args) {
JFrame frame = new JFrame("康樂彩歌");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.CYAN);
JRadioButton rb1 = new JRadioButton("加簡譜", false);
rb1.setFont(new Font("新細明體", Font.PLAIN, 20));
JRadioButton rb2 = new JRadioButton("加人聲", false);
rb2.setFont(new Font("新細明體", Font.PLAIN, 20));
JButton btPlay = new JButton("PLAY");
btPlay.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
JButton btStop = new JButton("STOP");
btStop.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
p1.add(rb1);
p1.add(rb2);
p1.add(btPlay);
p1.add(btStop);
p1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setBorder(BorderFactory.createLineBorder(Color.red));
JComboBox cmbox = new JComboBox(); //The JComboBox to be added to a JPanel
cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));
cmbox.addItem("紫竹調");
cmbox.addItem("走一同去郊遊");
cmbox.addItem("大野狼");
cmbox.addItem("歸來吧蘇連多");
cmbox.addItem("追尋");
cmbox.addItem("三輪車");
cmbox.addItem("我家門前有小河");
cmbox.addItem("漁家樂");
cmbox.addItem("嚕啦啦");
cmbox.addItem("踏雪尋梅");
p2.add(cmbox);
frame.add(p1, BorderLayout.PAGE_START);
frame.add(p2, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
btPlay.setBounds(100, 20, 100, 20); Do not set the bounds of components. Let the layouts (padding and borders) do their job.
frame.setBounds(0, 0, 1368, 500); That's just a guess, and if it's the right guess on one OS, it will be the wrong guess on others. Instead pack() the window after components are added.
Sidebar: GUIs should be started on the EDT. (Not implemented above: 'batteries not included'.)

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:-

JScrollBar is not working

I'm trying to add a JScrollPane to JTextArea, but when I add it, JPane is not showing my textarea anymore. Without JScrollPane it shows it, but then I can't display all the information retrieved from a file.
This is my code for one of textAreas which I want to be wrapped with JScrollPane.
public GUI_CWK()
{
//frame details
setSize(450,400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("H.o. | Location Database");
setExtendedState(JFrame.MAXIMIZED_BOTH);
getContentPane().setBackground(Color.YELLOW);
// Useing Lable to set a title for multiple buttons
lbl2 = new JLabel();
lbl2.setSize(420,85);
lbl2.setLocation(15,0);
lbl2.setEnabled(false);
lbl2.setFont(new Font ("arial",5,17));
lbl2.setText("Options to Manipulate with Data");
add(lbl2);
// Adding buttons to JFrame
btn1 = new JButton("Add new entry");
btn1.setSize(120,20);
btn1.setLocation(7,80);
btn1.addActionListener(this);
add(btn1);
btn2 = new JButton("Search");
btn2.setSize(120,20);
btn2.setLocation(7,110);
btn2.addActionListener(this);
add(btn2);
btn3 = new JButton("Update Entry");
btn3.setSize(120,20);
btn3.setLocation(150,110);
btn3.addActionListener(this);
add(btn3);
btn4 = new JButton("Print All");
btn4.setSize(120,20);
btn4.setLocation(150,80);
btn4.addActionListener(this);
add(btn4);
btn5 = new JButton("Print Arrays");
btn5.setSize(120,20);
btn5.setLocation(7,140);
btn5.addActionListener(this);
add(btn5);
btn6 = new JButton("Delete Entry");
btn6.setSize(120,20);
btn6.setLocation(300,80);
btn6.addActionListener(this);
add(btn6);
btn7 = new JButton("Delete ALL");
btn7.setSize(120,20);
btn7.setLocation(300,110);
btn7.addActionListener(this);
add(btn7);
lbl1 = new JLabel();
lbl1.setSize(420,85);
lbl1.setLocation(15,190);
lbl1.setEnabled(false);
lbl1.setFont(new Font ("arial",5,17));
lbl1.setText("Extra Options");
add(lbl1);
btn8 = new JButton("Sort Data");
btn8.setSize(160,20);
btn8.setLocation(7,265);
btn8.addActionListener(this);
add(btn8);
btn9 = new JButton("Open Text file");
btn9.setSize(160,20);
btn9.setLocation(7,300);
btn9.addActionListener(this);
add(btn9);
btn11 = new JButton("Retrieve from text file");
btn11.setSize(160,20);
btn11.setLocation(240,265);
btn11.addActionListener(this);
add(btn11);
btn12 = new JButton("Exit Program");
btn12.setSize(160,20);
btn12.setLocation(240,300);
btn12.addActionListener(this);
add(btn12);
lbl3 = new JLabel();
lbl3.setSize(420,85);
lbl3.setLocation(15,340);
lbl3.setEnabled(false);
lbl3.setFont(new Font ("arial",5,17));
lbl3.setText("Terminal Window");
add(lbl3);
// Adding textArea to display each user entered value
textArea2 = new JTextArea(5, 20);
textArea2.setEditable(false);
textArea2.setSize(440,300);
textArea2.setLocation(7,400);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial",5,14));
sp2 = new JScrollPane(textArea2);
sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp2);
lbl4 = new JLabel();
lbl4.setSize(420,40);
lbl4.setLocation(570,10);
lbl4.setEnabled(false);
lbl4.setFont(new Font ("arial",5,17));
lbl4.setText("Data Display");
add(lbl4);
textArea = new JTextArea(55, 50);
textArea.setEditable(false);
textArea.setSize(800,640);
textArea.setLocation(560,60);
textArea.setFont(new Font("arial",5,19));
textArea.setDisabledTextColor(Color.black);
sp = new JScrollPane(textArea);
sp.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp);
setVisible(true);
}
I think that this should solve your problem.
You'll notice that I erased the thiss between the addActionListener() methods because you have to specify what do you want the ActionListeners to do.
StartingPoint (main) class:
public class StartingPoint{
public static void main(String[] args){
GUI_CWK gui_cwk = new GUI_CWK(); //Creates a new instance of "GUI_CWK".
gui_cwk.setVisible(true); //You set the gui_cwk to visible.
}
}
GUI_CWK class:
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class GUI_CWK extends JFrame{
private static final long serialVersionUID = 1L;
public GUI_CWK() {
// frame details
this.setSize(450, 400);
JPanel panel = new JPanel();
this.setLayout(null); /* Each time that you want to refer to the classyou are in, you say "this" */
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("H.o. | Location Database");
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.getContentPane().setBackground(Color.YELLOW);
// Using labels to set a title for multiple buttons
JLabel lbl2 = new JLabel(); /*Remember that you have to create the instances of an object by naming the class before the name of the instance */
lbl2.setSize(420, 85);
lbl2.setLocation(15, 0);
lbl2.setEnabled(false);
lbl2.setFont(new Font("arial", 5, 17));
lbl2.setText("Options to Manipulate with Data");
panel.add(lbl2);
// Adding buttons to JFrame
JButton btn1 = new JButton("Add new entry");
btn1.setSize(120, 20);
btn1.setLocation(7, 80);
btn1.addActionListener(/* Insert ActionListener */);
panel.add(btn1); //The button is added to the panel, so then the last one can be added to the JFrame at the end.
JButton btn2 = new JButton("Search");
btn2.setSize(120, 20);
btn2.setLocation(7, 110);
btn2.addActionListener(/* Insert ActionListener */);
panel.add(btn2);
JButton btn3 = new JButton("Update Entry");
btn3.setSize(120, 20);
btn3.setLocation(150, 110);
btn3.addActionListener(/* Insert ActionListener */);
panel.add(btn3);
JButton btn4 = new JButton("Print All");
btn4.setSize(120, 20);
btn4.setLocation(150, 80);
btn4.addActionListener(/* Insert ActionListener */);
panel.add(btn4);
JButton btn5 = new JButton("Print Arrays");
btn5.setSize(120, 20);
btn5.setLocation(7, 140);
btn5.addActionListener(/* Insert ActionListener */);
panel.add(btn5);
JButton btn6 = new JButton("Delete Entry");
btn6.setSize(120, 20);
btn6.setLocation(300, 80);
btn6.addActionListener(/* Insert ActionListener */);
panel.add(btn6);
JButton btn7 = new JButton("Delete ALL");
btn7.setSize(120, 20);
btn7.setLocation(300, 110);
btn7.addActionListener(/* Insert ActionListener */);
panel.add(btn7);
JLabel lbl1 = new JLabel();
lbl1.setSize(420, 85);
lbl1.setLocation(15, 190);
lbl1.setEnabled(false);
lbl1.setFont(new Font("arial", 5, 17));
lbl1.setText("Extra Options");
panel.add(lbl1);
JButton btn8 = new JButton("Sort Data");
btn8.setSize(160, 20);
btn8.setLocation(7, 265);
btn8.addActionListener(/* Insert ActionListener */);
panel.add(btn8);
JButton btn9 = new JButton("Open Text file");
btn9.setSize(160, 20);
btn9.setLocation(7, 300);
btn9.addActionListener(/* Insert ActionListener */);
panel.add(btn9);
JButton btn11 = new JButton("Retrieve from text file");
btn11.setSize(160, 20);
btn11.setLocation(240, 265);
btn11.addActionListener(/* Insert ActionListener */);
panel.add(btn11);
JButton btn12 = new JButton("Exit Program");
btn12.setSize(160, 20);
btn12.setLocation(240, 300);
btn12.addActionListener(/* Insert ActionListener */);
panel.add(btn12);
JLabel lbl3 = new JLabel();
lbl3.setSize(420, 85);
lbl3.setLocation(15, 340);
lbl3.setEnabled(false);
lbl3.setFont(new Font("arial", 5, 17));
lbl3.setText("Terminal Window");
panel.add(lbl3);
// Adding textArea to display each user entered value
JTextArea textArea2 = new JTextArea(5, 20);
textArea2.setEditable(false);
textArea2.setSize(440, 300);
textArea2.setLocation(7, 400);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial", 5, 14));
JScrollPane sp2 = new JScrollPane(textArea2);
sp2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea2.add(sp2); //Here you add the scrollPanel to the textArea,
//in the original code you were adding it to the frame
panel.add(textArea2);
JLabel lbl4 = new JLabel();
lbl4.setSize(420, 40);
lbl4.setLocation(570, 10);
lbl4.setEnabled(false);
lbl4.setFont(new Font("arial", 5, 17));
lbl4.setText("Data Display");
panel.add(lbl4);
JTextArea textArea = new JTextArea(55, 50);
textArea.setEditable(false);
textArea.setSize(800, 640);
textArea.setLocation(560, 60);
textArea.setFont(new Font("arial", 5, 19));
textArea.setDisabledTextColor(Color.black);
JScrollPane sp1 = new JScrollPane(textArea);
sp1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setVisible(true);
textArea.add(sp1);
panel.add(textArea);
this.add(panel); //Everything which was added to the panel now is added to the frame.
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Here is an example of using a scroll pane.
import javax.swing.*;
import java.awt.*;
public class ScrollPaneExample{
public void buildGui(){
JFrame frame = new JFrame("button and scrollpane");
JButton button = new JButton("example");
JTextArea area = new JTextArea(20, 20);
//this is where we will place our components.
//It has a flow layout by default.
JPanel content = new JPanel();
JScrollPane sp = new JScrollPane(area);
content.add(button);
content.add(sp);
//this line limits the size of the scroll pane.
sp.setPreferredSize(new Dimension(400, 60));
//The frame will use our panel instead of the default one.
frame.setContentPane(content);
//sizes the frame to the content area.
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args){
EventQueue.invokeLater(()->{new ScrollPaneExample().buildGui();});
}
}
The main difference is that I set my content pane so I know its layout manager. You appear to be manually setting all of the sizes, which I am surprised it works. You should choose a good layout manager. pick one you think does what you want.
Also note that this is a minimal example that will compile.
Thank you for help guys, I found out the answer. As #matt told me, I should add size and location to JScrollPane instead of textArea. So I did it and it works for me now.
code is below.
textArea2 = new JTextArea(5,20);
textArea2.setEditable(false);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial",5,14));
sp2 = new JScrollPane(textArea2);
sp2.setSize(440,300);
sp2.setLocation(7,400);
sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp2);

GUI Layout for User Interface

I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}

java boxlayout space after adding jpanel

I have container jpanel in which i used a boxlayout manager and what i do is i add another panels inside in which the added panel contains a label and textfield using flowlayout manager. everytime i add a panel inside it creates an annoying big space after another added panel. I want to reduce the spacing of the panels i have tried using setsize and setpreferredsize method to adjust it. Here is my code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500,312));
global.setSize(500,312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
//Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
global.add(new JSeparator());
//Name Field
JPanel c = new JPanel();
c.setSize(100, 1);
c.setPreferredSize(new Dimension(100,1));
c.setLayout(new FlowLayout());
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20,25));
c.add(label);
c.add(text);
global.add(c);
//Hostname Field
JPanel c1 = new JPanel();
c1.setSize(100, 1);
c1.setPreferredSize(new Dimension(100,1));
c1.setLayout(new FlowLayout());
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20,25));
c1.add(label2);
c1.add(text1);
global.add(c1);
BoxLayout is a pretty aggressive LayoutManager and doesn't always honour the preferred size of components within it. Instead, we must set the maximum size of BoxLayout components to prevent them from being stretched. Additionally, we need to add a Box via Box.createVerticalGlue() - this is special component that gets stretched (rather than the other components).
Here is the rewritten code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500, 312));
global.setSize(500, 312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
// Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
JSeparator sep = new JSeparator();
sep.setMaximumSize(new Dimension((int) sep.getMaximumSize().getWidth(), 50));
global.add(sep);
// Name Field
JPanel c = new JPanel();
c.setMaximumSize(new Dimension((int) c.getMaximumSize().getWidth(), 50));
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20, 25));
c.add(label);
c.add(text);
global.add(c);
// Hostname Field
JPanel c1 = new JPanel();
c1.setMaximumSize(new Dimension((int) c1.getMaximumSize().getWidth(), 50));
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20, 25));
c1.add(label2);
c1.add(text1);
global.add(c1);
global.add(Box.createVerticalGlue());

Categories