Java / Swing / WebcamCapture : When I disable a JLabel, an other image appears - java

I come to you because I have a strange issue, for which I don't find any solution...
I build an application using a webcam, in order to take some photographs.
I use WebcamCapture to do that, and I don't encounter any issues with it.
The only "weird" thing that happens is the following :
I use a JDialog in which I make photograph. In its JFrame parent, I display those photographs in JLabel.
Then, i need to disable those JLabel, and I do that by calling a method which disable all components. The weird thing is, when I disable JLabel, the JLabel display the last image capture by the webcam. Not the last photographs, but really the last captured image.
It's seems that BufferedImage (used by WebcamPanel) are linked to the issue.
Here is the SSCE :
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
#SuppressWarnings("serial")
public class CameraFrame extends JFrame implements ActionListener{
public Webcam webcam;
Boolean enabled = true;
CameraFrame frame;
private JButton btnSaveVerso;
private JLabel lblVerso;
private JButton btnEnable;
private JButton btnQuit;
private JPanel mainPanel;
private WebcamPanel streamPanel;
public static void main(String[] args){
CameraFrame frame = new CameraFrame();
frame.setVisible(true);
}
public CameraFrame() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setMinimumSize(new Dimension(800, 600));
setPreferredSize(new Dimension(800,600));
buildPanel();
setContentPane(mainPanel);
}
});
}
public void buildPanel() {
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
Border blackline = BorderFactory.createLineBorder(Color.black, 1, true);
webcam = Webcam.getDefault();
webcam.open();
streamPanel = new WebcamPanel(webcam);
streamPanel.setPreferredSize(new Dimension(webcam.getViewSize()));
streamPanel.setMaximumSize(new Dimension(webcam.getViewSize()));
btnSaveVerso = new JButton("Take pic");
btnSaveVerso.setActionCommand("take");
btnSaveVerso.addActionListener(this);
lblVerso = new JLabel("Here will be the pic taken by the camera");
lblVerso.setBorder(blackline);
btnEnable = new JButton("Disable");
btnEnable.setActionCommand("disable");
btnEnable.addActionListener(this);
btnQuit = new JButton("Quit");
btnQuit.setActionCommand("quit");
btnQuit.addActionListener(this);
mainPanel.add(streamPanel);
mainPanel.add(btnSaveVerso);
mainPanel.add(lblVerso);
mainPanel.add(btnEnable);
mainPanel.add(btnQuit);
}
#Override
public void actionPerformed(final ActionEvent e) {
Thread newThread = new Thread(){
public void run(){
if(e.getActionCommand().equals("take")){
ImageIcon icon = new ImageIcon(webcam.getImage().getScaledInstance(100, 150, Image.SCALE_SMOOTH ));
lblVerso.setIcon(new ImageIcon(icon.getImage()));
lblVerso.revalidate();
lblVerso.repaint();
}
else if(e.getActionCommand().equals("disable")){
if(enabled){
lblVerso.setEnabled(false);
enabled = false;
btnEnable.setText("Enable");
}
else{
lblVerso.setEnabled(true);
enabled = true;
btnEnable.setText("Disable");
}
}
}
};
newThread.run();
if(e.getActionCommand().equals("quit")){
webcam.close();
this.setVisible(false);
}
}
}
I hope you will compile it without issues. Don't forget to link the librairies.
Thanks in advance

