Related
I am creating a Gui in java and I wanted to have a list of selectable checkboxes in a smaller pane that allows you to scroll through it. I have it set up but I may have messed up somewhere that it does not allow me to scroll, any help is appreciated.
GUI:
Code:
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.getContentPane().setLayout(null);
frame.setVisible(true);
frame.setSize(801, 500);
JPanel panel_1 = new JPanel();
panel_1.setBorder(null);
panel_1.setBackground(new Color(66, 69, 65));
panel_1.setBounds(0, 0, 194, 483);
frame.getContentPane().add(panel_1);
String[] sports = { "Select a sport", "NBA", "NFL", "NHL", "ATL", "MLB", "NCAA", "PGA", "CKE" }; // list of
panel_1.setLayout(null);
JButton btnNewButton = new JButton("Submit");
btnNewButton.setBounds(52, 382, 89, 25);
panel_1.add(btnNewButton);
btnNewButton.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 12));
// items
// contained
// in drop
// down
JComboBox comboBox_1 = new JComboBox(sports);
comboBox_1.setBounds(17, 345, 155, 22);
panel_1.add(comboBox_1);
comboBox_1.setMaximumRowCount(10);
comboBox_1.setBackground(new Color(255, 255, 255));
comboBox_1.addActionListener(comboBox_1);
comboBox_1.addActionListener(comboBox_1);
comboBox_1.addActionListener(comboBox_1);
JLabel lblSport = new JLabel("Sport\r\n");
lblSport.setBounds(0, 321, 188, 18);
lblSport.setForeground(Color.WHITE);
panel_1.add(lblSport);
lblSport.setHorizontalAlignment(SwingConstants.CENTER);
lblSport.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 14));
lblSport.setBackground(SystemColor.activeCaption);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(0, 0, 188, 291);
panel_1.add(scrollPane);
JPanel panel_2_1 = new JPanel();
scrollPane.setViewportView(panel_2_1);
panel_2_1.setToolTipText(
"This is where you input the URL of a gambling site that will be used in the calculator.");
panel_2_1.setBorder(null);
panel_2_1.setBackground(new Color(66, 69, 65));
panel_2_1.setLayout(null);
JLabel lblNewLabel = new JLabel("Sportsbook Selection");
lblNewLabel.setBounds(0, 0, 0, 0);
lblNewLabel.setForeground(Color.WHITE);
panel_2_1.add(lblNewLabel);
lblNewLabel.setFont(new Font("Calibri", Font.PLAIN, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setLabelFor(frame);
lblNewLabel.setBackground(SystemColor.activeCaption);
JCheckBox chckbxNewCheckBox = new JCheckBox("888Bets");
chckbxNewCheckBox.setBounds(6, 43, 89, 23);
chckbxNewCheckBox.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox);
JCheckBox chckbxNewCheckBox_2 = new JCheckBox("Bovada");
chckbxNewCheckBox_2.setBounds(97, 43, 89, 23);
chckbxNewCheckBox_2.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_2);
JCheckBox chckbxNewCheckBox_1 = new JCheckBox("BlueBet");
chckbxNewCheckBox_1.setBounds(6, 69, 89, 23);
chckbxNewCheckBox_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_1);
JCheckBox chckbxNewCheckBox_3 = new JCheckBox("Coral");
chckbxNewCheckBox_3.setBounds(97, 69, 89, 23);
chckbxNewCheckBox_3.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_3);
JCheckBox chckbxNewCheckBox_4 = new JCheckBox("BoyleSports");
chckbxNewCheckBox_4.setBounds(6, 95, 89, 23);
chckbxNewCheckBox_4.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_4);
JCheckBox chckbxNewCheckBox_5 = new JCheckBox("Circa Sports");
chckbxNewCheckBox_5.setBounds(97, 95, 89, 23);
chckbxNewCheckBox_5.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_5);
JCheckBox chckbxNewCheckBox_6 = new JCheckBox("Betclic");
chckbxNewCheckBox_6.setBounds(6, 121, 89, 23);
chckbxNewCheckBox_6.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_6);
JCheckBox chckbxNewCheckBox_2_1 = new JCheckBox("Casumo");
chckbxNewCheckBox_2_1.setBounds(97, 121, 89, 23);
chckbxNewCheckBox_2_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_2_1);
JCheckBox chckbxNewCheckBox_1_1 = new JCheckBox("Betfair");
chckbxNewCheckBox_1_1.setBounds(6, 147, 89, 23);
chckbxNewCheckBox_1_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_1_1);
JCheckBox chckbxNewCheckBox_3_1 = new JCheckBox("DraftKings");
chckbxNewCheckBox_3_1.setBounds(97, 147, 89, 23);
chckbxNewCheckBox_3_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_3_1);
JCheckBox chckbxNewCheckBox_4_1 = new JCheckBox("BetMGM");
chckbxNewCheckBox_4_1.setBounds(6, 173, 89, 23);
chckbxNewCheckBox_4_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_4_1);
JCheckBox chckbxNewCheckBox_5_1 = new JCheckBox("FanDuel");
chckbxNewCheckBox_5_1.setBounds(97, 173, 89, 23);
chckbxNewCheckBox_5_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_5_1);
JCheckBox chckbxNewCheckBox_7 = new JCheckBox("BetOnline");
chckbxNewCheckBox_7.setBounds(6, 199, 89, 23);
chckbxNewCheckBox_7.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_7);
JCheckBox chckbxNewCheckBox_2_2 = new JCheckBox("FOX Bet");
chckbxNewCheckBox_2_2.setBounds(97, 199, 89, 23);
chckbxNewCheckBox_2_2.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_2_2);
JCheckBox chckbxNewCheckBox_1_2 = new JCheckBox("BetRivers");
chckbxNewCheckBox_1_2.setBounds(6, 225, 89, 23);
chckbxNewCheckBox_1_2.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_1_2);
JCheckBox chckbxNewCheckBox_3_2 = new JCheckBox("GTbets");
chckbxNewCheckBox_3_2.setBounds(97, 225, 89, 23);
chckbxNewCheckBox_3_2.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_3_2);
JCheckBox chckbxNewCheckBox_4_2 = new JCheckBox("Bet Victor");
chckbxNewCheckBox_4_2.setBounds(6, 251, 89, 23);
chckbxNewCheckBox_4_2.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_4_2);
JCheckBox chckbxNewCheckBox_5_2 = new JCheckBox("Intertops");
chckbxNewCheckBox_5_2.setBounds(97, 251, 89, 23);
chckbxNewCheckBox_5_2.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_5_2);
JCheckBox chckbxNewCheckBox_6_1 = new JCheckBox("BetUS");
chckbxNewCheckBox_6_1.setBounds(6, 277, 89, 23);
chckbxNewCheckBox_6_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_6_1);
JCheckBox chckbxNewCheckBox_2_1_1 = new JCheckBox("Ladbrokes");
chckbxNewCheckBox_2_1_1.setBounds(97, 277, 89, 23);
chckbxNewCheckBox_2_1_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_2_1_1);
JCheckBox chckbxLeovegas = new JCheckBox("LeoVegas");
chckbxLeovegas.setBounds(6, 304, 89, 23);
chckbxLeovegas.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxLeovegas);
JCheckBox chckbxNewCheckBox_2_3 = new JCheckBox("LiveScore");
chckbxNewCheckBox_2_3.setBounds(97, 304, 89, 23);
chckbxNewCheckBox_2_3.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_2_3);
JCheckBox chckbxNewCheckBox_1_3 = new JCheckBox("LowVig");
chckbxNewCheckBox_1_3.setBounds(6, 330, 89, 23);
chckbxNewCheckBox_1_3.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_1_3);
JCheckBox chckbxNewCheckBox_3_3 = new JCheckBox("Matchbook");
chckbxNewCheckBox_3_3.setBounds(97, 330, 89, 23);
chckbxNewCheckBox_3_3.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_3_3);
JCheckBox chckbxNewCheckBox_4_3 = new JCheckBox("MarathonkBet ");
chckbxNewCheckBox_4_3.setBounds(6, 356, 89, 23);
chckbxNewCheckBox_4_3.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_4_3);
JCheckBox chckbxNewCheckBox_5_3 = new JCheckBox("Mr Green");
chckbxNewCheckBox_5_3.setBounds(97, 356, 89, 23);
chckbxNewCheckBox_5_3.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_5_3);
JCheckBox chckbxNewCheckBox_6_2 = new JCheckBox("MyBookie");
chckbxNewCheckBox_6_2.setBounds(6, 382, 89, 23);
chckbxNewCheckBox_6_2.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_6_2);
JCheckBox chckbxNewCheckBox_2_1_2 = new JCheckBox("Neds");
chckbxNewCheckBox_2_1_2.setBounds(97, 382, 89, 23);
chckbxNewCheckBox_2_1_2.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_2_1_2);
JCheckBox chckbxNewCheckBox_1_1_1 = new JCheckBox("NordicBet");
chckbxNewCheckBox_1_1_1.setBounds(6, 408, 89, 23);
chckbxNewCheckBox_1_1_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_1_1_1);
JCheckBox chckbxNewCheckBox_3_1_1 = new JCheckBox("PaddyPower");
chckbxNewCheckBox_3_1_1.setBounds(97, 408, 89, 23);
chckbxNewCheckBox_3_1_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_3_1_1);
JCheckBox chckbxNewCheckBox_4_1_1 = new JCheckBox("PointsBet(AU)");
chckbxNewCheckBox_4_1_1.setBounds(6, 434, 89, 23);
chckbxNewCheckBox_4_1_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_4_1_1);
JCheckBox chckbxNewCheckBox_5_1_1 = new JCheckBox("PointsBet(US)");
chckbxNewCheckBox_5_1_1.setBounds(97, 434, 89, 23);
chckbxNewCheckBox_5_1_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_5_1_1);
JCheckBox chckbxNewCheckBox_7_1 = new JCheckBox("Pinnacle");
chckbxNewCheckBox_7_1.setBounds(6, 460, 89, 23);
chckbxNewCheckBox_7_1.setFont(new Font("Calibri", Font.PLAIN, 11));
panel_2_1.add(chckbxNewCheckBox_7_1);
I was hoping to make the checkboxes able to be scrolled through.`
See the below runnable code to see one way this can be done using a JDialog for a window and Layout managers. Be sure to read the comments in code. You should be able to adapt the concept to your specific application:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class SportBookingsDemo {
//Class member variables:
private JDialog dia; // JDialog object (class global):
private JCheckBox[] chks; // JCheckBox object Array (class global):
private JComboBox sportCombo; // JDialog object (class global):
private JButton submit; // JButton object (class global):
// Captions for All JCheckBoxes (class global):
String[] sportsBookNames = {"888Bets", "Bovada", "BlueBet", "Coral", "BoyleSports",
"Circa Sports", "Betclic", "Casumo", "Betfair", "DraftKings",
"BetMGM", "FanDuel", "BetOnline", "FOX Bet", "BetRivers",
"GTbets", "Bet Victor", "Intertops", "BetUS", "Ladbrokes",
"LeoVegas", "LiveScore", "LowVig", "Matchbook", "MarathonkBet",
"Mr Green", "MyBookie", "Neds", "NordicBet", "PaddyPower",
"PointsBet(AU)", "PointsBet(US)", "Pinnacle"};
// ArrayList of String to hold captions for selected checkboxes (class global):
private ArrayList<String> bookings = new ArrayList<>();
// String Variable to hold the selected Sport (class global):
private String sport;
// Constructor:
public SportBookingsDemo() {
// Create the Dialog Window:
createDialog();
// `Submit` button ActionPerformed event:
submit.addActionListener((ActionEvent e) -> {
bookings.clear();
sport = "";
if (sportCombo.getSelectedItem().equals("Select a sport")) {
JOptionPane.showMessageDialog(dia, "You must select a specific Sport!",
"No Sport Selected", JOptionPane.WARNING_MESSAGE);
sportCombo.requestFocus();
return;
}
else {
sport = sportCombo.getSelectedItem().toString();
}
System.out.println("Selected Bookings:");
for (int i = 0; i < chks.length; i++) {
if (chks[i].isSelected()) {
bookings.add(sportsBookNames[i]);
}
}
if (bookings.isEmpty()) {
JOptionPane.showMessageDialog(dia, "You must select at least one Sportsbook!",
"No Sportbook Selected", JOptionPane.WARNING_MESSAGE);
sportCombo.requestFocus();
return;
}
// Display dialog results:
StringBuilder sb = new StringBuilder("");
sb.append("<html><center>Selected bookings for the Sport of:<br><br>");
sb.append("<font size='5', color=blue><b>").append(sport).append("</b></font><br><br></center>");
for (String bks : bookings) {
sb.append("<font size='4', color=green>✓ </font>").append(bks).append("<br>");
}
sb.append("<br></html>");
JOptionPane.showMessageDialog(dia, sb.toString(), "Bookings",
JOptionPane.INFORMATION_MESSAGE);
});
}
// Application entry point...
public static void main(String[] args) {
// App started this way to avoid the need for statics:
new SportBookingsDemo().startApp(args);
}
private void startApp(String[] args) {
// Display dialog window:
java.awt.EventQueue.invokeLater(() -> {
dia.setVisible(true);
});
}
// Method to create Dialog:
private void createDialog() {
dia = new JDialog(); // Initialize JDialog.
dia.setModal(true); // Set dialog to Modal.
dia.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); //Dispose dialog when closed.
dia.setTitle("Sports Book Dialog"); // Set dialog title caption.
dia.setAlwaysOnTop(true); // Dialog will always be on top of other windows.
dia.setSize(280, 390); // Set dialog size.
dia.setResizable(false); // Make dialog non-sizeable.
dia.setLocationRelativeTo(null); // Open dialog window to center screen.
// Decalre and initialize the JDialog's main content panel
JPanel mainPanel = new JPanel();
// Set Horizontal and Vertical Gaps for mainPanel's default FlowLayout.
FlowLayout layout = new FlowLayout();
/* Set Horiz and Vert layout Gaps. The helps ensure the
components in dialog are center aligned as a single
column. */
layout.setHgap(140); layout.setVgap(10);
mainPanel.setLayout(layout); // Apply Layout.
dia.add(mainPanel); // Add mainPanel to dialog Window
/* Declare and initialize a JLabel to display
the CheckBoxes scrollable list display. */
JLabel label_1 = new JLabel("Sportsbook Selection");
// Set the Font, font style, and font size for the title:
label_1.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 14));
// Set JLabel text Horizontal Alignment to center.
label_1.setHorizontalAlignment(JLabel.CENTER);
mainPanel.add(label_1); // Add title to mainPanel.
/* Declare a JPanel to hold all the checkboxes that
will be added to a JScrollPane within the dialog.
We also ensure a GridLayout of -1 rows (as many
as needed) and 2 columns for the JPanel. */
JPanel chkPanel = new JPanel(new GridLayout(-1, 2));
/* Initialize the chks[] JCheckBox Array to the required
length. This length is actually determined by the number
of elements within the sportsBookNames[] array which
will hereby be considered a `parallel` array to the chks[]
array. Every CheckBox element contained within the chks[]
array will contain a Caption text from the sportsBookNames[]
array at the same index value. */
chks = new JCheckBox[sportsBookNames.length];
/* Iterate through the chks[] array elements and apply
JCheckBox objects to them with text captions derived
from the sportsBookNames[] array at the current
iterated index. Add each chks[] element (JCheckBox)
to the `chkPanel` JPanel. */
for (int i = 0; i < chks.length; i++) {
chks[i] = new JCheckBox(sportsBookNames[i]);
chks[i].setFont(new Font("Calibri", Font.PLAIN, 12));
chkPanel.add(chks[i]);
}
// Add the now filled chkPanel to a JScrollPane (`chkScroll`).
JScrollPane chkScroll = new JScrollPane(chkPanel);
// Set the ScrollPane's Preferred Size we want within te dialog.
chkScroll.setPreferredSize(new Dimension(250, 200));
// Add the ScrollPane (`chkScroll`) to the Main Panel (`mainPanel`).
mainPanel.add(chkScroll);
/* Declare a JLabel to be a title for a Sport type JComboBox
and provide a title: */
JLabel sportLBL = new JLabel("Sport");
// Set the Font, font style, and font size for the title:
sportLBL.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 14));
// Add the JLabel (`sportLBL`) to the Main Panel (`mainPanel`).
mainPanel.add(sportLBL);
/* Create a JComboBox (`sportCombo`) and fill it with
Sport Types. Sport Type names are contained within
the `sports[]` array. */
String[] sports = {"Select A Sport", "NBA", "NFL", "NHL",
"ATL", "MLB", "NCAA", "PGA", "CKE"};
sportCombo = new JComboBox<>(sports);
sportCombo.setFont(new Font("Leelawadee UI Semilight", Font.PLAIN, 12));
// Set the Preferred Size of the JComboBox (`sportCombo`):
sportCombo.setPreferredSize(new Dimension(120, 25));
// Make sure the ComboBox is non-editable.
sportCombo.setEditable(false);
// Add the JComboBox (`sportCombo`) to the Main Panel (`mainPanel`).
mainPanel.add(sportCombo);
/* Initialize the `submit` JButton and apply the
text caption of "Submit". */
submit = new JButton("Submit");
submit.setFont(new Font("Leelawadee UI Semilight", Font.BOLD, 12));
// Add the JButton (`submit`) to the Main Panel (`mainPanel`).
mainPanel.add(submit);
}
}
I want to create a simple profile information page with JFrame and contentPane. I added all the components correctly. But I cant set their location. How can i set it? I used setBoundary() method bur it doesnt work.
My Anket.java class is:
import java.awt.*;
import javax.swing.*;
public class Anket {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(600, 300));
frame.setTitle("Profile Information");
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.GRAY);
JRadioButton rdbtnNewRadioButton = new JRadioButton("Age(10-20)");
rdbtnNewRadioButton.setBounds(412, 50, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Age(21-30)");
rdbtnNewRadioButton.setBounds(412, 20, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_1);
JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("Age(31-40)");
rdbtnNewRadioButton.setBounds(412, 30, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_2);
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("Age(41-50)");
rdbtnNewRadioButton.setBounds(412, 40, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_3);
JLabel lblNewLabel = new JLabel("PLEASE ENTER YOUR INFORMATION");
lblNewLabel.setBounds(149, 0, 182, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Enter Your Name");
lblNewLabel_1.setBounds(127, 25, 86, 14);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Enter Your Lastname");
lblNewLabel_2.setBounds(272, 25, 100, 14);
frame.getContentPane().add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("Enter Your Height");
lblNewLabel_3.setBounds(127, 103, 86, 14);
frame.getContentPane().add(lblNewLabel_3);
JTextField textField = new JTextField();
textField.setBounds(216, 100, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JTextField textField_1 = new JTextField();
textField_1.setBounds(127, 48, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JTextField textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(412, 48, 86, 20);
frame.getContentPane().add(textField_2);
JLabel lblNewLabel_4 = new JLabel(new ImageIcon("C:/Users/PC/eclipse-workspace/Project/src/Berguzar.jpg"));
lblNewLabel_4.setBounds(10, 21, 100, 85);
frame.getContentPane().add(lblNewLabel_4);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
The program doesnt have any error. How can i fix this problem?
My output screen is:
output
You should replace
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.GRAY);
with
JPanel contentPane = new JPanel(null); // The null is important
contentPane.setBackground(Color.GRAY);
frame.setContentPane(contentPane); // each time you change the content pane you need to revalidate the jframe (frame.setVisible(true) also revalidate the jframe so in this case it is not needed to revalidate the jframe)
I have been trying this for hours. I want to change each of the JButton components to image buttons (They were radio buttons that's why the variable names don't make sense). The for loop inside is just displaying all the images under the button's. Each image is called Option1, Option2, etc. So what I have been trying to do is get rid of the for loop and just have 10 buttons with images on them that you can click.
If someone could help me figure that out that would be great. All I've been able to do is have the button created with an image but the button appears on a completely different window.
public class JDialog2 extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Create the dialog.
*/
public JDialog2(Quiz quiz) {
setBounds(100, 100, 450, 600);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("2、Favourite subject at Hogwarts");
lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 14));
lblNewLabel_1.setBounds(94, 10, 248, 15);
contentPanel.add(lblNewLabel_1);
ButtonGroup btGroup = new ButtonGroup();
for(int i=1; i<=7; i++) {
ImageIcon image1 = new ImageIcon("Option" + i + ".jpg");
image1.setImage(image1.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
JLabel imageLablel1=new JLabel(image1);
contentPanel.add(imageLablel1);
imageLablel1.setBounds(29, 75 * i - 25, 50, 50);
}
JButton radioButton_1 = new JButton("1.Care of Magical Creatures");
radioButton_1.setBounds(29, 25, 226, 23);
contentPanel.add(radioButton_1);
JButton radioButton_2 = new JButton("2.Charms");
radioButton_2.setBounds(29, 50, 158, 23);
contentPanel.add(radioButton_2);
JButton radioButton_3 = new JButton("3.Defense Against the Dark Arts");
radioButton_3.setBounds(29, 75, 255, 23);
contentPanel.add(radioButton_3);
JButton radioButton_4 = new JButton("4.Divination");
radioButton_4.setBounds(29, 100, 121, 23);
contentPanel.add(radioButton_4);
JButton radioButton_5 = new JButton("5.Herbology");
radioButton_5.setBounds(29, 125, 179, 23);
contentPanel.add(radioButton_5);
JButton radioButton_6 = new JButton("6.History of Magic");
radioButton_6.setBounds(29, 150, 158, 23);
contentPanel.add(radioButton_6);
JButton radioButton_7 = new JButton("7.Muggle Studies");
radioButton_7.setBounds(29, 175, 121, 23);
contentPanel.add(radioButton_7);
JButton radioButton_8 = new JButton("8.Potions");
radioButton_8.setBounds(29, 200, 121, 23);
contentPanel.add(radioButton_8);
JButton radioButton_9 = new JButton("9.Study of Ancient Runes");
radioButton_9.setBounds(29, 220, 255, 23);
contentPanel.add(radioButton_9);
JButton radioButton_10 = new JButton("10.Transfiguration");
radioButton_10.setBounds(29, 245, 179, 23);
contentPanel.add(radioButton_10);
btGroup.add(radioButton_1);
btGroup.add(radioButton_2);
btGroup.add(radioButton_3);
btGroup.add(radioButton_4);
btGroup.add(radioButton_5);
btGroup.add(radioButton_6);
btGroup.add(radioButton_7);
btGroup.add(radioButton_8);
btGroup.add(radioButton_9);
btGroup.add(radioButton_10);
JButton btnCommit = new JButton("Commit");
btnCommit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Question question = quiz.getNextQuestion();
int choice = 0;
Enumeration<AbstractButton> en = btGroup.getElements();
while (en.hasMoreElements()) {
AbstractButton ab = en.nextElement();
choice++;
if (ab.isSelected()) {
break;
}
}
question.setSelectedAnswer(choice - 1);
JDialog3 dialog3 = new JDialog3(quiz);
dialog3.setVisible(true);
exit();
}
});
btnCommit.setBounds(300, 138, 93, 23);
contentPanel.add(btnCommit);
}
public void exit(){
this.setVisible(false);
}
}
basically I have a very simple GUI, but for some reason when I run the code the JButtons only appear when I mouse over them, and the JTextField only appear if I click on them. How do I fix this so that they are visible when the program runs? Thanks.
Here is my code:
//JFrame + settings
JFrame frmFormSubmission = new JFrame();
frmFormSubmission.setSize(new Dimension(350, 165));
frmFormSubmission.setTitle("Form Submission - Client");
frmFormSubmission.setLocationRelativeTo(null);
frmFormSubmission.setVisible(true);
frmFormSubmission.setResizable(false);
frmFormSubmission.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel
JPanel panel = new JPanel();
frmFormSubmission.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
//JLabels
//Name
JLabel lblName = new JLabel("Name:");
lblName.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblName.setBounds(46, 12, 40, 14);
panel.add(lblName);
//Address
JLabel lblAddress = new JLabel("Address:");
lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblAddress.setBounds(33, 37, 53, 14);
panel.add(lblAddress);
//Phone
JLabel lblPhone = new JLabel("Phone #:");
lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblPhone.setBounds(28, 62, 58, 14);
panel.add(lblPhone);
//Email
JLabel lblEmail = new JLabel("Email:");
lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblEmail.setBounds(46, 87, 36, 14);
panel.add(lblEmail);
//JTextFields
//Name
nameField = new JTextField();
nameField.setBounds(93, 11, 137, 20);
panel.add(nameField);
nameField.setColumns(10);
//Address
addressField = new JTextField();
addressField.setColumns(10);
addressField.setBounds(93, 36, 137, 20);
panel.add(addressField);
//Phone
phoneField = new JTextField();
phoneField.setColumns(10);
phoneField.setBounds(93, 61, 137, 20);
panel.add(phoneField);
//Email
emailField = new JTextField();
emailField.setColumns(10);
emailField.setBounds(93, 86, 137, 20);
panel.add(emailField);
//JButtons
//Submit
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(240, 10, 89, 23);
panel.add(btnSubmit);
//Cancel
JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(240, 44, 89, 23);
panel.add(btnCancel);
//Flush
JButton btnFlush = new JButton("Flush");
btnFlush.setBounds(240, 76, 89, 23);
panel.add(btnFlush);
//Checkbox
JCheckBox chckbxPromotions = new JCheckBox("Email me with new and promotions!");
chckbxPromotions.setBounds(25, 108, 205, 23);
panel.add(chckbxPromotions);
Any help is very much appreciated!
Call frmFormSubmission.setVisible(true); as the last thing you do. setVisible will layout your components so they can be properly drawn. If you add components without laying them out, then you'll have graphical issues.
For more information on what that means, check out the documentation on the validate method.
Also, try
frmFormSubmission.invalidate();
frmFormSubmission.validate();
frmFormSubmission.repaint();
If this still does not work, you can try this:
SwingUtilities.updateComponentTreeUI(frmFormSubmission);
Following is my Java Swing applet code in which I have first field as employee code and text box for entering employee code, I want to retrieve data of employee from MySql database when I will enter employee code in text box after hitting Enter key on keyboard and set retrieve data in respective text boxes and combo boxes. and same to embed it in JSP as applet.
Please help.
Here is my code:
import javax.swing.*;
import java.applet.*;
import java.awt.*;
public class App extends JApplet{
public void start()
{
JFrame frame = new JFrame("Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
//JButton button2 = new JButton("Cancel");
Container c;
c=frame.getContentPane();
c.setLayout(null);
JLabel name=new JLabel("Name :");
JLabel compcode=new JLabel("Company Code :");
JLabel cardno=new JLabel("Card Number: ");
JLabel cardtype=new JLabel("Card Type :");
JLabel pin=new JLabel("Pin :");
JLabel bldgrp=new JLabel("Blood Group :");
JLabel empcode=new JLabel("Employee Code :");
JLabel dob=new JLabel("DOB :");
JLabel valupto=new JLabel("Valid Upto :");
JLabel jdate=new JLabel("Joining Date :");
JLabel dept=new JLabel("Department :");
JLabel uid=new JLabel("UID :");
String data []={"A","AB","B","B +","A +","O +","O -"};
JTextField nametxt=new JTextField(10);
JComboBox compcodetxt=new JComboBox();
JTextField cardnumtxt=new JTextField(10);
JTextField cardtypetxt=new JTextField(10);
JTextField pintxt=new JTextField(10);
JComboBox bldgrptxt=new JComboBox(data);
bldgrptxt.setSelectedIndex(5);
JTextField empcodetxt=new JTextField(10);
JTextField dobtxt=new JTextField(10);
JTextField valuptotxt=new JTextField(10);
JTextField jdatetxt=new JTextField(10);
JTextField depttxt=new JTextField(10);
JTextField uidtxt=new JTextField(10);
empcode.setBounds(10, 10, 100, 25);
empcodetxt.setBounds(110, 10, 100, 25);
name.setBounds(10, 40, 100, 25);
nametxt.setBounds(110, 40, 100, 25);
compcode.setBounds(10, 70, 100, 25);
compcodetxt.setBounds(110, 70, 100, 25);
cardno.setBounds(10, 100, 100, 25);
cardnumtxt.setBounds(110, 100, 100, 25);
//pin.setBounds(10, 110, 100, 25);
//pintxt.setBounds(110, 110, 100, 25);
bldgrp.setBounds(10, 130, 100, 25);
bldgrptxt.setBounds(110, 130, 100, 25);
dob.setBounds(10, 160, 100, 25);
dobtxt.setBounds(110, 160, 100, 25);
valupto.setBounds(10, 190, 100, 25);
valuptotxt.setBounds(110, 190, 100, 25);
jdate.setBounds(10, 220, 100, 25);
jdatetxt.setBounds(110, 220, 100, 25);
dept.setBounds(10, 250, 100, 25);
depttxt.setBounds(110, 250, 100, 25);
uid.setBounds(10, 280, 100, 25);
uidtxt.setBounds(110, 280, 100, 25);
but_per.setBounds(10, 340, 120, 25);
//button2.setBounds(10, 70, 75, 25);
c.add(name); c.add(nametxt);
c.add(compcode); c.add(compcodetxt);
c.add(cardno); c.add(cardnumtxt);
c.add(pin); c.add(pintxt);
c.add(bldgrp); c.add(bldgrptxt);
c.add(empcode); c.add(empcodetxt);
c.add(dob); c.add(dobtxt);
c.add(valupto); c.add(valuptotxt);
c.add(jdate); c.add(jdatetxt);
c.add(dept); c.add(depttxt);
c.add(uid); c.add(uidtxt);
c.add(but_per);
//panel.add(button1);
//panel.add(button2);
//frame.add(panel);
frame.setSize(350,400);
//frame.pack();
frame.setVisible(true);
}
public void stop(){}
}
In your ActionListener for that button retrieve the text of the textfield using textfield.getText() and then use this value to query database. I'm assuming that you know well how to query database.
Wait! you haven't implemented ActionListener!
Do the following:
but_per.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
/// write your code here!
String a= field.getText();
System.out.print(a);
///or DB code
}
});
Here is a LINK if you aren't familiar with Action Listener
UPDATE:
If you want all processing after a key press, you have to implement keybord events,
you can find a tutorial link here
This source is the closest workable version of your code I could produce, but it still has many serious problems.
Using the start() method incorrectly. The method will be called each time the browser is restored from minimized.
Use of a null layout and setBounds().
A free floating, non modal GUI element will cause focus problems..
Having said that, type a number in the employee code field and hit enter to see something like (screenshot shortened vertically)..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* <applet code=App width=400 height=600></applet> */
public class App extends JApplet {
public void start() {
final JFrame frame = new JFrame("Form");
ActionListener doDB = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "Query the DB off the EDT!");
}
};
// This cannot be done in a sand-boxed applet
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// hides the bottom field..
//frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
JButton but_per = new JButton("Personalize");
Container c;
c = frame.getContentPane();
c.setLayout(null);
JLabel name = new JLabel("Name :");
JLabel compcode = new JLabel("Company Code :");
JLabel cardno = new JLabel("Card Number: ");
JLabel cardtype = new JLabel("Card Type :");
JLabel pin = new JLabel("Pin :");
JLabel bldgrp = new JLabel("Blood Group :");
JLabel empcode = new JLabel("Employee Code :");
JLabel dob = new JLabel("DOB :");
JLabel valupto = new JLabel("Valid Upto :");
JLabel jdate = new JLabel("Joining Date :");
JLabel dept = new JLabel("Department :");
JLabel uid = new JLabel("UID :");
String data[] = {"A", "AB", "B", "B +", "A +", "O +", "O -"};
JTextField nametxt = new JTextField(10);
JComboBox compcodetxt = new JComboBox();
JTextField cardnumtxt = new JTextField(10);
JTextField cardtypetxt = new JTextField(10);
JTextField pintxt = new JTextField(10);
JComboBox bldgrptxt = new JComboBox(data);
bldgrptxt.setSelectedIndex(5);
JTextField empcodetxt = new JTextField(10);
empcodetxt.addActionListener(doDB);
JTextField dobtxt = new JTextField(10);
JTextField valuptotxt = new JTextField(10);
JTextField jdatetxt = new JTextField(10);
JTextField depttxt = new JTextField(10);
JTextField uidtxt = new JTextField(10);
empcode.setBounds(10, 10, 100, 25);
empcodetxt.setBounds(110, 10, 100, 25);
name.setBounds(10, 40, 100, 25);
nametxt.setBounds(110, 40, 100, 25);
compcode.setBounds(10, 70, 100, 25);
compcodetxt.setBounds(110, 70, 100, 25);
cardno.setBounds(10, 100, 100, 25);
cardnumtxt.setBounds(110, 100, 100, 25);
bldgrp.setBounds(10, 130, 100, 25);
bldgrptxt.setBounds(110, 130, 100, 25);
dob.setBounds(10, 160, 100, 25);
dobtxt.setBounds(110, 160, 100, 25);
valupto.setBounds(10, 190, 100, 25);
valuptotxt.setBounds(110, 190, 100, 25);
jdate.setBounds(10, 220, 100, 25);
jdatetxt.setBounds(110, 220, 100, 25);
dept.setBounds(10, 250, 100, 25);
depttxt.setBounds(110, 250, 100, 25);
uid.setBounds(10, 280, 100, 25);
uidtxt.setBounds(110, 280, 100, 25);
but_per.setBounds(10, 340, 120, 25);
c.add(name);
c.add(nametxt);
c.add(compcode);
c.add(compcodetxt);
c.add(cardno);
c.add(cardnumtxt);
c.add(pin);
c.add(pintxt);
c.add(bldgrp);
c.add(bldgrptxt);
c.add(empcode);
c.add(empcodetxt);
c.add(dob);
c.add(dobtxt);
c.add(valupto);
c.add(valuptotxt);
c.add(jdate);
c.add(jdatetxt);
c.add(dept);
c.add(depttxt);
c.add(uid);
c.add(uidtxt);
c.add(but_per);
frame.setSize(350, 400);
frame.setVisible(true);
}
public void stop() {
}
}