How to read from URL after a button press - java

Ok hello, What I want is when my button is pressed I want read the text from a URL and display it in the GUI,
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
//???????????????????
}
I am very confused on this. I saw it in a Open Source project and I can't get it to work :/ All I really want is it to open up to a raw github file (in the GUI) to display the contents of the github.
This is the github link
I want the text to display as if it where actually in the application.
Thanks for any help

What I would do, is go ahead and make a new JFrame, named something different than your previous one, but set it's visibility to false. Within that new JFrame, "ex", add a layout and then add you github info into the layout.
ex:
JFrame ex = new JFrame();
ex.setSize(50,50);
ex.setVisible(false);
ex.setResizable(falsE);
ex.setDefaultCloseOperation(JFrame.Dispose);
//Use "dispose" to exit the window but not terminate your program
and then, when you have a JButton clicked, or whatever you're using, use an actionevent.
ex:
jButton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("JButton1 Button pressed");
ex.setVisible(true);
}
});

Related

Why ChangeListener of JTabbedPane loads with starting of main JFrame?

Following is snap of my ChangeListner method, when i run my project, before i actually reach to tabbedPan the
JOptionDialog pops out(actually along with jframe loads)! My actual purpose is i want to listen the changing of tabs so that i can load some contents on that tab from database! help me
jTabbedPane1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
jtabpanChangeListner(evt);
}
});
private void jtabpanChangeListner(ChangeEvent evt) {
// TODO add your handling code here:
int index = jTabbedPane1.getSelectedIndex();
String msg = jTabbedPane1.getTitleAt(index);
System.out.println("Tab changed to: " +msg);
JOptionPane.showMessageDialog(jTabbedPane1,"hello change me do you?+");}
JTabbedPane sends ChangeEvent's when a selected tab changes. In particular, when the JTabbedPane is empty and you add a first tab the JTabbedPane will send the ChangeEvent meaning that selected tab changed from null to something.
You need to either take into account that first change event, or to add the ChangeListener after you added the first tab into the JTabbedPane.

create an exit button in java?

I'm a beginner in java, I'm practicing a Project that have should create an exit button that exit the program when we press it. But when I run this project in JDK the exit button is not working.
How can I make exit button work?
There two ways for closing a window:
X exit button at the top side of the window.
Custom exit button.
Solution for 1
To achieve it you should set for the JFrame the suitable default action you want to produce when clicking on X.
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Solution for 2
this.dispose();
if you want to create a message of yes_no option than follow the given code whenever u press the exit button you have created in J Frame so it will show a dialog message confirm if you want to Exit click yes so your app will be closed.
private void JbtnExitActionPerformed(java.awt.event.ActionEvent evt) {
Frame = new JFrame("Exit");
if (JOptionPane.showConfirmDialog( Frame,"confirm if you Want to Exit","Name of the Application or Title",
JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
System.exit(0);
If you want a JButton that closes the application, you would create the button:
JButton Button = new JButton("Close");
Then you would add a custom handler to the button:
Button.addActionListener (new ActionListener ()) {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
};
Then you would add the button to the frame or panel.
frame.add(button);
(If your frame is called frame)

Opening a new GUI window on a main GUI using GUI builder on netbeans

I am playing about with GUI builder and i was wondering if there is an easy way to open the register window through the current main window (in reference to the page below). I am trying to do this through the menu bar.
I've been trying all day, because GUI Builder generates some code, its not possible to edit this code.
Thanks For the help!
Create a separate class which extends JDialog class and add your GUI components:
public Register extends JDialog {
//Make GUI
setModalityType(ModalityType.APPLICATION_MODAL); //Make it modal
}
Add ActionListener to that menu item which is supposed to open a register window:
mnuItmRegisteration.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Register r = new Register();
r.setVisible(true);
}
});
Right click on that shortcut button, click Events, click ActionPreformed.
There you should write codes to make your register window appear.
An example:
private void RegisterationEventActionPerformed(java.awt.event.ActionEvent evt) {
JFrame Register = new Register();
Register.setVisible(true);
}
Remember to make another JFrame called ("Register" assuming u are using the code i gave) at the same package as your current JFrame
Maybe u would probably should use the run button (The button with a Green Triangle or Arrow), run it try to press the menu item, it should appear the register window.