I finally resolved the problem : you simply need to close the webcam after each pictures, as follows :
BufferedImage picture = webcam.getImage();
webcam.close();
webcam.open();
... Do what you need with picture
(You don't even need to convert BufferedImage picture in an other type.)

Related

Insert a GIF image into JFrame and make it visible and invisible on demand

In my application I am extracting data from a project DB. When I do that I want to signal to user that data extraction is in progress. I want to achieve this by making my current frame view.getFrame2() invisible and making view.getFrame3() visible. Frame 3 will have GIF image with frame titled as "Extraction is in progress". I am able to achieve my target but I am not able to view the image in the frame. It's blank.
Below is the code snippet I am using; one class is used for view, another one for controller and yet another one for the model. I also have a main class to initialize all the M-V-C classes. I don't want to complicate the code by creating more classes.
View
My View class:-
/** View**/
package mvc.views;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import javax.swing.*;
public class View {
JFrame frame2, frame3;
JPanel panel3;
Toolkit tk;
Image icon;
Color background;
public View() {
/** processing image view **/
frame3 =new JFrame();
frame3.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame3.setTitle( "Extraction in process...." );
try {
icon = new ImageIcon("C:\\Users\\pc033k\\Desktop\\gears_animated.gif").getImage();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
panel3 = new JPanel();
background = Color.white;
panel3.setOpaque(true);
frame3.setContentPane(panel3);;
frame3.setBounds(100, 100, 400, 400);
frame3.setLocationRelativeTo(null);
frame3.setVisible(false);
/** End of processing image view **/
}
public void paint (Graphics g)
{
panel3.paint(g);
panel3.setBackground(background);
g.drawImage(icon,100,100,500,500,null);
}
}
/** End of View**/
Controller
/** Start Controller**/
package mvc.controllers;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import mvc.models.*;
import mvc.views.*;
public class Controller {
public Model model;
public View view;
public ActionListener myButtonListener;
//public MyDateListener listener;
boolean status, process1;
public String user, password, FN, LN, type;
JTextField text1, text2;
JCalendarCombo cal1, cal2;
public Date date1, date2;
public Controller(Model model, View view) {
this.model = model;
this.view = view;
}
public class MyButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == view.getGetcrmdataButton()){
FN = view.getUserText1().getText();
LN = view.getUserText2().getText();
date1 = view.getCalendar2().getDate();
date2 = view.getCalendar3().getDate();
type = (String) view.getComb1().getSelectedItem();
view.getFrame2().dispose();
view.getFrame3().setVisible(true);
view.getFrame3().repaint();
try {
process1 = model.CRMDataExtract(FN, LN, date1, date2, type);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(process1 == true){
view.getFrame3().setVisible(false);
view.getFrame2().setVisible(true);
JOptionPane.showMessageDialog((Component) (e.getSource()),
"pealse Check the output file for the data");
}
else
{
view.getFrame3().setVisible(false);
view.getFrame2().setVisible(true);
JOptionPane.showMessageDialog((Component) (e.getSource()),
" No Records found or Data Extraction failed!!");
}
}
}
}
I want to achieve this by making my current frame view.getFrame2() invisible and make a view.getFrame3() visible.
Don't be hiding/showing different windows. Users don't like frames disappearing. Instead you can simply display a modal JDialog containing your image on top of your main frame.
Or another approach is to use a GlassPane that paints over top of your main frame. See Disabled Glass Pane for an example of this approach. The existing example doesn't use an Icon, but you can easily change that.
You can use a label for insert the image.
Also I think CardLayout more suitable to your work. Here is some sample code. Consider that the Sample is a JFrame. You will see the Panel changes as you press the "Next" button.
public Sample() {
p = new JPanel();
p.setLayout(new CardLayout());
p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(new JLabel(new ImageIcon(new URL("http://icons.iconarchive.com/icons/chrisbanks2/cold-fusion-hd/128/ace-of-spades-icon.png"))));
p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(new JLabel(new ImageIcon(new URL("http://icons.iconarchive.com/icons/chrisbanks2/cold-fusion-hd/128/ace-of-hearts-icon.png"))));
p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(new JLabel(new ImageIcon(new URL("http://icons.iconarchive.com/icons/chrisbanks2/cold-fusion-hd/128/angrybirds-2-icon.png"))));
p.add(p1, "0");
p.add(p2, "1");
p.add(p3, "2");
b = new Button("Next");
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
rotator++;
if (rotator == 3) {
rotator = 0;
}
CardLayout layout = (CardLayout) p.getLayout();
layout.show(p, "" + rotator);
System.out.println("Rotator " + rotator);
}
});
add(p, "Center");
add(b, "North");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Blank JFrame and No JPanel appeared but already added

