Create border background colour

Hi i need to change tha background colour of a border which goes around a JTextArea.

http://www.esus.com/docs/GetQuestionPage.jsp?uid=1326

on this page is the class i am using (the second example called LineNumberedBorder).

is there anything i can add into that class to change the border background colour?

or alternatively should i do it when i actually assign that border to my text area?

code i use to set the border:

Border lineNumber = (new LineNumberedBorder(LineNumberedBorder.LEFT_SIDE,

LineNumberedBorder.LEFT_JUSTIFY)) ;

Border lineBorder =new LineBorder(java.awt.Color.gray,

1,true);

textArea.setBorder(javax.swing.BorderFactory.createCompoundBorder(

lineNumber, lineBorder));

please help, thanks

[892 byte] By [boblettoj99a] at [2007-11-26 19:27:41]
# 1

Hope this helps. Plz refrence the code.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LocalColor {

public static void main(String[] args) {

final JFrame f = new JFrame("LocalColor v1.0");

f.addWindowListener(new WindowAdapter( ) {

public void windowClosing(WindowEvent e) { System.exit(0); }

});

f.setSize(200, 200);

f.setLocation(200, 200);

final Container content = f.getContentPane( );

content.setLayout(new GridBagLayout( ));

JButton button = new JButton("Change color...");

content.add(button);

button.addActionListener(new ActionListener( ) {

public void actionPerformed(ActionEvent e) {

Color c = JColorChooser.showDialog(f,

"Choose a color", content.getBackground( ));

if (c != null) content.setBackground(c);

}

});

f.setVisible(true);

}

}

Vausta at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
thanks but it doesnt really help, is there any graphics method i dont know about which sets the background colour?i have tried g.setBackground(Color.red) but that doesnt do it :(
boblettoj99a at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 3

You would do your custom painting in the paintBorder(...) method.

I don't like the solution you are using anyway since the line number will scroll off the page when the line length exceeds the width of the text area. I prefer the line numbers to be fixed. Anyway there are so many solutions posted in the forums if you would just search. Here are a couple more suggestions:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=621974

camickra at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 4

i finally figured out how to fix the numbers in position, that was annoying me for some time, but still no background colour.

i have gone through the paintBorder method about a hundred times trying to find anything that would change it but no luck :(

is there a way perhaps, to change the background colour of a string?

then when the numbers are painted it will have that background colour.

just a thought, feel free to laugh at me :)

boblettoj99a at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 5
oo i used one of the classes from your link and it works like a dream, thanks for the help : )
boblettoj99a at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 6

Made the following changes to LineNumberedBorderII.java

