Display JColorChooser as frame and pass color value to buttons - jcolorchooser

Hello All and Thanks in advance.
I need JColorChooser to appear as the top panel in my window. I used a button in its place for formatting purposes.(See picture) I also need to pass chosen color value from colorchooser to chosen button in array. I have most of the structure complete but I am still pretty new to Java.
** I would like to also note that I do not need 'ok' or 'cancel' buttons in the colorchooser dialog because once you select a color you should be able to press as many buttons as you would like to change their colors to selected color.
View an image of my program
package main;
public class Main {
public static JPanel ToolPanel = new JPanel();
public static JPanel GridPanel = new JPanel();// Define the Panel that Holds 256 Button Array
public static JPanel FullPanel = new JPanel();
public static JButton button[] = new JButton[256];// Define button array for 256 buttons
private static JColorChooser colorChooser;
public static JButton ColorChooserButton = new JButton("This is where the JColorChooser(colorChooser) should go. ");
public static JFrame MaineWindow = new JFrame("Kola Color Crapper");
final static String ToolPanelString = "Tool Panel";
final static String GridPanelString = "Grid Panel";
public static ActionListener ButtonArrayActionListener = new ActionListener() {
public void actionPerformed(ActionEvent aef) {
if (aef.getSource() instanceof JButton) {
((JButton) aef.getSource()).setBackground(Color.blue);}}};
public static ActionListener ColorChooserButtonActionListener = new ActionListener() {
public void actionPerformed(ActionEvent aeef) {
String st="Welcome";
JOptionPane.showMessageDialog(null,st);
}};
public static void main( String[] args ){//Main (Run)
// setup the gui with buttons
CreateFullPanel();
CreateMaineWindow();
}
//CreateToolPanel() is just used to test formatting
public static void CreateToolPanel(){
ToolPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
ToolPanel.setLayout(new GridLayout(1,0));
ColorChooserButton.addActionListener(ColorChooserButtonActionListener);
ToolPanel.add(ColorChooserButton);
ColorChooserButton.setToolTipText("This is a tool tip.");
}
public static void CreateFullPanel(){
CreateToolPanel();
CreateGridPanel();
//I want to insert CreateColorChooserPanel() where CreateToolPanel() is
// CreateColorChooserPanel();
}
public static void CreateGridPanel() {
GridPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
GridPanel.setLayout(new GridLayout(16, 16, 0, 0));
CreateButtonArray();// Create array of buttons
}
public static void CreateButtonArray(){
for (int i = 0; i < button.length; i++) {
button[i] = new JButton(Integer.toString(i + 1));
button[i].addActionListener(ButtonArrayActionListener);
GridPanel.add(button[i]);
GridPanel.setToolTipText("ToolTIp does not work");
}
}
public static void CreateMaineWindow(){
MaineWindow.setLayout(new GridLayout(2,1));//Main Window is resizable
MaineWindow.add(ToolPanel, ToolPanelString);
MaineWindow.add(GridPanel, ToolPanelString);
MaineWindow.setBounds(300,300,600,400);
MaineWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MaineWindow.pack();// pack all the contents of Main Window
MaineWindow.setVisible(true);//Show Main Window
}
public static void CreateColorChooserPanel(){
}
}

Related

How to change the window title bar color

I want to know how to develop the idea plug-in, how to use the window, or how to define the child window of the idea
public class EncryptionAndDecryption extends AnAction {
#Override
public void actionPerformed(#NotNull AnActionEvent e) {
System.out.println("加密解密");
new test();
}
}
public class test {
private JButton button1;
private JPanel panel1;
public test() {
JFrame jFrame = new JFrame("test");
jFrame.setContentPane(panel1);
jFrame.setSize(500, 400);
//jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jFrame.setLocationRelativeTo(null);
// jFrame.setUndecorated(true);
jFrame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
jFrame.setVisible(true);
}
}
How to change title block color

How to transfer value from website to windows forms online?

I am creating an application on Windows Form and collided with a trouble.
I am getting each second a value from a website by Selenium Webdriver, using XPath and recording it to my local string. I am getting value of a timer. It doesn't matter, it's comfortable to me to get it like a string.
So. I have a loop, which reading data from website and this string I put to TextBox each second too.
But I am not able to see an output of the textBox until my loop is run. I don't want to stop it.
I want to see values from a website online in my application.
Do you have ideas how to refresh the form each second?
I can provide my code if you need.
Thank you in advance, my guru!
public static ChromeDriver GetDriver() {
return new ChromeDriver();
}
public static void CheckGame(string URLevent) {
while (GameIsRun) {
GetInformation(GetDriver(), URLevent);
GameIsRun = false;
}
}
static void GetInformation(ChromeDriver driver, string URLevent) {
driver.Navigate().GoToUrl(URLevent);
do {
TimeOfGame = driver.FindElement(By.XPath("html/body/div/div/div//div/" +
"div[#class='headroom-wrapper']/div//div[3]/div")).GetAttribute("innerHTML");
Print print = new Print()
print.Info(TimeOfGame);
System.Threading.Thread.Sleep(1000);
}
while (TimeOfGame != "90:00");
}
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
Form form = new Form();
}
private static Form1 _form = null;
public static Form1 Form {
get {
if (_form == null) {
_form = new Form1();
}
return _form;
}
}
private void button1_Click(object sender, EventArgs e) {
string eventId = tbEventID.Text; //this works vice versa. It's not my issue.
string URLevent = "url..." + tbEventID.Text + "/";
Program.CheckGame(URLevent);
}
}
public class Print {
public void Info(string TimeOfGame) {
Form1.Form.tbTeam1.Text = TimeOfGame;
//my issue is here. I want to check result at TextBox each second!
}
}
This statement in your code
TimeOfGame = driver.FindElement(By.XPath("html/body/div/div/div//div/div[#class='headroom-wrapper']/div//div[3]/div")).GetAttribute("innerHTML");
is very brittle. You should use getText() method for extracting the inner text of HTML methods. Also, in order to locate webElement precisely, it is preferred to use focused relative Xpath over absolute Xpath.

