Java Gui cannot scroll through scrollPane - java

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);
}
}

Related

How to validate JRadioButtons in Eclipse (JSwing)?

I am doing a quiz program, therefore I need JRadioButton. However, the problem is that I do not know how to validate them such that only one Radio button can be selected.
public class QuizQ1Panel extends MasterPanel{
protected static final JFrame JFrame = null;
/**
* Create the panel.
* #param myPanel
*/
public QuizQ1Panel(JFrame mf) {
super(mf);
setBounds(100, 100, 900, 750);
JLabel lblQuiz1JY = new JLabel("This quiz consists of 5 questions.");
lblQuiz1JY.setFont(new Font("Tahoma", Font.BOLD, 16));
lblQuiz1JY.setBounds(470, 162, 429, 20);
add(lblQuiz1JY);
JLabel lblQ1JY = new JLabel("Question 1");
lblQ1JY.setFont(new Font("Tahoma", Font.BOLD, 17));
lblQ1JY.setBounds(66, 216, 120, 20);
add(lblQ1JY);
JLabel lblQns1JY = new JLabel("What shape is used to represent a 'process' symbol?");
lblQns1JY.setFont(new Font("Tahoma", Font.BOLD, 17));
lblQns1JY.setBounds(66, 241, 534, 20);
add(lblQns1JY);
/* How do I validate JRadioButtons such that only one button can be selected?*/
JRadioButton rdbtnRectJY = new JRadioButton("A. Rectangle");
rdbtnRectJY.setBounds(60, 273, 155, 29);
add(rdbtnRectJY);
JRadioButton rdbtnDiaJY = new JRadioButton("B. Diamond");
rdbtnDiaJY.setBounds(60, 310, 155, 29);
add(rdbtnDiaJY);
JRadioButton rdbtnCirJY = new JRadioButton("C. Circle");
rdbtnCirJY.setBounds(60, 344, 155, 29);
add(rdbtnCirJY);
JRadioButton rdbtnParaJY = new JRadioButton("D. Parallelogram");
rdbtnParaJY.setBounds(60, 381, 155, 29);
add(rdbtnParaJY);
/* Currently when I clicked on JRadioButton, all can be selectd. However, I only want one button to be selected. So, how should I validate it? */
As stated in the comments, use Buttongroups for that task. They will allow only 1 RadionButton to be selected at a time: https://docs.oracle.com/javase/7/docs/api/javax/swing/ButtonGroup.html
credits to #LukasRotter

can not refer a value to a radiobutton

i write a convert temperature code using radiobutton, but when i use actionListener to print the value to a label, it show "Cannot refer to the non-final local variable valueto defined in an enclosing scope"
can some one show me the problem and how to fix it? (also sorry for my English)
the Radiobutton
JRadioButton Cbutton = new JRadioButton("Celsius");
Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton.setBounds(10, 40, 109, 23);
contentPane.add(Cbutton);
JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
Fbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(valueto));
}
});
i use this to convert
if(Fbutton.isSelected()&&Cbutton2.isSelected()){
value=F;
valueto=(F-32)/1.8;
}else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
value=F;
valueto=(F+459.67)*5/9;
}else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
value=C;
valueto=C*1.8+32;
}else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
value=C;
valueto=C+273.15;
}else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
value=K;
valueto=K-273.15;
}else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
value=K;
valueto=K*9.5-459.67;
}
Edit : i declare valueto at the bottom of the class
double value = 0;
double valueto = 0;
double F = 0, C = 0, K = 0;
it ask me to add final to "double valueto", but when i add it the valueto in "if" become error
The Whole code
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class CtoKtoF extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CtoKtoF frame = new CtoKtoF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* #return
*/
public CtoKtoF() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 303);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
double value = 0 ;
double valueto = 0;
double F = 0, C = 0, K = 0;
Border border = LineBorder.createGrayLineBorder();//make border for Jlabel
final JLabel label = new JLabel("New label");
label.setFont(new Font("Tahoma", Font.PLAIN, 12));
label.setBounds(10, 228, 220, 22);
label.setBorder(border);
contentPane.add(label);
JLabel lblNewLabel = new JLabel("Convert from");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblNewLabel.setBounds(10, 11, 92, 22);
contentPane.add(lblNewLabel);
JRadioButton Fbutton = new JRadioButton("Fahrenheit");
Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton.setBounds(10, 40, 109, 23);
//Fbutton.setActionCommand("F");
contentPane.add(Fbutton);
JRadioButton Cbutton = new JRadioButton("Celcius");
Cbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Cbutton.setBounds(121, 40, 109, 23);
//Cbutton.setActionCommand("C");
contentPane.add(Cbutton);
JRadioButton Kbutton = new JRadioButton("Kelvin");
Kbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Kbutton.setBounds(232, 40, 109, 23);
//Kbutton.setActionCommand("K");
contentPane.add(Kbutton);
JLabel lblEnterNumericTemperature = new JLabel("Enter Numeric Temperature");
lblEnterNumericTemperature.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblEnterNumericTemperature.setBounds(10, 70, 238, 22);
contentPane.add(lblEnterNumericTemperature);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 12));
textField.setBounds(10, 103, 220, 22);
textField.setText(String.valueOf(value));
contentPane.add(textField);
textField.setColumns(10);
JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
Fbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(valueto));
}
});
Fbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton2.setBounds(10, 165, 109, 23);
//Fbutton2.setActionCommand("F2");
contentPane.add(Fbutton2);
JRadioButton Cbutton2 = new JRadioButton("Celcius");
Cbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Cbutton2.setBounds(121, 165, 109, 23);
//Cbutton2.setActionCommand("C2");
contentPane.add(Cbutton2);
JRadioButton Kbutton2 = new JRadioButton("Kelvin");
Kbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Kbutton2.setBounds(232, 165, 109, 23);
//Kbutton2.setActionCommand("K2");
contentPane.add(Kbutton2);
ButtonGroup group1 = new ButtonGroup();
group1.add(Fbutton);
group1.add(Kbutton);
group1.add(Cbutton);
ButtonGroup group2 = new ButtonGroup();
group2.add(Fbutton2);
group2.add(Cbutton2);
group2.add(Kbutton2);
/*Fbutton.addActionListener(this);
Cbutton.addActionListener(this);
Kbutton.addActionListener(this);
Fbutton2.addActionListener(this);
Cbutton2.addActionListener(this);
Kbutton2.addActionListener(this);*/
//boolean checked = Fbutton.getState();
JLabel lblConvertTo = new JLabel("Convert to");
lblConvertTo.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblConvertTo.setBounds(10, 136, 92, 22);
contentPane.add(lblConvertTo);
JLabel lblComparableTemperatureIs = new JLabel("Comparable Temperature is");
lblComparableTemperatureIs.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblComparableTemperatureIs.setBounds(10, 195, 238, 22);
contentPane.add(lblComparableTemperatureIs);
if(Fbutton.isSelected()&&Cbutton2.isSelected()){
value=F;
valueto=(F-32)/1.8;
}else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
value=F;
valueto=(F+459.67)*5/9;
}else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
value=C;
valueto=C*1.8+32;
}else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
value=C;
valueto=C+273.15;
}else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
value=K;
valueto=K-273.15;
}else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
value=K;
valueto=K*9.5-459.67;
}
}
}