Can anyone help me? Whenever I ran the codes below, it always returns a blank frame, I don't know where I did wrong. Can you guys help me debug this? I already added the components to the panel, and the panel to the frame, but still it returns a blank output.
Here is the output I'm getting:
While this is what is required.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import javax.swing.BorderFactory;
import javax.swing.UIManager;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JRadioButton;
/**
*
* #author Chareux
*/
//Declaring Variables
public class TestUI {
private JFrame frm_main;
private JPanel sr_pnl;
private JLabel sr_lbl;
private JLabel sr_lbl2;
private JLabel ret_optn_lbl;
private JLabel ret_rsn_lbl;
private ButtonGroup ret_ops;
private JTextField sr_txtnum;
private JTextField sr_ret_txtrsn;
private JButton sr_start;
private JRadioButton ret_optn_rdbn_y;
private JRadioButton ret_optn_rdbn_n;
public TestUI(){
start();
}
public void start(){
//Creating the JFrame
frm_main = new JFrame("Service Desk SR Tool");
frm_main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm_main.setSize(500,450);
frm_main.setLocationRelativeTo(null);
frm_main.setResizable(false);
frm_main.setVisible(true);
// the Panel
sr_pnl = new JPanel();
//Components
sr_lbl = new JLabel("SERVICE DESK SR TIMER!");
sr_lbl2 = new JLabel("SR number: ");
sr_txtnum = new JTextField("Enter SR number here..",20);
ret_optn_lbl = new JLabel("Returning Ticket?");
ret_optn_rdbn_y = new JRadioButton("Yes");
ret_optn_rdbn_n = new JRadioButton("No");
ret_rsn_lbl = new JLabel("Reason: ");
sr_ret_txtrsn = new JTextField("Enter Reason number here..",20);
sr_start = new JButton("START!");
//adding the Components to the panel
sr_pnl.add(sr_lbl);
sr_pnl.add(sr_lbl2);
sr_pnl.add(sr_txtnum);
sr_pnl.add(ret_optn_lbl);
sr_pnl.add(ret_optn_rdbn_y);
sr_pnl.add(ret_optn_rdbn_n);
sr_pnl.add(ret_rsn_lbl);
sr_pnl.add(sr_ret_txtrsn);
sr_pnl.add(sr_start);
frm_main.add(sr_pnl,BorderLayout.CENTER);
//ButtonGroup for the radio button
ret_ops = new ButtonGroup();
ret_ops.add(ret_optn_rdbn_y);
ret_ops.add(ret_optn_rdbn_n);
}
public static void main(String[] args) {
new TestUI();
}
}
I'd recommend to use a nested or compound layout for this task. See further tips in comments in the source.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class SRTool {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new GridLayout(0,1,6,6));
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
// show the BG
gui.setBackground(Color.CYAN);
// center the label text
gui.add(new JLabel(
"Service Desk SR Tool", SwingConstants.CENTER));
// create a lyout that can center multiple components
FlowLayout layout = new FlowLayout(FlowLayout.CENTER,5,5);
JPanel srPanel = new JPanel(layout);
gui.add(srPanel);
srPanel.add(new JLabel("SR:"));
srPanel.add(new JTextField(8));
JPanel returnTicketPanel = new JPanel(layout);
gui.add(returnTicketPanel);
returnTicketPanel.add(new JLabel("Returning Ticket?"));
returnTicketPanel.add(new JCheckBox());
JPanel reasonPanel = new JPanel(layout);
gui.add(reasonPanel);
reasonPanel.add(new JLabel("Reason:"));
reasonPanel.add(new JTextField(14));
JPanel buttonPanel = new JPanel(layout);
gui.add(buttonPanel);
buttonPanel.add(new JButton("Start!"));
JFrame f = new JFrame("Demo");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See https://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.
Add frm_main.validate() in the end of start()
public void start(){
/*
...
Same As Above
...
*/
frm_main.add(sr_pnl,BorderLayout.CENTER);
//ButtonGroup for the radio button
ret_ops = new ButtonGroup();
ret_ops.add(ret_optn_rdbn_y);
ret_ops.add(ret_optn_rdbn_n);
frm_main.validate(); // Add this line ******
}