Android App: Replace default button background with custom background in a Dialog Fragment

What I'm trying to do is change the default backgrounds of a custom DialogFragment that I have written. Normally, I would do this by changing the XML layout file, however, for a DialogFragment these buttons don't exist in the layout file.
In essence, I'm trying to get access to the setPositiveButton, setNegativeButton and setNeutral buttons in order to modify them. Alternatively, I would next try to do this by getting them by id, however, since they aren't defined in a layout file, I do not have a corresponding id for them. I've found plenty examples of how to modify the rest of the layout, but I can't find anywhere that positive/neutral/negative buttons are modifiable.
Ideally, I could do this in the following block of code:
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
...
}
})
Thanks in advance.
Here is the code ... The button instance is valid only after the dialog created. Hope this helps you.
public static class CustomDialog extends DialogFragment
{
public static CustomDialog newInstance()
{
return new CustomDialog();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
super.onCreateDialog(savedInstanceState);
Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog dialog = builder.create();
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL",new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return dialog;
}
#Override
public void onStart()
{
super.onStart();
Button pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
Button nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
pButton.setBackgroundColor(getResources().getColor(R.color.Blue));
nButton.setBackgroundColor(getResources().getColor(R.color.Green));
}
}

Calling a ViewPart(SWT) from void main

I am new to eclipse plugin and SWT. I am designing a view part with some functionalities.
But the only problem is that I have to Launch it as a eclipse plugin to check.
Is there any way that we can call this from that main(String[] args) method?
I am posting my sample viewpart code
public class View extends ViewPart {
public static Display display=new Display();
public static final String ID = "Test.View"; //$NON-NLS-1$
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
public View() {
}
/**
* Create contents of the view part.
* #param parent
*/
#Override
public void createPartControl(Composite parent) {
Composite container = toolkit.createComposite(parent, SWT.NONE);
toolkit.paintBordersFor(container);
{
Label lblNewLabel = new Label(container, SWT.NONE);
lblNewLabel.setBounds(25, 46, 49, 13);
toolkit.adapt(lblNewLabel, true, true);
lblNewLabel.setText("New Label");
}
Spinner spinner = new Spinner(container, SWT.BORDER);
spinner.setBounds(88, 105, 47, 21);
toolkit.adapt(spinner);
toolkit.paintBordersFor(spinner);
DragSource dragSource = new DragSource(container, DND.DROP_MOVE);
DropTarget dropTarget = new DropTarget(container, DND.DROP_MOVE);
Canvas canvas = new Canvas(container, SWT.NONE);
canvas.setBounds(94, 161, 174, 148);
toolkit.adapt(canvas);
toolkit.paintBordersFor(canvas);
createActions();
initializeToolBar();
initializeMenu();
}
public void dispose() {
toolkit.dispose();
super.dispose();
}
/**
* Create the actions.
*/
private void createActions() {
// Create the actions
}
/**
* Initialize the toolbar.
*/
private void initializeToolBar() {
IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
}
/**
* Initialize the menu.
*/
private void initializeMenu() {
IMenuManager manager = getViewSite().getActionBars().getMenuManager();
}
#Override
public void setFocus() {
// Set the focus
}
Can I write something like
public static void main(String[] args){
View v=new View();
Shell shell=new Shell(display);
v.createPartControl(shell);
}
Not really, Eclipse views are part of RCP and depend on a its infrastructure. You can create a small RCP application which only shows your view instead.
Having public static Display display=new Display(); in your view is completely wrong! There is certainly a Display already by the time your code runs, which can be accessed by Display.getDefault() (on any thread) or Display.getCurrent() (on GUI thread).

GWT popup is not centered when built within onClickHandler

My aim is to use GWT.runSync to load the popup contents only when required.
If I construct my widget as:
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
popupPanel.center();
}
});
}
}
Then the popup will be centered correctly.
If I build the popup within the clickHandler:
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
#Override
public void onClick(ClickEvent event) {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
popupPanel.center();
}
});
}
}
The popup will not center correctly. I have tried using setPositionAndShow, however the supplied offsets are 12, even though the CreateWidget is actually about 200px for both width and height.
I want to use the second method so I can eventually use GWT.runAsync within the onClick as CreateWidget is very complex.
I am using GWT-2.1.1
Seems to work by delaying the call to center. Perhaps a once off Timer would work as well. Delaying the call also works when wrapping buildUI within GWT.runAsync
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
#Override
public void onClick(ClickEvent event) {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
#Override
public boolean execute() {
popupPanel.center();
return false;
}
}, 50); //a value greater than 50 maybe needed here.
});
}
}
}
}