I'm trying to find out, how to set the navigator.languages in JXBrowser. The array is always empty and for the specific site I'm on with the JXBrowser, I need to have the navigator.languages to be set.
I added the flag "--lang=en" to chromium, I also did set
BrowserContextParams bcp = new BrowserContextParams(browserContextPath);
bcp.setAcceptLanguage("en-US");
But all these things seem to not change anything. Is that a general JXBrowser "thing" or "bug"? Or am I just missing something?
I would rather not intercept every request and try to add or inject navigator.languages manually.
The corresponding Chromium switch seems to work properly and the navigator.language property is changed:
public class JxBrowserSample {
public static void main(String[] args) {
BrowserPreferences.setChromiumSwitches("--lang=zh-CN");
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.loadURL("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_nav_language");
}
}
Output:
The navigator.languages[0] contains the same value in this case.
I have used JxBrowser 6.19.1 for checking this behavior.
Related
I was reading about the cross-platform look & feel ie the metal L&f that looks the same across all platforms. I wanted to know what will be the shortcut keys for this l&f. I'm facing an issue where in MAC OS, if I have to copy/paste I have to use window shortcuts(ctrl + v/c) for copy/paste actions.I'm unable to use mac shortcuts (cmd+v/c). I'm wondering if it could be due to the l&f. Please help.Thanks in advance.
Yes, the look and feel can do this. One thing you can do, which I hope somebody has a better answer, is to set the input map for the components you want to behave differently. Here's a way to use the platform default input map for a text field.
public static void startGui(){
try{
LookAndFeel aqua = UIManager.getLookAndFeel(); //aqua
UIDefaults def = UIManager.getDefaults();
Object b = def.get("TextField.focusInputMap");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
UIDefaults def2 = UIManager.getDefaults();
def2.put("TextField.focusInputMap", b);
}catch(Exception e){
e.printStackTrace();
}
JFrame frame = new JFrame("title");
JTextField field = new JTextField();
frame.add(field);
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
EventQueue.invokeLater( ()->startGui() );
}
If you run that, then cmd+v should paste, but if you commend out the line def2.put(...) then ctrl+v will paste.
Verified with jdk11 on OSX.
I am trying to add a model to a JTable, which was created using IntelliJ Forms. As of right now, the main method has to be static, and if I make the JTable static as well, then IntelliJ says it cannot bind the JTable. I am confused on how I can add the model in this case.
public class DisplaySettings {
private JTable resolutionsTable;
private JPanel displaySettings;
public static void main(String[] args) {
JFrame frame = new JFrame("Display Settings");
frame.setContentPane(new DisplaySettings().displaySettings);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
String[] columns = {"Resolution Size"};
DefaultTableModel model = new DefaultTableModel(columns, 0);
resolutionsTable.setModel(model);
}
}
When you are dealing with IntelliJ Forms, they are automatically handled and allocated for by IntelliJ, by default. If you select the component you are working with in the ComponentTree, in the .form GUI Editor, there is an option called Custom Create. Check that.
Once that is checked, IntelliJ will automatically create a method called createUIComponents(). There you can allocate your JTable and set the model, since this method is not in a static context. This method will be automatically called when creating the UI.
Using the following works for me -
JTable resolutionsTable = new JTable(); // instances of both JTable and JPanel
JPanel displaySettings = new JPanel();
... // you can set the above component with diff attributes
frame.setContentPane(displaySettings);
... // and use them further
resolutionsTable.setModel(model);
When writing a standalone java application, I see a lot of beginners code in the static context.
I used to get around this problem by creating an instance of the class in main, and working from the constructor.
I've added a few examples of a very simple standalone program, and would like to know if there are best practises for "leaving" the static context.
I would also like to know if there are things a standalone java program should be doing in the static context or specifically in the main method, what it's function is besides being the entry point of every standalone java program.
Any reading material is also welcome!
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ExampleStatic
{
JLabel label;
public static void main(String[] args)
{
//Option 1 - Work from static context:
JFrame frame = new JFrame();
frame.setBounds(10,10,100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel staticlabel = new JLabel("static");
frame.add(staticlabel);
frame.setVisible(true);
//Option 2 - Create instance, call initialisation function
ExampleStatic e = new ExampleStatic();
e.initialise();
//Option 3 - Create instance, handle initialisation in constructor
new ExampleStatic(true);
}
public ExampleStatic(){}
public ExampleStatic(boolean init)
{
JFrame frame = new JFrame();
frame.setBounds(10,10,100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("constructor");
frame.add(label);
frame.setVisible(true);
}
public void initialise()
{
JFrame frame = new JFrame();
frame.setBounds(10,10,100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("init function");
frame.add(label);
frame.setVisible(true);
}
}
Option 2 and Option 3 both are fine, as both provide the loose coupling as well if you want to use your instance somewhere else in other classes you can use it easily. But if everything you will write in Main method you are going to loose the scope and its reusability.
The JVM needs main to be static, after that you're free to do what you want. I would call a non-static "second main" that would handle initialization and then any further processing in different methods (or classes).
I would avoid putting things in the constructor, unless you really feel it's the right place for them.
SEE UPDATE AT THE BOTTOM!!
I've tried to figured out how to do this for a couple of days but so far I have had no luck.
Basically what I want to do is have a combobox, which when an option is selected loads an applet, and passes a value to the applet.
Here is the code for the ComboBox class, which is supposed to open the other class in a new window. The other class is the main class for an applet. They are both in the same project but in different packages. I know that there aren't any errors with the rest of the code.
//where I evaluate the selection and then open SteadyStateFusionDemo
// more selections just showing one code block
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
String str = (String)combo.getSelectedItem();
if (str.equals("NSTX")) {
machine = "A";
JFrame frame = new JFrame ("MyPanel2");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
SteadyStateFusionDemo d = new SteadyStateFusionDemo();
frame.getContentPane().add (new SteadyStateFusionDemo());
d.init();
frame.pack();
frame.setVisible (true);
And just to cover everything here is the beginning of the init() method of SteadyStateFusionDemo as well as the main method in the class. Too much code to post otherwise. There are several different privates before the init method.
//method that initializes Applet or SteadyStateFusionDemo class
public void init() {
//main method of the SteadyStateFusionDemo class
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new SteadyStateFusionDemo());
frame.pack();
frame.setVisible (true);
What am I doing wrong? Why doesn't my class load?
UPDATED: Changed the code so that a JFrame opens and then the JApplet loads inside. I have successfully done this in a test Java applet but for some odd reason it won't work with this Applet. I even set up the test in a similar way (The code for this is virtually the same, except with different class names, and of course a much, much shorter init() method). Can someone help me figure out why this isn't working? Also, a JFrame will open if I delete the lines referring to SteadyStateFusionDemo, but once I reference it won't work. Why does this happen?
UPDATE:
Based on your feedback, it seems you are trying to achieve the following:
Use the code of an existing Applet (found here) in a Desktop application (i.e. in a JFrame).
Converting an Applet to a Desktop application is an "undertakable" task, the complexity of which depends on how much "Applet-specific" stuff is used by the Applet. It can be as simple as creating a JFrame and adding myFrame.setContentPane(new myApplet().getContentPane()); or as complex as...hell.
This tutorial might be a good place to start.
After taking a look at the Applet at hand, it seems to be fairly easy to convert it. The only complicating factor is the use Applet's methods getCodeBase() and getImage(URL) (somewhere in the code). These two methods result in a NullPointerException if the Applet is not deployed as...an Applet.
So, what you can do is override those two methods in order to return the intended values (without the exception). The code could look like this:
/* Import the necessary Applet entry-point */
import ssfd.SteadyStateFusionDemo;
/* Subclass SSFD to override "problematic" methods */
SteadyStateFusionDemo ssfd = new SteadyStateFusionDemo() {
#Override
public URL getCodeBase() {
/* We don't care about the code-base any more */
return null;
}
#Override
public Image getImage(URL codeBase, String imgPath) {
/* Load and return the specified image */
return Toolkit.getDefaultToolkit().getImage(
this.getClass().getResource("/" + imgPath));
}
};
ssfd.init();
/* Create a JFrame and set the Applet as its ContentPane */
JFrame frame = new JFrame();
frame.setContentPane(ssfd);
/* Configure and show the JFrame */
...
The complete code for an example JFrame Class can be found here.
Of course, you need to have all Classes from the original Applet accessible to your new Class (e.g. put the original Applet in your classpath).
I'm new to GWT, and am struggling through my first Web page with it.
I've created 2 Composite Widgets - ListWidget and MaintenanceWidget. When I add them both to a FlowPanel, they both show up as they should. However, when I try to use a SplitLayoutPanel, depending on how I do it, either none of them show or only one of them shows.
Below is my code:
public MainPanel(){
list = new ListWidget();
maintenance = new MaintenanceWidget();
panel = new SplitLayoutPanel();
panel.addWest(list, 200);
panel.addNorth(maintenance, 250);
initWidget(panel);
}
In my entry point onModuleLoad() method, I create an instance of MainPanel and add it to the root pane.
With this code, I get a blank space in the west where the list should be, and the maintenance widget on the top with a horizontal splitter beneath it.
I've tried different configurations of the panel.add****() method, but nothing has gotten me the results I'm looking for.
Any ideas? Thanks!
Make sure you have a doctype declaration in your HTML template (for example, <!doctype html>), since SplitLayoutPanel requires browser to work in standards mode.
I found some sample code here that used a method that I hadn't seen before.
My code now reads as follows:
public MainPanel(){
list = new ListWidget();
maintenance = new MaintenanceWidget();
panel = new SplitLayoutPanel();
panel.setPixelSize(500, 400);
panel.addWest(list, 200);
panel.add(maintenance);
initWidget(panel);
}
And now it works. Thanks for your help!
If not mistaken, SpliLayoutPanel must be attached to the body of the document.
Try something like:
public void onModuleLoad() {
SplitLayoutPanel panel = new SplitLayoutPanel();
panel.addWest(new Label("WEST"), 50);
panel.addNorth(new Label("NORTH"), 50);
panel.addEast(new Label("EAST"), 50);
panel.addSouth(new Label("SOUTH"), 50);
panel.add(new Label("CONTENT HERECONTENT HERECONTENT HERECONTENT HERECONTENT HERECONTENT HERE"));
RootLayoutPanel.get().add(panel); //This gets the body element and attaches itself to it, then adds panel.
}
Shouldn't be too hard to apply it to your code.