Tanslucent Java Chat showing duplicate components?

Pretty much, once I make my JTextArea and my JTextField transparent, as I type it looks as if all of my components are being duplicated and added to the screen. Am I doing something wrong, or is this a NetBeans bug?
package game;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* #author xDaegothx
*/
public class Game extends JFrame
{
JLayeredPane LP;
Game_Chat GC;
public Game()
{
LP = new JLayeredPane();
LP.setBackground(Color.black);
LP.setOpaque(true);
GC = new Game_Chat();
GC.setBounds(0,350,400,250);
LP.add(GC);
this.getContentPane().add(LP);
this.setBounds(0,0,1200,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args)
{
new Game();
}
public class Game_Chat extends JLabel
{
JTextArea TA;
JScrollPane JSP;
JTextField TF;
JButton Submit_btn;
public Game_Chat()
{
TA = new JTextArea();
TA.setForeground(new Color(255,255,255,0));
TA.setBackground(new Color(255,255,255,0));
TA.setOpaque(true);
TA.setText("Welcome to 'Game'!");
JSP = new JScrollPane(TA);
JSP.setOpaque(true);
JSP.setForeground(new Color(255,255,255,0));
JSP.setBackground(new Color(255,255,255,0));
JSP.setBounds(0,0,400,225);
TF = new JTextField();
TF.setOpaque(true);
//TF.setBackground(new Color(255,255,255,0));
TF.setBounds(0,225,350,25);
Submit_btn = new JButton("Send");
Submit_btn.setBorder(null);
Submit_btn.setBounds(350,225,50,25);
TF.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
Submit();
}
});
Submit_btn.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent me)
{
Submit();
}
});
add(JSP);
add(TF);
add(Submit_btn);
setBackground(Color.gray);
setOpaque(true);
}
public void Submit()
{
String charname = "MyName";
TA.append("\n"+charname+": "+TF.getText());
}
}
}
What is the point of setting both the foreground and background transparent? You will never see the text if it is transparent!
Anyway to make a component completely transparent you don't play with the background. Instead you just use:
textArea.setOpaque(false);
If you want partially transparent backgrounds then you do use the setBackground() method. But you will have painting problems. See Backgrounds With Transparency for an explanation of the problems and some potential solutions.
Also, you should NOT be using setBounds() to set the size/location of a component. Swing was designed to be used with layout managers. So take the time to learn how to use them for better functioning programs.

Need Help in debugging a simple swing app using JLayeredPane