Accessing a Button from another Object

I am busy doing a school project and hoped some awesome people out there could help me.
I currently have a Main, a Log in GUI and a Search frame (designed with Netbeans Gui dev).
I have an actionPerformed(ActionEvent e) method, where I am struggling is to access the Log in button that is on my Log in GUI. Currently I am doing this.
In my constructor: Declare the Frame. LogInFrame x = new LogInFrame();
In
public actionPerformed(ActionEvent eve)
{
if(eve.getSource()==x.LogInBtn())
{
x.setVisible(false);
}
}
At the moment the button isn't responding, so I was wondering if I was doing anything wrong.
The current Frame is just a standard frame. Really simple
public static void main (String[] args)
{
JFrame LogInFrame = new JFrame ("Log in");
LogInFrame.setSize (300, 300);
JButton close = new JButton ("Hid frame");
LogInFrame.getContentPane ().add (close);
}
}
Thats about everything that is needed. The problem does not lie with the GUI itself as I can run the GUI in netbeans fine. I can use the auto generated actionPerformed method of the button by double clicking on it. But i want to be able to access my GUI's compenents in my main.(tell the button what to do from my main). I have made sure that the button is public and can be accessed. I dont get any physical "coding" errors, the code just doesnt seem to be working (the button isnt responding if I add events from my main)

Desktop application - how do I dynamically create and destroy forms

I'm creating a small crypto app for the desktop using java.
I'm using JFrames (import javax.swing.JFrame) with Oracle
JDeveloper 11g under Linux.
I want to have a "welcome" form/frame where users can choose
their encryption method, and then on choosing the method,
I want to dynamically create the appropriate form for the
chosen encryption method and also destroy/free/dispose() of
the welcome form. When the user has finished their encrypting,
they should close the frame/form (either by clicking on the
x at the top right - or using the Exit button or by any
method) and the welcome frame should be dynamically recreated
and appear.
I've tried various things - btnEncode_actionPerformed(ActionEvent e)
then this.dispose() - and I've fiddled with this_windowClosed(WindowEvent e)
and dispose(), but nothing seems to work.
Even a workaround using setVisibl(true/false) would be acceptable at
this stage - this has been wrecking my head all day. It's very
easy to do in Delphi!
TIA and rgs,
Paul...
something like this usually does the trick: (Note I haven't tested this)
public class WelcomeMsg extends JFrame
.
.
.
public void btnContinue_actionPerformed(ActionEvent e)
{
this.dispose();
SwingUtilities.invokeLater(new Runnable(){ new JFrameAppropriateWindow(args) });
}
where btnContinue is the Continue button on your welcome form and JFrameAppropriateWindow is the next frame you would like to show depending on the user's choice. Args are any arguments you need to pass.
When you are ready, you can simply dispose the current frame and relaunch an instance of WelcomeMsg
I put together this simple example for creating and displaying a panel depending on a user choice.
public class Window extends JFrame {
public Window() {
this.setLayout(new BorderLayout());
JComboBox encryptionCombobox = new JComboBox();
encryptionCombobox.addItem("foo");
encryptionCombobox.addItem("bar");
//...
encryptionCombobox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// find choices and the correct panel
JPanel formPanel = new JPanel();
formPanel.setOpaque(true);
formPanel.setBackground(Color.RED);
//...
Window.this.add(formPanel, BorderLayout.CENTER);
Window.this.validate();
Window.this.repaint();
}
});
add(encryptionCombobox, BorderLayout.NORTH);
}
public static void main(String[] args) {
new Window().setVisible(true);
}
}
When I come to think about it, you should probably use a CardLayout instead, which allows you to switch between different panels (cards).

Categories

Resources