can't see the background color of a frame , why?


today in college i was trying to set the background color of a jframe but it only shows the color when i resize the frame and not when i just select the radio button
but if i set the background color of a panel instead frame it works fine
can you explain me why we cant see the background color of a frame

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javachallenge; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * * @author Admin */ public class JavaChallenge extends JFrame implements ActionListener{ /** * @param args the command line arguments */ JRadioButton green,red; JPanel panel; void foo() { panel = new JPanel(); ButtonGroup btngroup = new ButtonGroup(); red = new JRadioButton("Red"); btngroup.add(red); green = new JRadioButton("Green"); btngroup.add(green); panel.add(red); panel.add(green); add(panel,BorderLayout.SOUTH); pack(); red.addActionListener(this); green.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { // TODO code application logic here new JavaChallenge().foo(); } @Override public void actionPerformed(ActionEvent e) { if(red.isSelected()) { setBackground(Color.RED); setExtendedState(JFrame.MAXIMIZED_BOTH); // panel.updateUI(); }else { setBackground(Color.GREEN); setExtendedState(JFrame.MAXIMIZED_BOTH); //panel.updateUI(); } } }
Posted On: Saturday 29th of September 2012 05:26:49 AM Total Views:  187
View Complete with Replies

RELATED TOPICS OF Swing / AWT / SWT PROGRAMMING LANGUAGE




Change icon color dinamically

! I'm trying to create a JFrame with 3 circles inside it and make it work like a traffic light. I've created a separate class for lights, but I can't make them change colors dinamically, because once the icon has been drawn, I can't change it's color anymore Should I give up this idea or there is a way to get it done package trafficlight; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import javax.swing.Icon; public class Light implements Icon { private int size; private Color colorOff = Color.BLACK; private Color currentColor = Color.BLACK; private Color colorOn; private int delay; public Light(Color color, int size, int delay) { this.colorOn = color; this.size = size; this.delay = delay; } public void switchLight(boolean turnOn) { //here I want to make my Light to change its color } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(currentColor); g.drawOval(x, y, size, size); g.fillOval(x, y, size, size); } @Override public int getIconWidth() { return size; } @Override public int getIconHeight() { return size; } public int getDelay() { return delay; } public void setDelay(int delay) { this.delay = delay; } }
VIEWS ON THIS POST

133

Posted on:

Thursday 20th September 2012
View Replies!

color help

Folks, Below is a code I have been writing on my own (well a modification from what online version read). The error has been highlighted. import javax.swing.JOptionPane; public class playwithrange { public static void main(String[] args) { String color ; color= JOptionPane.showInputDialog("Choose between green, black and white"); switch (color){ case green: System.out.println("You are a green person"); break; default: System.out.println("Eh whatever"); } } }
VIEWS ON THIS POST

129

Posted on:

Thursday 20th September 2012
View Replies!

how to get colored line to differentiate text field array in swing

I have developed one frame on which I used GridBagLayout to arrange textfields of 12X12. i.e., total 144 textfields on frame. Now I want to differentiate these text fields with colored line after each 3 columns and three rows as shown in the following diagram. I shouldn't change the names of that text fields, because I interrelated those text fields using their names as jt[0][0], jt[0][1]......jt[6][2]........ and So on.I tried in many ways, but I couldn't find the solution. Please suggest. Below is the some part of my code.
VIEWS ON THIS POST

232

Posted on:

Friday 21st September 2012
View Replies!

Program color scheme controlled by a Colors class: object or enum?

I'm writing a rather large program with lots of classes and potentially lots of JFrames. Could I have one class devoted to establishing the primary selection of colors that will be repeated throughout the program That way, rather than remembering each color definition, I can just code something like Colors.TitleFont to represent the color of the title of each window, and Colors.InputPanelBg to represent the background of any input window. I have not found examples of this on-line, but it seems reasonable. Would a Colors object or an Enum work for this Here is my attempt, but Eclipse says "The constructor GSCOLORS(String) is undefined." import java.awt.Color; public enum GSCOLORS { InputPanelA("0xa29292"), InputPanelB("0x92a292"); private Color col; GSCOLORS(Color colorId) { col = colorId; } public Color getCol() { return col; } }
VIEWS ON THIS POST

134

Posted on:

Friday 21st September 2012
View Replies!

having problems with setting the color of a buffered Imaghe

I created a buffered image in my programme and I want it to have a green (orany color other than black)background however i have been unable to get it to change from black to any other other color.hers the code i have been working with .
VIEWS ON THIS POST

332

Posted on:

Friday 21st September 2012
View Replies!

i try to get the VK_TAB in jtextfield but i cant,here is the code what i doing wrong? the other keys

this is the code... private void jTextField18KeyReleased(java.awt.event.KeyEvent evt) { // TODO add your handling code here: if(evt.getKeyCode()==KeyEvent.VK_CONTROL){ jTextField3.setFocusable(true); jTextField3.requestFocusInWindow(); } } buy the way i generate the code with netbeans...
VIEWS ON THIS POST

246

Posted on:

Saturday 22nd September 2012
View Replies!

Change Background color on button Click

Hi I want to change Background color of Jpanel and its font on button click. Can anyone tell me what i am doing wrong Here is the code: import java.applet.*; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.event.*; import javax.swing.*; public class test3 extends Applet { JPanel c; JScrollPane s; Button connect; Panel controls; Color back,fore; public void init() { back = Color.black; fore = Color.white; setBackground(Color.darkGray); setLayout(new BorderLayout()); s = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); //s.setSize(100, 100); add("Center",s); c = new myCanvas11(this); s.setOpaque(false); s.setViewportView(c); //s.add(c); c.setSize(1000, 16000); add("North", controls = new Panel()); controls.setLayout(new FlowLayout()); controls.add(connect = new Button("Change Color")); } public void start() { // s.setScrollPosition(100, 100); } public boolean action(Event e, Object arg) { back = Color.magenta; fore = Color.blue; //setBackground(back); //invalidate(); //repaint(); c.setBackground(back); c.repaint(); //s.getViewport().setBackground(back); // s.getViewport().repaint(); //c.repaint(); c.setFocusable(true); return true; } } class myCanvas11 extends JPanel implements KeyListener { Image buffImage; Graphics offscreen; boolean initDone = false; int chw, chh; // size of a char (in pixels) int chd; // offset of char from baseline int width, height; // size of applet (in pixels) int w, h; // size of applet (in chars) Font fn; Graphics gr; int nh, nw; test3 owner; static int counter = 0; myCanvas11(test3 t) { super(); owner = t; nh = 16000; nw = 1000; this.setOpaque(true); this.setFocusable(true); addKeyListener(this); } public void reshape(int nx, int ny, int nw1, int nh1) { if (nw1 != width || nh1 != height) { width = nw; height = nh; gr = getGraphics(); fn = new Font("Courier", Font.PLAIN, 11); if (fn != null) gr.setFont(fn); FontMetrics fnm = gr.getFontMetrics(); chw = fnm.getMaxAdvance(); chh = fnm.getHeight(); chd = fnm.getDescent(); // kludge for Windows NT and others which have too big widths if (chw + 1 >= chh) { chw = (chw + 1) / 2; } // work out size of drawing area h = nh / chh; w = nw / chw; buffImage = this.createImage(nw, nh); offscreen = buffImage.getGraphics(); //offscreen.setColor(Color.black); //offscreen.fillRect(0, 0, nw, nh); offscreen.setColor(Color.blue); offscreen.setFont(fn); if (initDone) offscreen.drawString(" World!", 0, 50); else offscreen.drawString("khushbu", 2, 50); initDone = true; offscreen.drawImage(buffImage, 0, 0, this); } super.reshape(nx, ny, nw, nh); } public void paint(Graphics g) { // if (!initDone) // initpaint(g); // else g.drawImage(buffImage, 0, 0, this); //g.drawImage(buffImage, 0, 0, owner.back, this); } public void update(Graphics g) { g.drawImage(buffImage, 0, 0, this); super.update(g); //g.drawImage(buffImage, 0, 0, owner.back, this); } public void initpaint(Graphics g) { try { nh = getHeight(); nw = getWidth(); gr = getGraphics(); fn = new Font("Courier", Font.PLAIN, 11); if (fn != null) gr.setFont(fn); FontMetrics fnm = gr.getFontMetrics(); chw = fnm.getMaxAdvance(); chh = fnm.getHeight(); chd = fnm.getDescent(); // kludge for Windows NT and others which have too big widths if (chw + 1 >= chh) { chw = (chw + 1) / 2; } // work out size of drawing area h = nh / chh; w = nw / chw; buffImage = this.createImage(nw, nh); offscreen = buffImage.getGraphics(); //offscreen.setColor(Color.black); //offscreen.fillRect(0, 0, nw, nh); offscreen.setColor(Color.white); offscreen.setFont(fn); if (initDone) offscreen.drawString(" World!", 0, 50); else offscreen.drawString("khushbu", 2, 50); initDone = true; g.drawImage(buffImage, 0, 0, this); } catch (Exception e) { e.printStackTrace(); } } /** Handle the key typed event from the text field. */ public void keyTyped(KeyEvent e) { } /** Handle the key pressed event from the text field. */ public void keyPressed(KeyEvent e) { String s; offscreen.setColor(owner.fore); offscreen.setFont(fn); for (int i = counter; i < counter + 25; ++i) { s = Integer.toString(i); offscreen.drawString(s, 3, i * chh); offscreen.drawLine(10, i * chh, 160, i * chh); } //owner.s.setScrollPosition(0, counter * 16); counter = counter + 25; repaint(); } /** Handle the key released event from the text field. */ public void keyReleased(KeyEvent e) { } public boolean keyDown(Event e, int k) { String s; offscreen.setColor(owner.fore); offscreen.setFont(fn); for (int i = counter; i < counter + 25; ++i) { s = Integer.toString(i); offscreen.drawString(s, 3, i * chh); offscreen.drawLine(10, i * chh, 160, i * chh); } //owner.s.setScrollPosition(0, counter * 16); counter = counter + 25; repaint(); return true; } }
VIEWS ON THIS POST

184

Posted on:

Saturday 22nd September 2012
View Replies!

Change canvas background colour at button click

I am developing Java applet, which have canvas in it. I want to change background color of canvas at one button click. i have changed it using c.offscreen.setColor(back); c.offscreen.fillRect(0, 0, c.getWidth(), c.getHeight()); c.repaint();
VIEWS ON THIS POST

202

Posted on:

Saturday 22nd September 2012
View Replies!

Trouble with JEditorPane changing colors with styles

I'm using Styles to change the color of my reserved words, but its changing incorrectly, its changing right when I type in the first line but in the next lines only part of the word is changed, what is wrong with my code\t the problem! Uploaded with ImageShack.us this is the code -> public void doFindText(String find) { SimpleAttributeSet bluAttr = new SimpleAttributeSet(); StyleConstants.setForeground(bluAttr, Color.BLUE); SimpleAttributeSet GreenAttr = new SimpleAttributeSet(); StyleConstants.setForeground(GreenAttr, Color.GREEN); int nextPosn = 0; if (!find.equals(findText) ) // *** new find word findPosn = 0; // *** start from top nextPosn = nextIndex( Ed_Prog.getText(), find, findPosn, false); if ( nextPosn >= 0 ) { //Ed_Prog.setSelectionStart( nextPosn-1 ); // position cursor at word start //Ed_Prog.setSelectionEnd( nextPosn + find.length() ); if(find.equalsIgnoreCase("programa")){ Ed_Prog.getStyledDocument().setCharacterAttributes(nextPosn,nextPosn + find.length()+1, bluAttr, false); }else if(find.equalsIgnoreCase("string")){ Ed_Prog.getStyledDocument().setCharacterAttributes(nextPosn-1,nextPosn + find.length()+1, GreenAttr, false); } else if(find.equalsIgnoreCase("inteiro")){ Ed_Prog.getStyledDocument().setCharacterAttributes(nextPosn-1,nextPosn + find.length()+1, GreenAttr, false); } else if(find.equalsIgnoreCase("real")){ Ed_Prog.getStyledDocument().setCharacterAttributes(nextPosn-1,nextPosn + find.length()+1, GreenAttr, false); } findPosn = nextPosn + find.length()+1; // reset for next //search findText = find; // save word & case nextPosn = 0; } else { findPosn = nextPosn; // set to -1 if not found //JOptionPane.showMessageDialog(this, find + " not Found!" ); } }
VIEWS ON THIS POST

206

Posted on:

Saturday 22nd September 2012
View Replies!

It doesnt see my 'if' statements

A stripped down version of my program is in the 2 files copied below. It is a simple calculator like in Windows accessories. The example below only does addition and subtraction however it doesn't work. Here's how it is supposed to work. A number is sent to the textbox using button clicks and when '+' button is clicked the displayed string is converted to double. The displayed string remains displayed until the first digit of the next number is entered then the first number is cleared and the second number begins in the display. When '=' is clicked the second displayed number (actually a string) is converted to double and added to the first double number. The operation should be controlled by the 'opFlag' and 'addFlag' settings. The problem apparently is that the 'if' statements that set the flags are not being executed. I tried NetBeans debugger to watch the values but I don't know how to use the debugger well enough. I'm hoping someone here can tell me what I'm doing wrong so I can move on. Your comments will be appreciated. TIA Bill S. THE GUI FILE: import java.awt.*; import javax.swing.*; public class CalcGUIQ2 extends JFrame { CrunchQ2 crunchNu = new CrunchQ2(this); // set up row 1 JPanel row1 = new JPanel(); JTextField number1 = new JTextField(30); // set up row 2 JPanel row2 = new JPanel(); JButton sev = new JButton("7"); JButton ate = new JButton("8"); JButton nin = new JButton("9"); JButton fou = new JButton("4"); JButton fiv = new JButton("5"); JButton six = new JButton("6"); JButton one = new JButton("1"); JButton two = new JButton("2"); JButton tre = new JButton("3"); JButton add = new JButton("+"); JButton sub = new JButton("--"); JButton zro = new JButton("0"); JButton dot = new JButton("."); JButton equ = new JButton("="); public CalcGUIQ2() { super(); setTitle("Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout layout = new GridLayout(2, 1, 10, 10); setLayout(layout); //add listeners dot.addActionListener(crunchNu); zro.addActionListener(crunchNu); one.addActionListener(crunchNu); two.addActionListener(crunchNu); tre.addActionListener(crunchNu); fou.addActionListener(crunchNu); fiv.addActionListener(crunchNu); six.addActionListener(crunchNu); sev.addActionListener(crunchNu); ate.addActionListener(crunchNu); nin.addActionListener(crunchNu); equ.addActionListener(crunchNu); add.addActionListener(crunchNu); sub.addActionListener(crunchNu); FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10); row1.add(number1); row1.setLayout(layout1); add(row1); GridLayout layout2 = new GridLayout(5, 3, 10, 10); row2.setLayout(layout2); row2.add (sev); row2.add (ate); row2.add (nin); row2.add (fou); row2.add (fiv); row2.add (six); row2.add (one); row2.add (two); row2.add (tre); row2.add (zro); row2.add (dot); row2.add (equ); row2.add (add); row2.add (sub); add(row2); pack(); setVisible(true); } public static void main(String[] arguments) { CalcGUIQ2 frame = new CalcGUIQ2(); } }
VIEWS ON THIS POST

100

Posted on:

Saturday 22nd September 2012
View Replies!

Trying to get a square change color on button click

EDIT: Obviously a beginners question! Probably in the wrong forum, please feel free to move mods! Apologies! I thought I'd help myself understanding the GUI and ActionListener proces a bit better by sussing out how to do something simple like putting a button and a rectangle on a screen and then having it change color from blue to red on buttonclick. Appears I've ended up in a swamp and everytime I move I get sucked farther in. I soon found out I have no clue how to easily change the color of something when clicking a button so I thought I'd fake it first by simply running a different color panel on mouseclick. And that failed miserably too! Trying to use the boolean swoosh in two different classes is not the way to go. So a little help, hint or push in the right direction would be greatly appreciated! import javax.swing.*; import java.awt.*; import java.awt.event.*; public class KnopOefen implements ActionListener { JFrame frame; public static void main(String[] args) { boolean swoosh = true; KnopOefen knop = new KnopOefen(); knop.go(); } public void go() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Click to make the square red!"); button.addActionListener(this); Rectangle recty = new Rectangle(); frame.getContentPane().add(BorderLayout.SOUTH, button); frame.getContentPane().add(BorderLayout.CENTER, recty); frame.setSize(300,300); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { boolean swoosh = false; KnopOefen knip = new KnopOefen(); knip.go(); } }
VIEWS ON THIS POST

199

Posted on:

Wednesday 26th September 2012
View Replies!

adding background image to JSPlitPane

How can i set background image to the lef side of the JSplitPane.
VIEWS ON THIS POST

145

Posted on:

Wednesday 26th September 2012
View Replies!

background and foreground color is not changing in full screen window

why my background and foreground color is not changing\t import java.awt.*; import javax.swing.*; public class mainScreen extends JFrame{ public static void main(String args[]) { DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); mainScreen ms = new mainScreen(); ms.run(dm); } public void run(DisplayMode dm) { setBackground(Color.PINK); setForeground(Color.GRAY); setFont(new Font("Arial", Font.PLAIN, 24)); Screen s = new Screen(); try{ s.setFullScreen(dm, this); try{ Thread.sleep(5000); }catch(Exception e){} }finally{ s.restoreScreen(); } } public void paint(Graphics g) { g.drawString("This is FullScreen", 200, 200); } }
VIEWS ON THIS POST

118

Posted on:

Wednesday 26th September 2012
View Replies!

JCheckboxMenuItem won't change selection color

guy, Am I doing something wrong or what I want to change the color for the selection background of the JCheckboxMenuItem and it doesn't change at all. This is what I have, what am i doing wrong UIManager.put( "CheckBoxMenuItem.acceleratorForeground", Crawler.color_black); UIManager.put( "CheckBoxMenuItem.acceleratorSelectionForeground", Crawler.color_black); UIManager.put( "CheckBoxMenuItem.background", Crawler.color_white); UIManager.put( "CheckBoxMenuItem.border",BorderFactory.createLineBorder(Crawler.color_white, 1)); UIManager.put( "CheckBoxMenuItem.disabledBackground", Crawler.color_white); UIManager.put( "CheckBoxMenuItem.disabledForeground", Crawler.color_white); UIManager.put( "CheckBoxMenuItem.foreground", Crawler.color_black); UIManager.put( "CheckBoxMenuItem.selectionBackground", Crawler.color_blue); UIManager.put( "CheckBoxMenuItem.selectionForeground", Crawler.color_black);
VIEWS ON THIS POST

103

Posted on:

Wednesday 26th September 2012
View Replies!

Set the background Color to JTable of Chekckbox

Hi i have a created the JTable of 3 column, 1st Column is used as Check-box, 2nd and 3rd column used as String i have created the checkbox as follows public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); }
VIEWS ON THIS POST

65

Posted on:

Thursday 27th September 2012
View Replies!

Substitute one color with another on an image

Hi! I need to substitute one color with white on an image. I've read about IndexColorMmodel, ColorModel and RGBImageFilter, but I don't understand how they work. The color I want to substitute is: R: 113 G: 75 B: 96 And of course, white is 255,255,255. I've read several examples, but can't grasp all the concepts. I'm new working with images. How can I do it
VIEWS ON THIS POST

90

Posted on:

Thursday 27th September 2012
View Replies!

creating a non regular GUI with a .jpg background

I'm making an application for a group use and I want to make a GUI that uses a pdf as a background with buttons arranged in curtain positions as according to the .pdf. I know what the .pdf will be, but so far in my building of this application I haven't come across a means to build a GUI with using a different background and being able to specify where buttons are. So the .pdf is a picture of a parking lot and I want to arrange buttons in the parking spots, so if possible some with text going vertically (rotated for reading), and text at an angle of say 15 degrees. I am wondering if anyone knows of a way I can do this. I think I could use GridBagLayout but it would be extremely difficult to line up all the spots and make it look nice.
VIEWS ON THIS POST

73

Posted on:

Thursday 27th September 2012
View Replies!

Cannot see two Jtables until resize

hey , I am running into a problem and i need help, i basically have a jframe with two JPanels which would hold the JScrollpanes of jtables, there are also two JTextFields who would act as a search bar for populating the the Jtable, the programming of data is done correctly but i am running into a problem, the second Jtable does not show until and unless i resize the jframe window, i cant get it to work. I have tried validate(), update(), setvisible() but it wont work, Please i need your help. I have add screenshots for reference and the code i given below:
VIEWS ON THIS POST

129

Posted on:

Friday 28th September 2012
View Replies!

main() method is static but swing frames are objects

what does the following staement mean: The main() method is static but swing frames are objects so you have to figure how to get your program out of static context. This can be done by creating the application as a class that extends JFrame. the main method then calls the class constructor in effect creating an instance of itself.
VIEWS ON THIS POST

198

Posted on:

Friday 28th September 2012
View Replies!