Populate a JComboBox using SQL

I am currently doing a diploma course in Java and for our project we have to create a Restaurant Calculator(See screenshot), I have the majority of the GUI done but I am having great difficulty with the SQL. I have never used SQL before so I'm not 100% certain what to do. I understand how it works but I can't even run the SQL file that the college supplied us in MySQL. Any help with this would be greatly appreciated as I am really struggling with this. I have had this GUI done the past 2 weeks and have been searching online for help but cannot get my head around it
Code is below...
import java.awt.Container;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RestaurantCalculator {
private static JTextField TotalText;
private static JTextField TaxText;
private static JTextField Table;
private static JTextField Sub;
private static JTextField WName;
private static JComboBox BevMenu;
private static JComboBox AppMenu;
private static JComboBox MainMenu;
private static JComboBox DesMenu;
public static void testFun(String myName){
TotalText.setText(myName);
}
public static void addComponentsToPane(Container pane){
pane.setLayout(null);
Border border = BorderFactory.createLoweredBevelBorder();
Border button = BorderFactory.createRaisedBevelBorder();
JLabel Welcome = new JLabel("Restaurant");
Welcome.setBounds(250, 5, 150, 100);
Welcome.setFont(new Font("Arial Black", Font.BOLD, 20));
JLabel Waiter = new JLabel("Waiter Information");
Waiter.setBounds(100, 100, 200, 100);
Waiter.setFont(new Font("Arial Black", Font.BOLD, 14));
JLabel Tableno = new JLabel("Table number:");
Tableno.setBounds(120, 130, 200, 100);
Tableno.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel WaiterName = new JLabel("Waiter Name:");
WaiterName.setBounds(120, 160, 200, 100);
WaiterName.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel MenuItems = new JLabel("Menu Items");
MenuItems.setBounds(100, 220, 150, 100);
MenuItems.setFont(new Font("Arial Black", Font.BOLD, 14));
JLabel Beverage = new JLabel("Beverage:");
Beverage.setBounds(120, 250, 200, 100);
Beverage.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Appetizer = new JLabel("Appetizer:");
Appetizer.setBounds(120, 280, 200, 100);
Appetizer.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel MainCourse = new JLabel("Main Course:");
MainCourse.setBounds(120, 310, 200, 100);
MainCourse.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Dessert = new JLabel("Dessert:");
Dessert.setBounds(120, 340, 200, 100);
Dessert.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel SubTotal = new JLabel("Sub Total:");
SubTotal.setBounds(120, 440, 200, 100);
SubTotal.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Tax = new JLabel("Tax:");
Tax.setBounds(120, 470, 200, 100);
Tax.setFont(new Font("Arial Black", Font.BOLD, 12));
JLabel Total = new JLabel("Total:");
Total.setBounds(120, 500, 200, 100);
Total.setFont(new Font("Arial Black", Font.BOLD, 12));
Table = new JTextField(10);
Table.setBounds(290, 171, 150, 20);
Table.setBorder(border);
WName = new JTextField(11);
WName.setBounds(290, 201, 150, 20);
WName.setBorder(border);
Sub = new JTextField(10);
Sub.setBounds(260,482,150, 20);
Sub.setBorder(border);
Sub.setEditable(false);
TotalText = new JTextField(10);
TotalText.setBounds(260,545,150, 20);
TotalText.setBorder(border);
TotalText.setEditable(false);
TaxText = new JTextField(10);
TaxText.setBounds(260,515,150, 20);
TaxText.setBorder(border);
TaxText.setEditable(false);
BevMenu = new JComboBox();
BevMenu.setBounds(290, 295, 180, 20);
AppMenu = new JComboBox();
AppMenu.setBounds(290, 325, 180, 20);
MainMenu = new JComboBox();
MainMenu.setBounds(290, 355, 180, 20);
DesMenu = new JComboBox();
DesMenu.setBounds(290, 385, 180, 20);
JButton calculateJButton;
calculateJButton = new JButton( "Calculate Bill" );
calculateJButton.setBounds(250,425, 170, 30);
calculateJButton.setBorder(button);
pane.add(Welcome);
pane.add(Waiter);
pane.add(Tableno);
pane.add(WaiterName);
pane.add(MenuItems);
pane.add(Beverage);
pane.add(Appetizer);
pane.add(MainCourse);
pane.add(Dessert);
pane.add(SubTotal);
pane.add(Tax);
pane.add(Total);
pane.add(Table);
pane.add(WName);
pane.add(Sub);
pane.add(TaxText);
pane.add(TotalText);
pane.add(calculateJButton);
pane.add(BevMenu);
pane.add(AppMenu);
pane.add(MainMenu);
pane.add(DesMenu);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Restaurant Bill Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Size and display the window.
Insets insets = frame.getInsets();
frame.setSize(600 + insets.left + insets.right,
700 + insets.top + insets.bottom);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
String theBest = "test";
testFun(theBest);
}
});
}