public class LineNumberedBorderII extends AbstractBorder {

// abillconsl add these two global variables.

private Color backgroundColor;

private intthickness;

// ...

public Insets getBorderInsets(Component c, Insets insets) {

// if c is not a JTextArea...nothing is done...

if (c instanceof JTextArea) {

//int width = lineNumberWidth((JTextArea) c);

// abillconsl modified to use Global thickness instead of local width.

thickness = lineNumberWidth((JTextArea) c);

if (location == LEFT_SIDE) {

insets.left = thickness;

} else {

insets.right = thickness;

// ...

// NOTE: abillconsl Added method to change border background color

public void setBackground(Color color) throws IllegalArgumentException {

if (color == null || !(color instanceof Color))

throw new IllegalArgumentException("class LineNumberedBorder\t"

+"method setBackground\t"

+"Invalid Color Object passed in\n");

backgroundColor = color;

}

// ...

public void paintBorder(Component c, Graphics g, int x, int y,

int width, int height) {

java.awt.Rectangle clip = g.getClipBounds();

// NOTE: abillconsl mode to paint the background

if (backgroundColor == null)

backgroundColor = c.getBackground();

g.setColor(backgroundColor);

System.out.println("thickness: "+thickness);

g.fillRect(0, 0, thickness, height);

//for (int i = 0; i < thickness; i++)

//g.drawRect(x+i, y+i, thickness-i-i-1, height-i-i-1);

// End abillconsl mod

... Ran as follows:

/** class BorderColor*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.event.*;

public class BorderColor {

private JFrame frame;

private JTextArea textArea;

private Container container;

private LineNumberedBorderII lineNumber;

private LineBorder lineBorder;

public BorderColor() {

frame = new JFrame("BorderColor");

container = frame.getContentPane();

textArea = new JTextArea(20,40);

lineNumber = (new LineNumberedBorderII(LineNumberedBorder.LEFT_SIDE,

LineNumberedBorder.LEFT_JUSTIFY));

lineNumber.setBackground(Color.YELLOW);

lineBorder = new LineBorder(java.awt.Color.gray, 1, false);

textArea.setBorder(javax.swing.BorderFactory.createCompoundBorder(

lineNumber, lineBorder));

//textArea.setBackground(Color.WHITE);

container.add(textArea);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

public static void main ( String[] argv ) throws Exception {

JFrame.setDefaultLookAndFeelDecorated( true );

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

new BorderColor();

}

});

}

}

abillconsla at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 7
me? if it was me no problem. Anytime.
Vausta at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 8
See you've already got it fixed ... too late ... and I also agree that this class is not perhaps the best way to do this ... but if you have a look at the code you should pick up a few tips. HTH~Bill
abillconsla at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 9
abillconsl could you help me on my problem. It's still located in Swing. Getting correct and in correct to show on my GUI. Thanks
Vausta at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 10
> abillconsl could you help me on my problem. It's> still located in Swing. Getting correct and in> correct to show on my GUI. ThanksI looks like you have your answer ... what do you need?
abillconsla at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 11
http://forum.java.sun.com/thread.jspa?threadID=5140485&tstart=0or look for the Getting correct and incorrect to show on your GUI. It's still part of this forum.
Vausta at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 12
Ya, what's wrong with what tjacobs01 gave you? I don't have time to read through all the posts and figure it out. Tell me what you need and then one of us might be able to help
abillconsla at 2007-7-9 21:54:22 > top of Java-index,Desktop,Core GUI APIs...
# 13

1. First i want to create a GUI that it a quiz.

2. Create Questions by using Combo Box

3. Next it creates a text field and allow me to input my answer

4. following that, after the input is inputted, i click a button that says check answer and it will return if it is correct or false.

The problem is that code is so messed up that i don't know where to go. Thats why can't really brief wat i need help in. So, could you write a quick code that can summarize the whole thing. You can use my code and fix it up. Thanks very so much!

package anaquiz;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class AnAQuiz extends JPanel {

private JComboBox choice;

private JLabel label;

private JTextField text;

Button CheckAnswer;

Button TryAgain;

private String Answer1 = "Wilson";

private String Answer2 = "Ace";

private String Answer3 = "Yes";

private String input = " ";

boolean Answer = false;

JLabel testresult;

public AnAQuiz() {

choice = new JComboBox(); //Step 1

label = new JLabel(); //Step2

label.setBackground(Color.blue);

choice.addItem("Tennis Question #1"); //step 3

choice.addItem("Tennis Question #2"); // Step 3

choice.addItem("Tennis Question #3"); //step 3

text = new JTextField(42); //step 4

Listener listen = new Listener();

choice.addActionListener(listen);

add(choice);

add(label);

add(text);

}

private class Listener implements ActionListener {

public void actionPerformed(ActionEvent event ) {

int c = choice.getSelectedIndex();

switch (c) {

case 0:

label.setText("Whats the famous tennis brand that begins with 'W'?");

if (input.equals(Answer1))testresult = new JLabel("Correct!");

else testresult = new JLabel("Incorrect!");

break;

case 1:

label.setText("What do you call when someone misses a serve?");

break;

case 2:

label.setText("Should you shake hands after a match?");

break;

}

}

//--

public static void main (String [] args)

{

JFrame frame = new JFrame("Quiz");

JPanel p = new JPanel();

frame.getContentPane().add(new AnAQuiz());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JTextField field = new JTextField(8);

p.add(field);

JButton b = new JButton("CheckAnswer");

p.add(b);

final JLabel l = new JLabel(" ");

p.add(l);

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

if (field.getText().equals("Wilson")) {

l.setText("Correct");

} else l.setText("Incorrect");

}

});

frame.setBounds(100, 100, 100, 100);

frame.setVisible(true);

frame.pack();

frame.show();

}

}

}

Thanks again

Message was edited by:

Vaust

Vausta at 2007-7-9 21:54:23 > top of Java-index,Desktop,Core GUI APIs...