Switch case

Hi

I only give one condition switch case...

but it is showing error how can i solve this in a manner...

i want to add two actions how is it possible...

Would anybody please help me ...

import javax.swing.*;

import javax.swing.JButton;

import java.awt.*;

import java.awt.Font;

import java.awt.event.*;

publicclass bold

{

static JTextArea textarea;

static JFrame frame;

static JPanel panel;

static JButton bold;

//static JButton boldcancel;

publicstaticvoid main(String args[])

{

frame =new JFrame("Textarea bold");

textarea =new JTextArea(15,35);

bold =new JButton("Bold");

//boldcancel = new JButton("Cancel Bold");

panel =new JPanel();

panel.add(textarea);

panel.add(bold);

//panel.add(boldcancel);

boldlistener blisten =new boldlistener();

bold.addActionListener(blisten);

//boldcancellistener bclisten = new boldcancellistener();

//bold.addActionListener(bclisten);

frame.getContentPane().add(panel);

frame.setDefaultCloseOperation(3);

frame.setSize(500,300);

frame.setVisible(true);

}

staticclass boldlistenerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent evt)

{

Object obj = evt.getSource();

switch(obj == bold)//switch

{

case obj :

Font font=new Font("Courier",Font.BOLD,14);

textarea.setFont(font);

break;

}

}

}

/*static class boldcancellistener implements ActionListener

{

public void actionPerformed(ActionEvent evtplain)

{

Object obj1 = evtplain.getSource();

if(obj1 == bold)

{

Font font=new Font("Courier",Font.PLAIN,14); //Font.BOLD,ITALIC

textarea.setFont(font);

}

}

}*/

}

[3408 byte] By [Reona] at [2007-11-27 1:53:32]
# 1
Here is the tutorial on how to use switch case http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html
rym82a at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 2
i read that above link before but it just relating to cui not gui..My topic relates to gui....
Reona at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 3

1) The tutorial works on all cases (console, applet, swing, J2ME, J2EE).

2) Your switch case structure is wrong, you won't put a condition statement in switch block.

3) The First paragraph already mentioned the restriction of using switch cases.

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive types: Character, Byte, Short, and Integer (discussed in Simple Data Objects ).

Make sure you really read the tutorial, and digest it.

rym82a at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 4
would you please give me a small idea in that code to step forward...
Reona at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 5

This:

switch(obj == bold)

What is the value between the parentheses? What type is it?

Do you know what == does?

Have you ever seen an example in your textbook or any tutorial that has something like that between the parentheses after switch?

paulcwa at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 6

'==' is for comparison..

obj == bold is not possible outside switch thats y i think so...

Object obj = evt.getSource();

obj == bold; //this is not possiblt thats why i put obj == bold insiode switch

switch(bold)

{

case obj :

Font font=new Font("Courier",Font.BOLD,14);

textarea.setFont(font);

break;

Reona at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 7

> i read that above link before but it just relating to cui not gui..

> My topic relates to gui....

The code you posted shows an error - as you pointed out in your original post - because the switch statement is not being used correctly. The link given explains how the switch statement should be used. The fact that you are writing a GUI makes no difference.

In this case using a switch statement seems pointless. In your other thread (http://forum.java.sun.com/thread.jspa?threadID=5163388) you seemed to be suggesting that you wanted a single button that would toggle the boldness of the text area: ie make it bold if it was plain, and make it plain again if it was bold.

In that thread I explained (reply 4) that the appropriate thing to do was to write a single event listener responding to a single event (the button click). And that this event handler should check what the current font is and change it appropriately. It still seems to me that this is the case, and that an if statement rather than a switch statement is called for. If you do not, in fact, wish to toggle the font of the text area, please say. If your insistance that there should be two different events is for some reason, what is that? In any case you only have one button: as written there will only be one event.

The easiest way to check the font is to create a couple of fonts right at the start and have a variable that represents the currently active font. (All three variables would be more statics I suppose...)

pbrockway2a at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 8
Inside the action listener you would check to see if the component was already bold. If it was you would make it plain, if it wasn't you would make it bold.hows is this possible inside actionlistener
Reona at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 9

> hows is this possible inside actionlistener

Like this:import java.awt.BorderLayout;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextArea;

import javax.swing.SwingUtilities;

public class ToggleBoldEg extends JFrame {

// The fonts we will use in any frame's text area

private final static Font PLAIN_FONT = new Font("Courier", Font.PLAIN, 14);

private final static Font BOLD_FONT = new Font("Courier", Font.BOLD, 14);

// The font being - this is updated whenever

// a new font is set

private Font currentFont;

// The text area - it will have its font changed

// by the setBold() method

private JTextArea textarea;

public ToggleBoldEg(String title) {

super(title);

textarea = new JTextArea("This is some text in a JTextArea");

add(textarea);

setBold(true);

// Create a single button

JButton button = new JButton("Click me!");

// Add a single event listener that responds

// to a single event (the button click)

button.addActionListener(boldToggler);

add(button, BorderLayout.SOUTH);

}

private ActionListener boldToggler = new ActionListener() {

public void actionPerformed(ActionEvent event) {

// currentFont==PLAIN_FONT is a boolean value that will

// be true if the current font is plain. Otherwise it

// will be false. The setBold() method expects an argument

// of true if it is to change the font to bold.

setBold(currentFont == PLAIN_FONT);

}

};

// The actual font setting is done here.

private void setBold(boolean b) {

// The ? operator means that if b is "true" BOLD_FONT will be

// used. If b is "false" PLAIN_FONT will be used

// First we set the currentFont (the actionPerformed method will

// need to know it next time there is a click)...

currentFont = b ? BOLD_FONT : PLAIN_FONT;

// Then we change the font of the text area

textarea.setFont(currentFont);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {createAndShowGui();}

});

}

private static void createAndShowGui() {

ToggleBoldEg test = new ToggleBoldEg("Toggle bold example");

test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

test.pack();

test.setVisible(true);

}

}

pbrockway2a at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 10
Hi pbrockway2Really really thank you for wat you did ...I tried it so far as i can, searched and posted a lot .. Atlast you helps me..Thanks again.....
Reona at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...
# 11
You're welcome.
pbrockway2a at 2007-7-12 1:23:49 > top of Java-index,Java Essentials,Java Programming...