WindowBuilder Source Code Layout and Database Implementation

Hi I am a newbie java programmer, I am currently creating a java gui application using WindowBuilder within eclipse.
I have the layout that I like however, the source code is all in one class, and I can't seem to navigate through the panels correctly.
A link to the image for how the layout of panels is in WindowBuilder - http://postimg.org/image/xmnm9ilm1/
I was wondering how can I best layout the code, and how best to navigate through the program.
Bear in mind I don't know many techical terms.
Also what is the best way to implement a microsoft access backend database that can be editied within the program for login usernames, passwords and adding and removing items? I have a seperate database class at the moment.
Thank You
Source code of program is shown below
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;
import javax.swing.JList;
import javax.swing.Box;
import java.awt.Panel;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class UnivEPOS {
private JFrame frmUniveposV;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UnivEPOS window = new UnivEPOS();
window.frmUniveposV.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UnivEPOS() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmUniveposV = new JFrame();
frmUniveposV.setTitle("UnivEPOS v1.0");
frmUniveposV.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frmUniveposV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmUniveposV.getContentPane().setLayout(new CardLayout(0, 0));
JPanel Login = new JPanel();
Login.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Login, "name_244778779455709");
Login.setLayout(null);
JLabel lblLogin = new JLabel("LOGIN");
lblLogin.setForeground(new Color(255, 255, 255));
lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
lblLogin.setBounds(10, 0, 60, 50);
Login.add(lblLogin);
textField = new JTextField();
textField.setBounds(518, 282, 262, 20);
Login.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(518, 319, 262, 20);
Login.add(textField_1);
JLabel lblNewLabel = new JLabel("Username");
lblNewLabel.setForeground(new Color(255, 255, 255));
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setBounds(416, 283, 92, 14);
Login.add(lblNewLabel);
JLabel lblPassword = new JLabel("Password");
lblPassword.setForeground(new Color(255, 255, 255));
lblPassword.setFont(new Font("Tahoma", Font.BOLD, 16));
lblPassword.setBounds(416, 320, 92, 14);
Login.add(lblPassword);
JButton btnNewButton = new JButton("ENTER");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton.setBounds(691, 396, 89, 23);
Login.add(btnNewButton);
JButton btnNewButton_1 = new JButton("EXIT");
btnNewButton_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
}
});
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_1.setBounds(518, 396, 89, 23);
Login.add(btnNewButton_1);
JPanel Home = new JPanel();
Home.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Home, "name_244789265918213");
Home.setLayout(null);
JLabel lblCurrentOrders = new JLabel("CURRENT ORDERS");
lblCurrentOrders.setForeground(new Color(255, 255, 255));
lblCurrentOrders.setBounds(535, 0, 346, 31);
lblCurrentOrders.setFont(new Font("Tahoma", Font.BOLD, 35));
Home.add(lblCurrentOrders);
JButton btnNewButton_2 = new JButton("Previous");
btnNewButton_2.setBounds(614, 598, 95, 31);
Home.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("Next");
btnNewButton_3.setBounds(731, 598, 95, 31);
Home.add(btnNewButton_3);
JButton btnNewButton_6 = new JButton("NEW ORDER");
btnNewButton_6.setBounds(10, 598, 244, 112);
btnNewButton_6.setFont(new Font("Tahoma", Font.BOLD, 18));
Home.add(btnNewButton_6);
JButton btnNewButton_4 = new JButton("Show Paid");
btnNewButton_4.setBounds(614, 632, 95, 31);
Home.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("Show Unpaid");
btnNewButton_5.setBounds(731, 632, 95, 31);
Home.add(btnNewButton_5);
JButton btnShowAll = new JButton("SHOW ALL");
btnShowAll.setBounds(658, 663, 131, 31);
Home.add(btnShowAll);
JButton btnAdmin = new JButton("ADMIN");
btnAdmin.setBounds(1245, 682, 95, 37);
Home.add(btnAdmin);
JLabel lblCurrentUser = new JLabel("Current User:");
lblCurrentUser.setForeground(new Color(255, 255, 255));
lblCurrentUser.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblCurrentUser.setBounds(1081, 12, 83, 14);
Home.add(lblCurrentUser);
JLabel lblNewLabel_1 = new JLabel("Date & Time:");
lblNewLabel_1.setForeground(new Color(255, 255, 255));
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel_1.setBounds(10, 14, 80, 14);
Home.add(lblNewLabel_1);
JPanel Order = new JPanel();
Order.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Order, "name_244796279334447");
Order.setLayout(null);
JLabel lblOrder = new JLabel("ORDER:");
lblOrder.setForeground(new Color(255, 255, 255));
lblOrder.setFont(new Font("Tahoma", Font.BOLD, 40));
lblOrder.setBounds(10, 11, 166, 43);
Order.add(lblOrder);
JLabel lblDateTime = new JLabel("DATE & TIME:");
lblDateTime.setForeground(new Color(255, 255, 255));
lblDateTime.setFont(new Font("Tahoma", Font.BOLD, 40));
lblDateTime.setBounds(771, 11, 282, 43);
Order.add(lblDateTime);
JLabel lblTable = new JLabel("TABLE:");
lblTable.setForeground(new Color(255, 255, 255));
lblTable.setFont(new Font("Tahoma", Font.BOLD, 40));
lblTable.setBounds(391, 11, 146, 43);
Order.add(lblTable);
Panel panel = new Panel();
panel.setBounds(10, 75, 650, 489);
Order.add(panel);
panel.setLayout(null);
JLabel lblNewLabel_2 = new JLabel("TOTAL:");
lblNewLabel_2.setForeground(new Color(255, 255, 255));
lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 40));
lblNewLabel_2.setBounds(10, 421, 157, 57);
panel.add(lblNewLabel_2);
JLabel lblCurrentOrder = new JLabel("CURRENT ORDER");
lblCurrentOrder.setForeground(new Color(255, 255, 255));
lblCurrentOrder.setBounds(251, 5, 198, 31);
lblCurrentOrder.setFont(new Font("Tahoma", Font.PLAIN, 25));
panel.add(lblCurrentOrder);
Panel panel_1 = new Panel();
panel_1.setBounds(690, 75, 650, 489);
Order.add(panel_1);
panel_1.setLayout(null);
JLabel lblCategories = new JLabel("CATEGORIES");
lblCategories.setForeground(new Color(255, 255, 255));
lblCategories.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblCategories.setBounds(254, 11, 153, 31);
panel_1.add(lblCategories);
JButton btnPrevious = new JButton("PREVIOUS");
btnPrevious.setBounds(462, 455, 89, 23);
panel_1.add(btnPrevious);
JButton btnNext = new JButton("NEXT");
btnNext.setBounds(551, 455, 89, 23);
panel_1.add(btnNext);
JButton btnNewButton_7 = new JButton("SAVE");
btnNewButton_7.setFont(new Font("Tahoma", Font.BOLD, 30));
btnNewButton_7.setBounds(10, 582, 254, 137);
Order.add(btnNewButton_7);
JButton btnSavePay = new JButton("SAVE & PAY");
btnSavePay.setFont(new Font("Tahoma", Font.BOLD, 30));
btnSavePay.setBounds(274, 582, 274, 137);
Order.add(btnSavePay);
JButton btnFinish = new JButton("FINISH");
btnFinish.setFont(new Font("Tahoma", Font.BOLD, 30));
btnFinish.setBounds(558, 582, 253, 137);
Order.add(btnFinish);
JButton button = new JButton("ADMIN");
button.setFont(new Font("Tahoma", Font.BOLD, 11));
button.setBounds(1245, 682, 95, 37);
Order.add(button);
JButton btnExit = new JButton("EXIT");
btnExit.setFont(new Font("Tahoma", Font.BOLD, 11));
btnExit.setBounds(1114, 682, 95, 37);
Order.add(btnExit);
JPanel Reports = new JPanel();
Reports.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Reports, "name_244798869260892");
Reports.setLayout(null);
Panel panel_2 = new Panel();
panel_2.setBounds(0, 180, 1350, 459);
Reports.add(panel_2);
panel_2.setLayout(null);
JLabel lblSales = new JLabel("SALES");
lblSales.setForeground(new Color(255, 255, 255));
lblSales.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblSales.setBounds(600, 0, 78, 52);
panel_2.add(lblSales);
JLabel lblReports = new JLabel("REPORTS");
lblReports.setForeground(new Color(255, 255, 255));
lblReports.setFont(new Font("Tahoma", Font.BOLD, 35));
lblReports.setBounds(556, 0, 174, 52);
Reports.add(lblReports);
JButton btnNewButton_8 = new JButton("SALES");
btnNewButton_8.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnNewButton_8.setBounds(10, 97, 174, 77);
Reports.add(btnNewButton_8);
JButton btnCancellations = new JButton("CANCELLATIONS");
btnCancellations.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnCancellations.setBounds(194, 97, 221, 77);
Reports.add(btnCancellations);
JButton btnItemsSold = new JButton("ITEMS SOLD");
btnItemsSold.setFont(new Font("Tahoma", Font.PLAIN, 25));
btnItemsSold.setBounds(425, 97, 221, 77);
Reports.add(btnItemsSold);
JLabel lblNewLabel_3 = new JLabel("PAGE");
lblNewLabel_3.setForeground(new Color(255, 255, 255));
lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 40));
lblNewLabel_3.setBounds(610, 645, 106, 74);
Reports.add(lblNewLabel_3);
JButton btnNewButton_9 = new JButton("PREVIOUS");
btnNewButton_9.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_9.setBounds(860, 645, 163, 74);
Reports.add(btnNewButton_9);
JButton btnNext_1 = new JButton("NEXT");
btnNext_1.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNext_1.setBounds(1025, 645, 164, 74);
Reports.add(btnNext_1);
JButton btnNewButton_10 = new JButton("EXIT");
btnNewButton_10.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_10.setBounds(1199, 645, 141, 74);
Reports.add(btnNewButton_10);
JPanel Settings = new JPanel();
Settings.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Settings, "name_244813941995636");
Settings.setLayout(null);
JLabel lblNewLabel_4 = new JLabel("SYSTEM SETTINGS");
lblNewLabel_4.setForeground(new Color(255, 255, 255));
lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 35));
lblNewLabel_4.setBounds(500, 0, 339, 54);
Settings.add(lblNewLabel_4);
JList list = new JList();
list.setForeground(new Color(255, 255, 255));
list.setBackground(new Color(128, 0, 0));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setFont(new Font("Tahoma", Font.PLAIN, 40));
list.setModel(new AbstractListModel() {
String[] values = new String[] {"USER 1", "USER 2", "USER 3", "USER 4", "USER 5"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
list.setBounds(10, 82, 634, 481);
Settings.add(list);
JButton btnNewButton_11 = new JButton("ADD USER");
btnNewButton_11.setFont(new Font("Tahoma", Font.BOLD, 11));
btnNewButton_11.setBounds(10, 617, 196, 102);
Settings.add(btnNewButton_11);
JButton btnChangeSuperuserPassword = new JButton("CHANGE SUPERUSER PASSWORD");
btnChangeSuperuserPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
btnChangeSuperuserPassword.setBounds(433, 617, 211, 102);
Settings.add(btnChangeSuperuserPassword);
JButton button_1 = new JButton("EXIT");
button_1.setFont(new Font("Tahoma", Font.BOLD, 11));
button_1.setBounds(1245, 682, 95, 37);
Settings.add(button_1);
Panel panel_3 = new Panel();
panel_3.setBounds(650, 84, 690, 479);
Settings.add(panel_3);
panel_3.setLayout(null);
JLabel lblNewLabel_5 = new JLabel("RECEIPT OF SALE");
lblNewLabel_5.setForeground(new Color(255, 255, 255));
lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblNewLabel_5.setBounds(260, 11, 178, 44);
panel_3.add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("COMPANY");
lblNewLabel_6.setForeground(new Color(255, 255, 255));
lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblNewLabel_6.setBounds(24, 136, 125, 44);
panel_3.add(lblNewLabel_6);
JLabel lblSalesRepresentative = new JLabel("SALES REP");
lblSalesRepresentative.setForeground(new Color(255, 255, 255));
lblSalesRepresentative.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblSalesRepresentative.setBounds(24, 213, 125, 44);
panel_3.add(lblSalesRepresentative);
JLabel lblAddress = new JLabel("ADDRESS");
lblAddress.setForeground(new Color(255, 255, 255));
lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblAddress.setBounds(24, 171, 125, 48);
panel_3.add(lblAddress);
JLabel lblMessage = new JLabel("MESSAGE");
lblMessage.setForeground(new Color(255, 255, 255));
lblMessage.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblMessage.setBounds(24, 256, 125, 38);
panel_3.add(lblMessage);
textField_2 = new JTextField();
textField_2.setBounds(154, 151, 404, 26);
panel_3.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(154, 188, 404, 26);
panel_3.add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(154, 228, 404, 26);
panel_3.add(textField_4);
textField_5 = new JTextField();
textField_5.setColumns(10);
textField_5.setBounds(154, 268, 404, 26);
panel_3.add(textField_5);
JPanel Administration = new JPanel();
Administration.setBackground(new Color(128, 0, 0));
frmUniveposV.getContentPane().add(Administration, "name_244816080502214");
Administration.setLayout(null);
JLabel lblNewLabel_7 = new JLabel("ADMINISTRATION");
lblNewLabel_7.setForeground(new Color(255, 255, 255));
lblNewLabel_7.setFont(new Font("Tahoma", Font.BOLD, 35));
lblNewLabel_7.setBounds(506, 0, 335, 43);
Administration.add(lblNewLabel_7);
JLabel lblCategories_1 = new JLabel("CATEGORIES");
lblCategories_1.setForeground(new Color(255, 255, 255));
lblCategories_1.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblCategories_1.setBounds(90, 88, 214, 43);
Administration.add(lblCategories_1);
JLabel lblSubcategories = new JLabel("SUB-CATEGORIES");
lblSubcategories.setForeground(new Color(255, 255, 255));
lblSubcategories.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblSubcategories.setBounds(528, 88, 292, 43);
Administration.add(lblSubcategories);
JLabel label = new JLabel("SUB-CATEGORIES");
label.setForeground(new Color(255, 255, 255));
label.setFont(new Font("Tahoma", Font.PLAIN, 35));
label.setBounds(1008, 88, 292, 43);
Administration.add(label);
JList list_1 = new JList();
list_1.setBounds(55, 142, 292, 404);
Administration.add(list_1);
JList list_2 = new JList();
list_2.setBounds(528, 142, 292, 404);
Administration.add(list_2);
JList list_3 = new JList();
list_3.setBounds(1008, 142, 292, 404);
Administration.add(list_3);
JButton btnAdd = new JButton("ADD");
btnAdd.setBounds(55, 557, 89, 23);
Administration.add(btnAdd);
JButton button_2 = new JButton("ADD");
button_2.setBounds(528, 557, 89, 23);
Administration.add(button_2);
JButton button_3 = new JButton("ADD");
button_3.setBounds(1008, 557, 89, 23);
Administration.add(button_3);
JButton btnRemove = new JButton("REMOVE");
btnRemove.setBounds(258, 557, 89, 23);
Administration.add(btnRemove);
JButton button_4 = new JButton("REMOVE");
button_4.setBounds(731, 557, 89, 23);
Administration.add(button_4);
JButton button_5 = new JButton("REMOVE");
button_5.setBounds(1211, 557, 89, 23);
Administration.add(button_5);
JButton btnReports = new JButton("REPORTS");
btnReports.setFont(new Font("Tahoma", Font.BOLD, 30));
btnReports.setBounds(10, 638, 180, 81);
Administration.add(btnReports);
JButton btnSettings = new JButton("SETTINGS");
btnSettings.setFont(new Font("Tahoma", Font.BOLD, 30));
btnSettings.setBounds(200, 638, 185, 81);
Administration.add(btnSettings);
JButton button_6 = new JButton("EXIT");
button_6.setFont(new Font("Tahoma", Font.BOLD, 11));
button_6.setBounds(1245, 682, 95, 37);
Administration.add(button_6);
}
}
Source code of database class is shown below
import java.awt.event.*;
import java.sql.*;
public class database {
Connection con;
Statement st;
ResultSet rs;
public database()
{
connect();
frame();
}
public void connect()
{
try{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db = "jdbc:odbc:db";
con = DriverManager.getConnection(db);
st = con.createStatement();
}
catch(Exception ex){
}
}
public void frame()
{
new UnivEPOS();
}
}

Java Swing GUI Invisible

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);

Categories

Resources