I am trying to develop a swing application which looks like the following :-
(The demo for this application can be found here)
The black square moves as the cursor is moved .The ComboBox entry selects the topmost layer in the stack,above which the square will appear during movement but below the remaining next top layers.
I have completed it nearly but the black square(image) is vibrating when mouse is moved (motion is not smooth) and also its motion is confined to small area and it is not moving over complete layered pane.Also there is no effect of changing settings in ComboBox or CheckBox.
Here is the code :-
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JLayeredPaneDemo implements ActionListener, MouseMotionListener{
private String layerString[]={"Red(0)","Yellow(1)","Blue(2)","Green(3)","Orange(4)"};
private Color layerColors[]={Color.RED,Color.YELLOW,Color.BLUE,Color.GREEN,Color.ORANGE};
JComboBox jcb;
JCheckBox jcheck;
JLayeredPane layeredPane;
ImageIcon img;
JLabel cursor;
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){public void run(){new JLayeredPaneDemo();}});
}
public JLayeredPaneDemo()
{
JFrame frame=new JFrame("JLayeredPane Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2,1));
frame.add(createControlPanel());
//frame.add(Box.createRigidArea(new Dimension(0, 10)));
frame.add(createLayeredPane());
try
{
img=new ImageIcon(ImageIO.read(new File("src/watermark.png")));
}catch(IOException e){System.out.println("ERROR LOADING IMAGE :"+ e);}
cursor=new JLabel(img);
cursor.setBounds(45,45,img.getIconWidth(),img.getIconHeight());
cursor.addMouseMotionListener(this);
layeredPane.add(cursor,new Integer(2),0);
frame.pack();
frame.setVisible(true);
}
public JPanel createControlPanel()
{
JPanel panel=new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Select the Settings"));
jcb=new JComboBox(layerString);
jcb.setSelectedIndex(0);
jcb.setActionCommand("LAYER_SETTING");
jcb.addActionListener(this);
jcheck=new JCheckBox();
jcheck.setSelected(true);
jcheck.setActionCommand("FRONT_BACK");
jcheck.addActionListener(this);
panel.add(jcb);
panel.add(jcheck);
return panel;
}
public JLayeredPane createLayeredPane()
{
layeredPane=new JLayeredPane();
layeredPane.setBorder(BorderFactory.createTitledBorder("Layered Pane : Move Your Mouse"));
layeredPane.setPreferredSize(new Dimension(300,310));
Point origin=new Point(20,20);
int offset=35;
for(int i=0;i<layerString.length;i++)
{
JLabel label=createColoredLabel(layerString[i],layerColors[i],origin);
layeredPane.add(label,new Integer[i]);
origin.x+=offset;
origin.y+=offset;
}
return layeredPane;
}
private JLabel createColoredLabel(String str,Color color,Point origin)
{
JLabel label =new JLabel(str);
label.setVerticalAlignment(JLabel.TOP);
label.setHorizontalAlignment(JLabel.CENTER);
label.setOpaque(true);
label.setBackground(color);
label.setForeground(Color.BLACK);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
label.setBounds(origin.x,origin.y,100,100);
return label;
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("LAYER_SETTING"))
{
int position=jcheck.isSelected() ? 0 :1;
layeredPane.setLayer(cursor,jcb.getSelectedIndex(),position);
}
else if(cmd.equals("FRONT_BACK"))
{
if(jcheck.isSelected())
layeredPane.moveToFront(cursor);
else
layeredPane.moveToBack(cursor);
}
}
#Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
//Do nothing
}
#Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
cursor.setLocation(e.getX(),e.getY());
}
}
Please Help me to find the problem.
Your mouseMoved() implementation no longer applies the offsets used in the original; you'll need to supply them based on the geometry of the Icon used in your cursor.
//Adjustments to put Duke's toe at the cursor's tip.
private static final int XFUDGE = 40;
private static final int YFUDGE = 57;
...
//Make Duke follow the cursor.
public void mouseMoved(MouseEvent e) {
dukeLabel.setLocation(e.getX()-XFUDGE, e.getY()-YFUDGE);
}
Addendum: I don't understand why I didn't get any error when I used the wrong brackets? Can anyone point out the reason, please.
layeredPane.add(label,new Integer(i));
Because the language defines an array as an Object, the compiler chose the matching available add() method:
public void add(Component comp, Object constraints);
There are two silly mistakes which consumed my 11 precious hours.
1)I set actionListener for 'cursor' instead of layeredPane and that's why the movement was confined to a small area.
2)Secondly,i wrote layeredPane.add(label,new Integer[i]); instead of layeredPane.add(label,new Integer(i)); .(Notice the square brackets).

How to display different components in a JFrame?

