I'm using the Netbeans GUI builder, but it's a little confusing now. How can I add an image to a panel? I think i'm doing it correct, but it's not showing up. I think it should be in the init() method, but netbeans does not allow me to change that part of the code. This is the code I added for the image:
//these four lines I added to add the image
imageIcon = new ImageIcon("login_icon.png");
image = new JLabel(imageIcon);
image.setToolTipText("SGS Security");
topPanel.add(image);
My Class starts here:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Login.java
*
* Created on Oct 27, 2009, 8:34:15 PM
*/
package sgs;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Login extends javax.swing.JFrame {
JLabel image;
ImageIcon imageIcon;
/** Creates new form Login */
public Login() {
initComponents();
//these four lines I added to add the image
imageIcon = new ImageIcon("login_icon.png");
image = new JLabel(imageIcon);
image.setToolTipText("SGS Security");
topPanel.add(image);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
topPanel = new javax.swing.JPanel();
userLabel = new javax.swing.JLabel();
passwordLabel = new javax.swing.JLabel();
connectLabel = new javax.swing.JLabel();
forgotPassLabel = new javax.swing.JLabel();
forgotPassCheckBox = new javax.swing.JCheckBox();
cancelButton = new javax.swing.JButton();
okButton = new javax.swing.JButton();
passwordTextField = new javax.swing.JTextField();
userTextField = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(216, 156, 60));
topPanel.setBackground(new java.awt.Color(28, 90, 198));
javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
topPanel.setLayout(topPanelLayout);
topPanelLayout.setHorizontalGroup(
topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 406, Short.MAX_VALUE)
);
topPanelLayout.setVerticalGroup(
topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 71, Short.MAX_VALUE)
);
userLabel.setText("User name:");
passwordLabel.setText("Password:");
connectLabel.setText("Connect to SGS");
forgotPassLabel.setText("Forgot password");
forgotPassCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
forgotPassCheckBoxActionPerformed(evt);
}
});
cancelButton.setText("Cancel");
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
userTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
userTextFieldActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(connectLabel)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(passwordLabel)
.addComponent(userLabel)))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(userTextField)
.addComponent(passwordTextField)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(forgotPassCheckBox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(forgotPassLabel))
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
.addComponent(topPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(topPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(connectLabel)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(userLabel)
.addComponent(userTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(passwordLabel)
.addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(forgotPassLabel)
.addComponent(forgotPassCheckBox))
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void forgotPassCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void userTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cancelButton;
private javax.swing.JLabel connectLabel;
private javax.swing.JCheckBox forgotPassCheckBox;
private javax.swing.JLabel forgotPassLabel;
private javax.swing.JButton okButton;
private javax.swing.JLabel passwordLabel;
private javax.swing.JTextField passwordTextField;
private javax.swing.JPanel topPanel;
private javax.swing.JLabel userLabel;
private javax.swing.JTextField userTextField;
// End of variables declaration
}
Ditch the GUI builder and learn how to create GUIs on your own. That way you spend time learning Java instead of learning an IDE. There is probably some GroupLayout property that is not properly set and since GroupLayout was designed to be used by IDEs and not humans I have no idea what the problem might be.
The other possibility is that the IDE can't find your image. Did you add a System.out.println to diplay the image and make sure its not null.
I suggest you read the section from the Swing tutorial on How to Use Icons for a working example that you can download and test to see if it works. Just replace the icons in the tutorial with your icons to make sure they are found.
Edit:
After having a second look at the code I believe my original suggestion is correct. You attempt to add the label to the panel using a single line of code:
topPanel.add(image);
Look at the code generated by the IDE. The add statements are NOT that simple. If you want to manually add a component after the fact then you need read the section from the tutorial on "How to Use Group Layout" to understand the various constraints and methods used.
Or you need to figure out how to do it in the IDE. Thats why I prefer the do it yourself approach. Then you are responsible for the code, not the IDE.
This is one of the weird behaviors in Java (GUIs). You can manually paint the image on a panel.
Here is what I use:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2= (Graphics2D) g;
if (currentImage != null) {
g2.drawImage(currentImage, null, 0, 0);
}
}
Also you should create a "ImagePanel" component, which is an JPanel that encapsolates the Image.
The middle arguement for drawImage is null, because I do not intend on performing an image operation on it.
Related
This question already has answers here:
GUI Freezes During While Loop in Java
(1 answer)
The program freezes when i put a while loop in a action listener
(1 answer)
Closed 2 years ago.
I am attempting to redirect my system.out.println to dislpay to the JTextArea element of the GUI (hence it being public and static so that the methods that print something can print to it.
But once it gets to the line:
System.out.println("sdsdsdsdsa");
the program just does not go any further. The next two lines are:
System.setOut(printStream);
System.setErr(printStream);
which it does not seem to print out and when I exit the program this error appears:
"Exception: java.lang.NullPointerException thrown from the UncaughtExceptionHandler in thread "main""
and I am unable to verify whether printStream is null or not because the debugger does not seem to following into the action performed method once the start button (JButton1) is pressed.
Below is the main class in the GUI:
public class TrainingProgramme {
static PrintStream printStream = new PrintStream(new GUITextOutputStream(TrainingProgrammeGUI.jTextArea1));
public static void main(String[] args) {
TrainingProgrammeGUI gui = new TrainingProgrammeGUI();
gui.setVisible(true);
System.setOut(printStream);
System.setErr(printStream);
System.out.println("==================================\n"+
"* WELCOME TO CATCH ME IF YOU CAN *\n"+
"==================================\n");
}
GUI class:
package javaapplication2;
import java.awt.event.ActionEvent;
import java.io.PrintStream;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author viljo
*/
public class TrainingProgrammeGUI extends javax.swing.JFrame {
static PrintStream printStream = new PrintStream(new GUITextOutputStream(TrainingProgrammeGUI.jTextArea1));
double startTimer = System.nanoTime();
double stopTimer = 0;
Scanner scan = new Scanner(System.in);
Player gameCharacter = null;
Teacher teacher = null;
int finishedProgram = 0;
int userChoice = -1;
/**
* Creates new form TrainingProgrammeGUI
*/
public TrainingProgrammeGUI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767));
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jButton4 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
jLabel1.setText("CIA Training Programme");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel2.setText("Game: ");
jButton1.setText("Start Game");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton3.setText("What is this game?");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel3.setText("Type Here: ");
jButton4.setText("Exit");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(159, 159, 159))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(261, 261, 261)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(94, 94, 94)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(170, 170, 170)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(175, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(70, 70, 70)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
.addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(20, 20, 20))
);
pack();
}// </editor-fold>
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
TrainingProgrammeGUI programme = new TrainingProgrammeGUI();
programme.setVisible(true);
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.print("\nThank you for using our training programme!");
System.exit(0);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("sdsdsdsdsa");
System.setOut(printStream);
System.setErr(printStream);
System.out.println("==================================\n"+
"* WELCOME TO CATCH ME IF YOU CAN *\n"+
"==================================\n");
userChoice = TrainingProgramme.checkInputWithPhrase("1.) Start Game\n2.) What is this game?\n3.) Quit", 1, 3);
teacher = new Teacher("John Doe", "Teacher");
gameCharacter = teacher.introductionLines(); // teacher reads a few lines and asks questions then returns Player class
boolean userFinished = false;
gameCharacter.getHotelRoom().setPlayerHere(true);
do { // Checks which room player is in then reads instructions for room. Loop finishes when player wants to chooses suspect
if (gameCharacter.getHotelKitchen().isPlayerHere()) {
if ("HotelRoom".equals((gameCharacter.getHotelKitchen().instructions(teacher)))) {
System.out.println("\nRoom chosen!\n");
gameCharacter.getHotelKitchen().setPlayerHere(false); // Takes player out of current room
gameCharacter.getHotelRoom().setPlayerHere(true); // Puts players into chosen room
}
} else {
if ("Choose".equals((gameCharacter.getHotelRoom().instructions(teacher)))) {
stopTimer = System.nanoTime();
Player.timeTaken = (stopTimer - startTimer) / 10000000; //time taken to finish inspecting items (in seconds)
teacher.chooseWhoDidIt(gameCharacter);
finishedProgram = 1; //Stop the curret loop
userFinished = true; //Stop the main menu loop
} else {
System.out.println("\nKitchen chosen!\n");
gameCharacter.getHotelRoom().setPlayerHere(false);// Takes player out of current room
gameCharacter.getHotelKitchen().setPlayerHere(true);// Puts players into chosen room
}
}
} while (userFinished == false);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("\n+===========================================================+");
System.out.println("|This game was created to help train our new junior recruits|");
System.out.println("+===========================================================+\n\n");
System.out.println("Objective: Gather evidence by looking for clues\n"+
"and once you are done go back to the room to guess \n"+
"which suspect did it. Suspects are provided once you have\n"+
"went back to the room and decided to guess who did the crime!\n");
}
// Variables declaration - do not modify
private javax.swing.Box.Filler filler1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
public static javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
PrintStream class:
package net.codejava.swing;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
/**
* This class extends from OutputStream to redirect output to a JTextArrea
* #author www.codejava.net
*
*/
public class CustomOutputStream extends OutputStream {
private JTextArea textArea;
public CustomOutputStream(JTextArea textArea) {
this.textArea = textArea;
}
#Override
public void write(int b) throws IOException {
// redirects data to the text area
textArea.append(String.valueOf((char)b));
// scrolls the text area to the end of data
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
Im trying to learn Java and i have now met Jpanels, and since i'm using Netbeans i can use their "constructor" in which i can design the panel i want inside my frame.
With netbeans i made a panel inside my panel, and now i wonder:
How do i draw on a panel inside a panel?
(The panel i want to draw on is EvoPanel)
This is my current design
The dark gray box is the second panel inside the panel, if i try to draw a line from the top left corner to the bottom right i end up with this
My code:
Vindue.java
package vindue;
import javax.swing.*;
/**
*
* #author BE56df
*/
public class Vindue {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
paneler panel = new paneler();
JFrame vindue = new JFrame("NEZ - Evolution Simulator v0.1");
vindue.add(panel);
vindue.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
vindue.setResizable(false);
vindue.pack();
vindue.setVisible(true);
}
}
Paneler.java
package vindue;
import java.awt.*;
import javax.swing.*;
/**
*
* #author BE56df
*/
public class paneler extends javax.swing.JPanel {
/**
* Creates new form paneler
*/
public paneler() {
initComponents();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(0, 0, 750, 550);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
EvoPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jToggleButton1 = new javax.swing.JToggleButton();
setMaximumSize(new java.awt.Dimension(750, 550));
setMinimumSize(new java.awt.Dimension(750, 550));
setPreferredSize(new java.awt.Dimension(750, 550));
EvoPanel.setBackground(new java.awt.Color(200, 200, 200));
EvoPanel.setMaximumSize(new java.awt.Dimension(550, 550));
EvoPanel.setMinimumSize(new java.awt.Dimension(550, 550));
javax.swing.GroupLayout EvoPanelLayout = new javax.swing.GroupLayout(EvoPanel);
EvoPanel.setLayout(EvoPanelLayout);
EvoPanelLayout.setHorizontalGroup(
EvoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 550, Short.MAX_VALUE)
);
EvoPanelLayout.setVerticalGroup(
EvoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 550, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setText("Control panel");
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToggleButton1.setText("Auto play");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(EvoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(63, 63, 63)
.addComponent(jLabel1)
.addGap(0, 58, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
.addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(EvoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jToggleButton1))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JPanel EvoPanel;
private javax.swing.JLabel jLabel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}
Obviously you paint the line on the main JFrame (paneler panel) when you really wants to paint on EvoPanel. You should create a class EvoPanel (with a paintComponent method) and add after paneler#initComponents.
public paneler() {
initComponents();
EvoPanel evoPanel = new EvoPanel();
add(evoPanel) // properly in a layout
}
Advice: for simple GUI's don't use Mantisse (Netbeans GUI builder tool), it won't give you any benefit.
So let's start with some background this is for a first year collage project. the GUI is built so one class, the interface-handler calls all the JPanels and puts them in the right place. This is for structure and so it's easy to maintain.
the problem is the panel holding the menu does't show it's components after it's added. i'll include some image's so it's easier to understand.
The panel in question is called MenuPanel.
so here is the login button which currently only clears the contentpanel and then adds the menupanel.
Login page where the button is located
and here is the menu panel empty
here is the menu panel all empty
the code
The interface handler
you can see me messing around with the .repaint() method in the menu method already.
public class InterfaceHandler {
private static InterfaceHandler singleton;
/**
* Checks if there is already a instance running of InterfaceHandler if so
* return the instance if not create a instance.
*
* #return Instance
*/
public static InterfaceHandler instance() {
if (singleton == null) {
singleton = new InterfaceHandler();
}
return singleton;
}
private ContentPanel contentPanel;
private LoginPanel loginPanel;
private MainFrame mainFrame;
private MainPanel mainPanel;
private MenuPanel menuPanel;
/**
* Initializes default Interface
*/
public InterfaceHandler() {
initComponents();
}
/**
* Initializes GUI Components that will be needed now or in the future.
*/
public void initComponents() {
mainFrame = new MainFrame();
mainPanel = new MainPanel();
contentPanel = new ContentPanel();
loginPanel = new LoginPanel();
menuPanel = new MenuPanel();
contentPanel.setBounds(100, 100, 860, 700);
menuPanel.setBounds(10, 10, 80, 400);
loginPanel.setBounds(280, 250, 300, 200);
mainFrame.add(mainPanel);
mainPanel.add(contentPanel);
mainFrame.setVisible(true);
contentPanel.add(loginPanel);
}
/**
* Clears the ContentPanel of all components
*/
public void clear() {
System.out.println("Clear Ran");
contentPanel.removeAll();
contentPanel.repaint();
}
/**
* Adds the Login Screen.
*/
public void login() {
System.out.println("Login Ran");
contentPanel.add(loginPanel);
contentPanel.repaint();
}
/**
* Adds the menu.
*/
public void menu() {
System.out.println("Menu Ran");
mainPanel.sidePanel.add(menuPanel);
menuPanel.repaint();
mainPanel.repaint();
mainPanel.sidePanel.repaint();
}
}
And here is the code for the panel, Most of it is autogenerated by the netbeans design editor.
so it's ugly (i'm sorry)
public class MenuPanel extends javax.swing.JPanel {
/**
* Creates new form MenuPanel
*/
public MenuPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jLabel2 = new javax.swing.JLabel();
baggageButton = new javax.swing.JLabel();
jLabel1.setBackground(new java.awt.Color(204, 204, 204));
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Menu");
jSeparator1.setBackground(new java.awt.Color(204, 204, 204));
baggageButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
baggageButton.setText("Baggage");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator1)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(baggageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(baggageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel baggageButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JSeparator jSeparator1;
// End of variables declaration
}
just to clarify, the method menu is called from a different class.
If you are adding components to an already visible panel, you need to call validate().
public void menu() {
System.out.println("Menu Ran");
mainPanel.sidePanel.add(menuPanel);
mainPanel.sidePanel.validate(); // try this
menuPanel.repaint();
mainPanel.repaint();
mainPanel.sidePanel.repaint();
}
(This answer is wrong but the ensuing comments do help understand why #whiskeyspider's answer is right)
In this code:
mainFrame.add(mainPanel);
mainPanel.add(contentPanel);
mainFrame.setVisible(true);
contentPanel.add(loginPanel);
You're not adding menuPanel anywhere. It is added (too late) in the menu() method, but that's not being called.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I need to have an interface ready and with clickable buttons to show all the different windows we have created so far.
Now a teammember of mine has created multiple JFrames for each window we want the user to work with. We're all working in the NetBeans IDE. I'm trying to get a login window to popup and from there on i will be making all the windows show up when a button is clicked which should direct the user to that window.
Here is what we have so far:
(Main)
import java.awt.*;
import javax.swing.*;
public class Corendon {
public static void main(String[] args) {
showLogin();
}
private static void showLogin(){
Login login = new Login();
login.setVisible(true);
login.validate();
}
}
The code in Login.java as requested by a user:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Daan
*/
public class Login extends javax.swing.JPanel {
/**
* Creates new form Login
*/
public Login() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jPasswordField1 = new javax.swing.JPasswordField();
jTextField1 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Daan\\Dropbox\\HvA\\Programming\\Corendon\\corendon.png")); // NOI18N
jLabel2.setText("Username");
jLabel3.setText("Password");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dutch", "English" }));
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Cancel");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jLabel1))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(45, 45, 45)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(22, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(16, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(35, 35, 35))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
As you can see i'm trying to get the Login window to showup when the user runs the program. But what i'm currently dealing with is the fact that if i set the setVisible parameter to true, it doesn't actually show up. The program will in fact just run for 1 second and than stop.
Is there anything i'm doing wrong here? Also in each of the classes we have not yet created any event handlers. They are only windows with multiple components on them.
JPanel needs to be wrapped inside a Window component such as a JFrame to show up.
Replace your showLogin Method with the following:
Login login = new Login();
JFrame frame = new JFrame();
frame.add(login);
frame.pack();
frame.setVisible(true);
What I am trying to do is allow the user to select an option from a drop down menu and then select a radio button and then calculate their results to appear in a textfield or I guess a new panel. I don't want all of the code, because I am trying to learn Java myself, but any tips pointers or sample code would be great. Basically, if they choose the first option on the drop down and the radio button the result should return a 'LOW', I've been trying to use NetBeans, so I think I am confusing myself more. I'm wondering if I can store the drop down variables as a character, such as A-E and the radio buttons as 1-5 and then in the calculate button do an if statement or something? How do I declare the textfield or panel to get the results to show there? So like a*1 = 'low' b*2='low' e*5='high'
The drop down menu represents the likelihood of the risk happening, and the radio buttons represents the consequence of the risk. The two get multiplied together to determine the risk at hand. The program is something I made in PHP but I am trying to make it into a java program as I am trying to learn Java.
The likelihood of the risk happening holds a probability. From the Remote chance probability is .1, .3, .5, .7, and .9 chance for a 'Near Certainty' option. The consequence holds a cost value. For minimal or no impact has no cost value, the 2nd button holds a cost value of <5, 5-7%, 7-10% and >10% for the last button (Can't achieve key milestone. It was easy to do in PHP as I was able to just make a database that holds these values and then send a query when the two choices were chosen. Would it be easier to just give them numeric values to multiply the two?
The reason for the two different data collections was because I was trying to make the interface clean. The website is hosted at timesdomain.com if you'd care to take a look at how the function runs web based. You'll have to register, but it holds no value as it is being deleted soon. Don't worry about your password being taken because it is stored in a 40 bit hash with a 4 digit salt, so there is literally no way for me to know it ;). I have most of my programming experience in databases and php, so the whole java language is still new. I'd like to make my program I made web based into an application based. I hope it makes more sense.
Here is my code.
package riskcalculator;
public class Gui extends javax.swing.JFrame {
/** Creates new form Gui */
public Gui() {
initComponents();
}
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Consequence = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton4 = new javax.swing.JRadioButton();
jRadioButton5 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel1.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
jLabel1.setText("Risk Claculator");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose a Liklihood", "Remote Chance", "Unlikely Chance", "Likely Chance", "Highly Likely Chance", "Near Certainty Chance" }));
Consequence.add(jRadioButton1);
jRadioButton1.setText("Minimal or no impact");
Consequence.add(jRadioButton2);
jRadioButton2.setText("Additional resources required, able to satisfy");
Consequence.add(jRadioButton3);
jRadioButton3.setSelected(true);
jRadioButton3.setText("Minor slip in key milestones; not able to meet need date");
Consequence.add(jRadioButton4);
jRadioButton4.setText("Major slip in key milestones or critical path impacted");
Consequence.add(jRadioButton5);
jRadioButton5.setText("Can't achieve key milestone");
jButton1.setText("Calculate Risk");
jButton1.setActionCommand("Calculate Risk");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.setBorder(new javax.swing.border.MatteBorder(null));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 186, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 70, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(26, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3)
.addComponent(jRadioButton4)
.addComponent(jRadioButton5))
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(99, 99, 99)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(110, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jLabel1)
.addGap(92, 92, 92))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(80, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(29, 29, 29)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton5)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(29, 29, 29))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup Consequence;
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JRadioButton jRadioButton5;
// End of variables declaration
}
For your current problem at hand, the issue in a nutshell is how to get the selected item from a JComboBox and a collection of JRadioButtons when a JButton is pressed. What you do with this information is dependent on your program logic and really isn't the issue here. To learn do to this, I suggest that you go to the Swing tutorials for details. Briefly though, the selection from a JComboBox is obtained simply by calling getSelectedItem on the combo box. For a JRadioButton collection it's a little trickier, but I've always liked getting the selection from the ButtonGroup that holds the JRadioButtons together. The methods for this and for the combo box are shown in the example below:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GetInfoFromCombosRadios extends JPanel {
private static final String[] STUFF_A = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private static final String[] STUFF_B = {"Fe", "Fi", "Fo", "Fum"};
private JComboBox comboStuffA = new JComboBox(STUFF_A);
private ButtonGroup btnGroupStuffB = new ButtonGroup();
public GetInfoFromCombosRadios() {
JPanel radioPanel = new JPanel(new GridLayout(0, 1, 0, 5));
for (String stuffB : STUFF_B) {
JRadioButton radioBtn = new JRadioButton(stuffB);
radioBtn.setActionCommand(stuffB);
btnGroupStuffB.add(radioBtn);
radioPanel.add(radioBtn);
}
JPanel comboPanel = new JPanel();
comboPanel.add(comboStuffA);
JButton doItBtn = new JButton("Do It!");
doItBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
doItActionPerformed();
}
});
JPanel doItBtnPanel = new JPanel();
doItBtnPanel.add(doItBtn);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(comboPanel);
add(radioPanel);
add(doItBtnPanel);
}
private void doItActionPerformed() {
String comboSelection = comboStuffA.getSelectedItem().toString();
System.out.println("Combo Selection: " + comboSelection);
String radioSelection = "";
ButtonModel selectedBtnModel = btnGroupStuffB.getSelection();
if (selectedBtnModel == null) {
radioSelection = "No radio button selected yet";
} else {
radioSelection = selectedBtnModel.getActionCommand();
}
System.out.println("Radio selection: " + radioSelection);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("GetInfoFromCombosRadios");
frame.getContentPane().add(new GetInfoFromCombosRadios());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
From scanning the code, it looks like you've got the components initialized and laid out in the container.
In order to make the interface interactive, you need to look into the other big part of Java Swing interfaces, the event listeners.
Basically, everything you do (clicks, value changes, moving the mouse around, pressing keys, etc.) generates an event. You then write Event Listeners that react to those events.
Check out this intro to the topic.