I am very new to Java AWT. My question header must seem ridiculous to you, sorry about that. In my application I have three buttons which display different threads when clicked on. Now I want to add maybe a button or checkboxes or choicelist, etc when clicked on a particular button. For eg, if I click on yes button, it should display a choice list, something like that. How do I achieve something like that? Here is my code so far:
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AppWindow extends Frame implements ActionListener{
String keymsg = "Test message";
String mousemsg = "Nothing";
int mouseX=30, mouseY=30;
String msg;
public AppWindow(){
//addKeyListener(new MyKeyAdapter(this));
//addMouseListener(new MyMouseAdapter(this));
addWindowListener(new MyWindowAdapter());
}
public void paint(Graphics g){
g.drawString(msg, 150, 100);
}
//Here the window is created:
public static void main(String args[]){
AppWindow appwin = new AppWindow();
appwin.setSize(new Dimension(300,200));
appwin.setTitle("My first AWT Application");
appwin.setLayout(new FlowLayout(FlowLayout.LEFT));
appwin.setVisible(true);
Button yes,no,maybe;
yes = new Button("yes");
no = new Button("no");
maybe = new Button("maybe");
appwin.add(yes);
appwin.add(no);
appwin.add(maybe);
yes.addActionListener(appwin);
no.addActionListener(appwin);
maybe.addActionListener(appwin);
}
#Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
String str = ae.getActionCommand();
if(str.equals("yes")){
msg = "You pressed Yes";
}
if(str.equals("no")){
msg = "You pressed No";
}
if(str.equals("maybe")){
msg = "You pressed Maybe";
}
repaint();
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
Points describing what you should be doing :
As already mentioned by others, better to use Swing over AWT, since Swing is more advanced.
As much as possible, always try to Paint on top of a JPanel or a
JComponent, instead of Painting right on top of your JFrame, by
overriding the paintComponent(Graphics g) method of the said
JComponent/JPanel
Never call setVisible(true) on the JFrame until and unless it's
size has been established. So in general terms, this has to be the
last call, once you are done adding components to the JFrame and
the size of the JFrame has been realized by the LayoutManager.
Inside your actionPerformed(...), instead of writing all if
statement blocks, you should adhere to the if-else if statement
blocks. The benefit of this, over the former is that, at any given
time, only one event will be fired, hence once the said condition is
satisfied, you don't want your code to keep checking other
conditions, which in general is really not a good programming
practice, IMHO.
MOST IMPORTANT THING : Never make calls like pack()/setVisible(...) from within the main method, such calls belong
to the Event Dispatch Thread, and must be done on the same. Please
read Concurrency in Swing for more detail.
Have a look at the example program, for better understanding.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ComponentExample
{
private CustomPanel drawingBoard;
private JPanel contentPane;
private JButton yesButton;
private JButton noButton;
private JButton maybeButton;
private JComboBox cbox;
private ActionListener buttonAction = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
JButton button = (JButton) ae.getSource();
if (cbox.isShowing())
contentPane.remove(cbox);
if (button == yesButton)
{
drawingBoard.setText("You Pressed YES.");
contentPane.add(cbox, BorderLayout.PAGE_END);
}
else if (button == noButton)
drawingBoard.setText("You Pressed NO.");
else if (button == maybeButton)
drawingBoard.setText("You Pressed MAYBE.");
/*
* revalidate()/repaint() is needed
* when the JComponent is added or
* removed from the already
* visible Container.
*/
contentPane.revalidate();
contentPane.repaint();
}
};
public ComponentExample()
{
cbox = new JComboBox(
new String[]{"I GOT IT"
, "I STILL HAD DOUBT"});
}
private void displayGUI()
{
JFrame frame = new JFrame("Component Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(5, 5));
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(true);
buttonPanel.setBackground(Color.WHITE);
yesButton = new JButton("YES");
yesButton.addActionListener(buttonAction);
noButton = new JButton("NO");
noButton.addActionListener(buttonAction);
maybeButton = new JButton("MAY BE");
maybeButton.addActionListener(buttonAction);
buttonPanel.add(yesButton);
buttonPanel.add(noButton);
buttonPanel.add(maybeButton);
contentPane.add(buttonPanel, BorderLayout.PAGE_START);
drawingBoard = new CustomPanel();
contentPane.add(drawingBoard, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ComponentExample().displayGUI();
}
});
}
}
class CustomPanel extends JPanel
{
private String msg;
public CustomPanel()
{
msg = "";
setOpaque(true);
setBackground(Color.WHITE);
}
public void setText(String msg)
{
this.msg = msg;
repaint();
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(300, 300));
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(msg, getWidth() / 3, getHeight() / 3);
}
}
I don't know if I have understood the question well but... couldn't you create those elements and call their setVisible(boolean) methods to make them not visible at first, and them make them visible when user pushes buttons?

Categories