Using the file location to show the file in 'windows explorer' - java

Good Morning, I want to show the file location when the button "Locate" is pressed. Also I have full file path in JTextField, when I tried, I get an 'NullPointerException' exception, please give me directions, thanks
So far have tried:
public class locateExp {
private JFrame frame;
private JTextField txtCusersmyFirstPdf;
private Desktop desktop;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
locateExp window = new locateExp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public locateExp() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 489, 329);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton button = new JButton("Locate");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//full file path
File pdfFilepath = new File(txtCusersmyFirstPdf.getText());
if (pdfFilepath.exists()){
try {
//desktop.open(pdfFilepath.getParentFile());
desktop.open(pdfFilepath);
} catch(IOException ex) {
}
}
}
});
button.setMnemonic('t');
button.setFont(new Font("Calibri", Font.BOLD, 12));
button.setBorder(null);
button.setBackground(SystemColor.menu);
button.setBounds(126, 112, 44, 19);
frame.getContentPane().add(button);
JButton button_1 = new JButton("Open");
button_1.setMnemonic('o');
button_1.setFont(new Font("Calibri", Font.BOLD, 12));
button_1.setBorder(null);
button_1.setBackground(SystemColor.menu);
button_1.setBounds(180, 112, 44, 19);
frame.getContentPane().add(button_1);
JButton button_2 = new JButton("Print");
button_2.setMnemonic('p');
button_2.setFont(new Font("Calibri", Font.BOLD, 12));
button_2.setBorder(null);
button_2.setBackground(SystemColor.menu);
button_2.setBounds(228, 112, 44, 19);
frame.getContentPane().add(button_2);
JLabel label = new JLabel("File:");
label.setFont(new Font("Calibri", Font.BOLD, 12));
label.setBounds(114, 142, 44, 14);
frame.getContentPane().add(label);
JLabel lblMyFirstPdf = new JLabel("My First PDF Doc.pdf");
lblMyFirstPdf.setBounds(162, 141, 269, 14);
frame.getContentPane().add(lblMyFirstPdf);
JLabel label_2 = new JLabel("Path/name:");
label_2.setFont(new Font("Calibri", Font.BOLD, 12));
label_2.setBounds(89, 173, 63, 14);
frame.getContentPane().add(label_2);
txtCusersmyFirstPdf = new JTextField();
txtCusersmyFirstPdf.setText("C:\\Users\\My First PDF Doc.pdf");
txtCusersmyFirstPdf.setColumns(10);
txtCusersmyFirstPdf.setBounds(162, 168, 269, 23);
frame.getContentPane().add(txtCusersmyFirstPdf);
JLabel label_3 = new JLabel("File Size:");
label_3.setFont(new Font("Calibri", Font.BOLD, 12));
label_3.setBounds(106, 205, 325, 14);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("513 k bytes");
label_4.setBounds(162, 204, 269, 14);
frame.getContentPane().add(label_4);
}
}

If you get NullPointerException at desktop.open(pdfFilePath), have you checked whether the object desktop itself has been properly initialized? Assuming it is java.awt.Desktop you are using, verify if you have checked both
1. whether Desktop is supported on your platform and
2. whether OPEN Action is supported
You can do this as follows, before the call to desktop.open(...).
if(!Desktop.isDesktopSupported()){
System.out.println("Desktop is not supported");
return;
}
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.OPEN) && pdfFilepath.exists()) {
try {
//desktop.open(pdfFilepath.getParentFile());
desktop.open(pdfFilepath);
} catch(IOException ex) {
...
}
}
If you have initialized it properly as above and still get NullPointerException, the the file is null (as per docs). But since pdfFilepath.exists() returns true in your case, check the desktop initialization as shown above.

Related

Adding new JTextFields at the click of a JButton

I am working on an Invoice System and I want to create new fields each time the add new button is clicked.
It needs to add the fields in the code below each time.
The fields need to appear under its respective columns.
JPanel panel_2 = new JPanel();
panel_2.setBounds(10, 256, 990, 303);
panel.add(panel_2);
panel_2.setLayout(null);
code = new JTextField();
code.setBounds(10, 11, 86, 20);
panel_2.add(code);
code.setColumns(10);
code.setEditable(false);
desc = new JTextField();
desc.setBounds(106, 11, 345, 20);
panel_2.add(desc);
desc.setColumns(10);
desc.setEditable(false);
quantity = new JTextField("0");
quantity.setBounds(461, 11, 86, 20);
panel_2.add(quantity);
quantity.setColumns(10);
quantity.setEditable(false);
price = new JTextField("0");
price.setBounds(557, 11, 106, 20);
panel_2.add(price);
price.setColumns(10);
price.setEditable(false);
individualTotal = new JTextField();
individualTotal.setBounds(673, 11, 106, 20);
panel_2.add(individualTotal);
individualTotal.setColumns(10);
individualTotal.setEditable(false);
Below is my buttons that I have set up:
JButton newEntry = new JButton("+");
newEntry.setBackground(Color.PINK);
newEntry.setForeground(Color.BLUE);
newEntry.setFont(new Font("Tahoma", Font.BOLD, 15));
newEntry.setBounds(10, 204, 57, 20);
panel.add(newEntry);
newEntry.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
code.setEditable(true);
desc.setEditable(true);
quantity.setEditable(true);
price.setEditable(true);
individualTotal.setEditable(true);
}
});
newEntry.setEnabled(false);
JButton minusEntry = new JButton("-");
minusEntry.setBackground(Color.PINK);
minusEntry.setForeground(Color.RED);
minusEntry.setFont(new Font("Wide Latin", Font.BOLD, 16));
minusEntry.setBounds(77, 205, 57, 20);
panel.add(minusEntry);
minusEntry.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
code.setEditable(false);
desc.setEditable(false);
quantity.setEditable(false);
price.setEditable(false);
individualTotal.setEditable(false);
}
});
minusEntry.setEnabled(false);
I know there must be an answer somewhere on this site but I cannot seem to find it.
Please also note that I am new at Java development
Create your own subclass of a JPanel which represents one "datasheet" or tablecell or what ever
public DataPanel extends JPanel{
private JTextField field1 = new JTextField();
private JTextField field2 = new JTextField();
// ..... and so on
public DataPanel(YourDataObject data){
field1.setText(data.getValue1());
field2.setText(data.getValue2());
// ... and so on
// then add all of your text fields to the panel
add(field1);
add(field2);
// .... and so on
}
}
Then on button click you add the panel to the component you want to show it on
onClick(SomeEvent event){
yourComponent.add(new DataPanel(yourDataObject));
}

Issue in Radio Button Action Listener

I have written some code when one of the option in radio button is clicked
it should display one jlabel and jtext field. And when other option in the radio button is clicked it should hide the previous shown jlabel and jtext field and display new jlabel and jtext field.
In the output when I click on one of the radio button it is displaying nothing unless and until I maximize my Window. After geting my jlabel and jtextfield If I click on other radio button the jlabel and jtextfield is hidden but Im not able to see new jlabel and jtextfield for that radiobutton.
enter code here
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group =new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
jrdbuttonFullTime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jrdbuttonFullTime.isSelected()){
//jrdbtnContract.setSelected(false);
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
jlblEmpHour.setVisible(false);
jtxtHour_Pay.setVisible(false);
jtxtHour_Worked.setVisible(false);
jlblEmpWork.setVisible(false);
}
}
});
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
jrdbtnContract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jrdbtnContract.isSelected()){
//jrdbuttonFullTime.setSelected(false);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
jlblEmpSal.setVisible(false);
jtxtSal.setVisible(false);
}
}
});
jrdbtnContract.setBounds(218, 91, 109, 23);
panel_1.add(jrdbtnContract);
}
}
Insteed of adding and removing components, simply add all and hide/show them on radiobox selection like this:
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
ActionListener myAction = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jlblEmpHour.setVisible(jrdbtnContract.isSelected());
jtxtHour_Pay.setVisible(jrdbtnContract.isSelected());
jtxtHour_Worked.setVisible(jrdbtnContract.isSelected());
jlblEmpWork.setVisible(jrdbtnContract.isSelected());
jlblEmpSal.setVisible(jrdbuttonFullTime.isSelected());
jtxtSal.setVisible(jrdbuttonFullTime.isSelected());
}
};
myAction.actionPerformed(null); // to initialize labels first
jrdbuttonFullTime.addActionListener(myAction); // add actionlisteners
jrdbtnContract.addActionListener(myAction);// add actionlisteners
As you can see, you dont even need 2 separate action listeners as one but shared instance is just enough.
So the complete app will look like this:
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group = new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
ActionListener myAction = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jlblEmpHour.setVisible(jrdbtnContract.isSelected());
jtxtHour_Pay.setVisible(jrdbtnContract.isSelected());
jtxtHour_Worked.setVisible(jrdbtnContract.isSelected());
jlblEmpWork.setVisible(jrdbtnContract.isSelected());
jlblEmpSal.setVisible(jrdbuttonFullTime.isSelected());
jtxtSal.setVisible(jrdbuttonFullTime.isSelected());
}
};
myAction.actionPerformed(null); // to initialize labels first
jrdbuttonFullTime.addActionListener(myAction);
jrdbtnContract.addActionListener(myAction);
jrdbtnContract.setBounds(218, 91, 109, 23);
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
panel_1.add(jrdbtnContract);
}
}
I've revised your code a little. This should get you going on the right path:
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group = new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
//*******************************************************************
// Add all your salary fields here, not in ActionListeners
// Start them off invisible
//*******************************************************************
jlblEmpSal.setVisible(false);
panel_1.add(jlblEmpSal);
jtxtSal.setVisible(false);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
jlblEmpHour.setVisible(false);
panel_1.add(jtxtHour_Pay);
jtxtHour_Pay.setVisible(false);
panel_1.add(jlblEmpWork);
jlblEmpWork.setVisible(false);
jtxtHour_Worked.setVisible(false);
panel_1.add(jtxtHour_Worked);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
jrdbuttonFullTime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (jrdbuttonFullTime.isSelected()) {
//jrdbtnContract.setSelected(false);
// ****************************************************
// In ActionListeners for radiobuttons, hide the fields you
// don't want to see, make visible the ones you do want to see
// ****************************************************
jlblEmpSal.setVisible(true);
jtxtSal.setVisible(true);
jlblEmpHour.setVisible(false);
jtxtHour_Pay.setVisible(false);
jtxtHour_Worked.setVisible(false);
jlblEmpWork.setVisible(false);
}
}
});
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
jrdbtnContract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (jrdbtnContract.isSelected()) {
//jrdbuttonFullTime.setSelected(false);
// ****************************************************
// In ActionListeners for radiobuttons, hide the fields you
// don't want to see, make visible the ones you do want to see
// ****************************************************
jlblEmpHour.setVisible(true);
jtxtHour_Pay.setVisible(true);
jlblEmpWork.setVisible(true);
jtxtHour_Worked.setVisible(true);
jlblEmpSal.setVisible(false);
jtxtSal.setVisible(false);
}
}
});
jrdbtnContract.setBounds(218, 91, 109, 23);
panel_1.add(jrdbtnContract);
}
}
Your radio buttons start off both unchecked, so you see no salary detail initially. When you click one or the other, the corresponding details appear.
In which case(which radiobutton checked ?) you want to show which controls?

The method setVisible(boolean) is undefined for the type orbital_app

This is the code. My main frame is orbital_app and I would like it to when I click on JButton (button), data is being saved, the current window closes and another window orbital_app opens.
public class signup_try {
private JFrame frame;
private JTextField txtname;
private JTextField textusername;
private JTextField txtpass;
private JTextField textmail;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
signup_try window = new signup_try();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public signup_try() {
initialize();
}
Connection connection=null;
/**
* Initialize the contents of the frame.
*/
private void initialize() {
connection=dbase.dBase();
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("Orbital");
label.setForeground(SystemColor.activeCaption);
label.setFont(new Font("Arial Black", Font.BOLD, 17));
label.setBounds(170, 11, 71, 25);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("Name:");
label_1.setForeground(SystemColor.activeCaption);
label_1.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_1.setBounds(21, 66, 46, 14);
frame.getContentPane().add(label_1);
txtname = new JTextField();
txtname.setColumns(10);
txtname.setBounds(102, 63, 200, 22);
frame.getContentPane().add(txtname);
textusername = new JTextField();
textusername.setColumns(10);
textusername.setBounds(102, 108, 200, 22);
frame.getContentPane().add(textusername);
txtpass = new JTextField();
txtpass.setColumns(10);
txtpass.setBounds(102, 150, 200, 22);
frame.getContentPane().add(txtpass);
textmail = new JTextField();
textmail.setColumns(10);
textmail.setBounds(102, 192, 200, 22);
frame.getContentPane().add(textmail);
JLabel label_2 = new JLabel("Username:");
label_2.setForeground(SystemColor.activeCaption);
label_2.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_2.setBounds(21, 111, 71, 14);
frame.getContentPane().add(label_2);
JLabel label_3 = new JLabel("Password:");
label_3.setForeground(SystemColor.activeCaption);
label_3.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_3.setBounds(21, 154, 71, 14);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("Email:");
label_4.setForeground(SystemColor.activeCaption);
label_4.setFont(new Font("Arial Black", Font.PLAIN, 12));
label_4.setBounds(21, 196, 46, 14);
frame.getContentPane().add(label_4);
JButton button = new JButton("Sign Up");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query="insert into Users(Name, Username, Password, Email) values(?,?,?,?)";
PreparedStatement prepstat=connection.prepareStatement(query);
prepstat.setString(1, txtname.getText());
prepstat.setString(2, textusername.getText());
prepstat.setString(3, txtpass.getText());
prepstat.setString(4, textmail.getText());
prepstat.execute();
JOptionPane.showMessageDialog(null, "Data saved");
prepstat.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
I get the error saying that The method setVisible(boolean) is undefined for the type orbital_app. what should I do to fix this? Here I want to close this present frame(signup_try) and go to other frame (orbital_app).. setVisible is underlined red and says "The method setVisible(boolean) is undefined for the type orbital_app".
frame.dispose();
orbital_app orb=new orbital_app();
orb.setVisible(true);
}
});
button.setBounds(170, 239, 91, 23);
frame.getContentPane().add(button);
}
}
Orbital_app code(in this code setVisible works properly without any error):
package project;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.UIManager;
public class orbital_app{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
orbital_app window = new orbital_app();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public orbital_app() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(UIManager.getColor("Button.background"));
frame.setResizable(false);
frame.setBounds(100, 100, 450, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel toplbl = new JLabel("Orbital");
toplbl.setForeground(SystemColor.activeCaption);
toplbl.setFont(new Font("Arial Black", Font.BOLD, 17));
toplbl.setVerticalAlignment(SwingConstants.TOP);
toplbl.setBounds(182, 11, 71, 25);
frame.getContentPane().add(toplbl);
JLabel infolbl = new JLabel("Multipurpose app == orbital 1.0\r\n");
infolbl.setFont(new Font("Arial", Font.PLAIN, 11));
infolbl.setForeground(SystemColor.activeCaption);
infolbl.setBounds(138, 47, 165, 25);
frame.getContentPane().add(infolbl);
JButton signup_btn = new JButton("Sign Up");
signup_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
signup_form sup_for=new signup_form();
3.setVisible works here
sup_for.setVisible(true);
}
});
signup_btn.setFont(new Font("Arial", Font.PLAIN, 11));
signup_btn.setForeground(SystemColor.activeCaption);
signup_btn.setBounds(61, 133, 91, 23);
frame.getContentPane().add(signup_btn);
JButton signin_btn = new JButton("Sign In");
signin_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
signin_form log_for=new signin_form();
4.and here too
log_for.setVisible(true);
}
});
signin_btn.setFont(new Font("Arial", Font.PLAIN, 11));
signin_btn.setForeground(SystemColor.activeCaption);
signin_btn.setBounds(285, 133, 91, 23);
frame.getContentPane().add(signin_btn);
JLabel notelbl = new JLabel("note: click Sign Up for new account or Sign In for existing account.");
notelbl.setHorizontalAlignment(SwingConstants.CENTER);
notelbl.setBounds(10, 199, 406, 25);
frame.getContentPane().add(notelbl);
}
}
I faced the same problem, it is solved by exteding the JFrame class.
Your class should extends JFrame, because setVisible() method belongs to JFrameclass.
There seems to be a few issues with your code.
Most importantly: You have multiple entry points in your program.
You should only have one public static void main(String[] args) ... function in your application, that is where the 'program starts'.
The error message you get (The method setVisible(boolean) is undefined for the type orbital_app) comes from the fact that the class orbital_app donĀ“t have a setVisible function, one of its members does, but that doesn't matter.
Your orbital_app have a private member that is a JFrame, which makes it possible for you to call the JFrames methods from inside the orbital_app by accessing the frame, but you cant reach it from outside.
It seems you have mixed up inheritance and ownership.
If you wish your orbital_app class to be a JFrame, you need to inherit from the JFrame. Else you could just implement the methods you wish to make public for your other classes.
Or you could just create a getter for the private JFrame object so that you can access it from outside.

Refresh JTable using Vectors strings

I'm trying to refresh my table but it won't refresh. I tried using the fireTableDataChanged() as well as creating a new model but my Table will not budge. I can get the initial values in but I can't figure out how to refresh it.
HS = new Vector(Arrays.asList(finalHSArr));
SF = new Vector(Arrays.asList(finalFlagsArr));
I have two separate JTables that I am trying to refresh and these are the new values above. I've been trying for 6 hours now.
public class TemplateGui extends JFrame {
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField textField;
private static String [] sortedRoles_Flags,finalFlagsArr,finalHSArr;
private static String finalFlags="",finalHS="",columnToConvert="Result";
private Vector<String> SF,HS,column;
private JTable hotelSecurityTable,securityFlagsTable;
private DefaultTableModel hsTableModel,sfTableModel;
public TemplateGui(){
super("Galaxy Template Generator V1.0");
//column name
column = new Vector(Arrays.asList(columnToConvert));
getContentPane().setForeground(new Color(0, 0, 0));
getContentPane().setLayout(null);
getContentPane().setBackground(new Color(51, 51, 51));
//radio buttons
JRadioButton rdbtnNewRadioButton = new JRadioButton("Central User ");
rdbtnNewRadioButton.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnNewRadioButton.setBackground(Color.WHITE);
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(222, 75, 127, 36);
getContentPane().add(rdbtnNewRadioButton);
final JRadioButton rdbtnPropertyUser = new JRadioButton("Property User");
rdbtnPropertyUser.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnPropertyUser.setBackground(Color.WHITE);
buttonGroup.add(rdbtnPropertyUser);
rdbtnPropertyUser.setBounds(222, 38, 127, 34);
getContentPane().add(rdbtnPropertyUser);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 18));
textField.setBounds(10, 35, 53, 34);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblHotelSecurity = new JLabel("Hotel Security (H S)");
lblHotelSecurity.setHorizontalAlignment(SwingConstants.CENTER);
lblHotelSecurity.setFont(new Font("Tahoma", Font.BOLD, 13));
lblHotelSecurity.setBounds(10, 144, 189, 23);
lblHotelSecurity.setBackground(new Color(204, 204, 204));
lblHotelSecurity.setOpaque(true);
getContentPane().add(lblHotelSecurity);
JLabel label = new JLabel("Security Flags (S F)");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Tahoma", Font.BOLD, 13));
label.setBounds(222, 144, 372, 23);
label.setBackground(new Color(204, 204, 204));
label.setOpaque(true);
getContentPane().add(label);
JLabel lblEnterTemplateCode = new JLabel("ENTER TEMPLATE CODE");
lblEnterTemplateCode.setForeground(new Color(255, 255, 255));
lblEnterTemplateCode.setFont(new Font("Tahoma", Font.BOLD, 14));
lblEnterTemplateCode.setBounds(10, 9, 175, 23);
getContentPane().add(lblEnterTemplateCode);
JLabel lblSelectUserRole = new JLabel("SELECT USER ROLE LEVEL");
lblSelectUserRole.setForeground(new Color(255, 255, 255));
lblSelectUserRole.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSelectUserRole.setBounds(222, 13, 195, 14);
getContentPane().add(lblSelectUserRole);
//Submit button action
Button button = new Button("Generate Template");
button.setFont(new Font("Dialog", Font.BOLD, 12));
button.setBackground(new Color(102, 255, 102));
button.setForeground(Color.BLACK);
button.setBounds(467, 83, 127, 41);
getContentPane().add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Query excell = new Query();
//get template text
String template = textField.getText().toUpperCase();
System.out.println(template);
if(rdbtnPropertyUser.isSelected()){
try {
//property user was selected
excell.runProcess(1);
System.out.println("you selected Property user");
} catch (IOException e) {
e.printStackTrace();
}
}
else{
try {
//Central User was selected
excell.runProcess(2);
System.out.println("you selected central user");
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("NOW WERE HERE");
//get static variables from Excel Query
for(int i = 0; i< Query.sortedGF.length; i++)
{
if(Query.sortedGF[i].contains(template)){
sortedRoles_Flags =Query.sortedGF[i].split(" ");
System.out.println("THIS RAN"+" :"+i);
break;
}
}
System.out.println("NOW WERE HERE 103 " +Query.securityFlags.length);
//add data to table
int j=0;
int sizeOfFlags = Query.securityFlags.length;
//Add HS to FinalHS Variable only if Yes
for(int i=0;i< sortedRoles_Flags.length-sizeOfFlags;i++)
{
if(sortedRoles_Flags[i].matches("Y|y|Y\\?|\\?Y|y\\?|\\?y"))
{
System.out.println("Hotel security:"+" "+sortedRoles_Flags[i]+" HS Added: "+Query.hotelSecurity[i]);
finalHS += Query.hotelSecurity[i]+" ";
System.out.println("Hotel security:"+" "+finalHS);
}
}
//add Security Flags to Final Flags
for(int i=(sortedRoles_Flags.length-sizeOfFlags);i< sortedRoles_Flags.length;i++)
{
finalFlags += Query.securityFlags[j]+": "+ sortedRoles_Flags[i]+" + ";
j++;
}
//Leave open just incase they would prefer a text file for template in which case we just write it
System.out.println(finalFlags);
System.out.println(finalHS);
//Convert to String Arrays in order to add to our JTable
finalFlagsArr= finalFlags.split("\\+");
finalHSArr = finalHS.split(" ");
//convert to vectors
HS = new Vector<String>(Arrays.asList(finalHSArr));
SF = new Vector<String>(Arrays.asList(finalFlagsArr));
System.out.print(HS);
hsTableModel.fireTableDataChanged();
sfTableModel.fireTableDataChanged();
}
});
//scroll panes for flags
JScrollPane scrollPaneHS = new JScrollPane();
scrollPaneHS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneHS.setBounds(10, 170, 189, 185);
getContentPane().add(scrollPaneHS);
JScrollPane scrollPaneSF = new JScrollPane();
scrollPaneSF.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPaneSF.setBounds(222, 170, 372, 187);
getContentPane().add(scrollPaneSF);
//tables for updates
hsTableModel = new DefaultTableModel(HS,column);
hotelSecurityTable = new JTable(hsTableModel);
scrollPaneHS.setViewportView(hotelSecurityTable);
sfTableModel = new DefaultTableModel(SF,column);
securityFlagsTable = new JTable(sfTableModel);
scrollPaneSF.setViewportView(securityFlagsTable);
}
}
When you do
HS = new Vector<String>(Arrays.asList(finalHSArr));
SF = new Vector<String>(Arrays.asList(finalFlagsArr));
You change what HS and SF where point at, however, it does not change what, internally, the TableModel is pointing at.
You could use DefaultTableModel#setDataVector, but i might simpler to simply create new TableModels and set them to the JTables instead.
hsTableModel = new DefaultTableModel(HS, column);
hotelSecurityTable.setModel(hsTableModel);
sfTableModel = new DefaultTableModel(HS, column);
securityFlagsTable.setModel(sfTableModel);
As it's pretty much the same thing...
Updated
DefaultTableModel is expecting a Vector of Vectors, but you're supplying a Vector of Strings.
Try something like...
hsTableModel.setRowCount(0);
for (String row : HS) {
hsTableModel.addRow(new Object[]{row});
}
Instead...

Connecting of Database per each JFrame

I have created this Automated Library. And I need to know how to connect my Database per each JFrame.
This is my main method:
package com.Student.GUI;
public class Student_HomePage extends JFrame {
private JPanel contentPane;
private JTextField searchBox;
private static String results[];
private static Connection conn;
private static String searchText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DBConnect db = new DBConnect();
conn = db.openConnection();
Student_HomePage frame = new Student_HomePage();
frame.setVisible(true);
frame.setBounds(400,150,599,489);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Student_HomePage() {
setBackground(Color.WHITE);
setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\ITC302-Core Java\\java_workspace\\Pictures\\lala.png"));
setTitle("LookBook");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 489);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnHome.setEnabled(false);
btnHome.setContentAreaFilled(false);
btnHome.setBorder(null);
btnHome.setBounds(144, 24, 95, 25);
contentPane.add(btnHome);
JButton btnAuthors = new JButton("Authors");
btnAuthors.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnAuthors.setContentAreaFilled(false);
btnAuthors.setBorder(null);
btnAuthors.setBounds(241, 24, 95, 25);
contentPane.add(btnAuthors);
JButton btnBooks = new JButton("Books");
btnBooks.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 16));
btnBooks.setContentAreaFilled(false);
btnBooks.setBorder(null);
btnBooks.setBounds(338, 24, 95, 25);
contentPane.add(btnBooks);
JTextArea textArea = new JTextArea();
textArea.setText("|");
textArea.setFont(new Font("Monospaced", Font.PLAIN, 35));
textArea.setEditable(false);
textArea.setBounds(227, 11, 26, 57);
contentPane.add(textArea);
JTextArea textArea_1 = new JTextArea();
textArea_1.setText("|");
textArea_1.setFont(new Font("Monospaced", Font.PLAIN, 35));
textArea_1.setEditable(false);
textArea_1.setBounds(325, 11, 26, 57);
contentPane.add(textArea_1);
searchBox = new JTextField();
searchBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
searchText = temp;
}
});
searchBox.setColumns(10);
searchBox.setBounds(37, 210, 148, 25);
contentPane.add(searchBox);
JTextArea textArea_2 = new JTextArea();
textArea_2.setText("Search title of books,\r\nauthors or genre");
textArea_2.setFont(new Font("Plantagenet Cherokee", Font.PLAIN, 14));
textArea_2.setBounds(37, 248, 188, 40);
contentPane.add(textArea_2);
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
btnSearchActionPerformed(e, temp);
}
});
btnSearch.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\searchIcon.png"));
btnSearch.setContentAreaFilled(false);
btnSearch.setBorder(null);
btnSearch.setBackground(Color.WHITE);
btnSearch.setBounds(185, 210, 87, 25);
contentPane.add(btnSearch);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\download.jpg"));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(270, 202, 290, 215);
contentPane.add(label);
JLabel bookIcon = new JLabel("");
bookIcon.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\BookIcon.png"));
bookIcon.setBounds(101, 78, 116, 90);
contentPane.add(bookIcon);
JLabel lookBookIcon = new JLabel("");
lookBookIcon.setIcon(new ImageIcon("D:\\ITC302-Core Java\\java_workspace\\Pictures\\image.png"));
lookBookIcon.setBounds(213, 74, 290, 135);
contentPane.add(lookBookIcon);
}
public void btnSearchActionPerformed (ActionEvent e, String searchText) {
DBConnect db = new DBConnect();
results = db.selectMatchedData(searchText, conn);
//if (results.length != 0) {
this.dispose();
Student_SearchList window = new Student_SearchList();
window.setVisible(true);
//}
//else {
//Student_Warning window = new Student_Warning();
//window.setVisible(true);
//}
}
}
And this is where I want to connect my Database:
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = new String(searchBox.getText());
btnSearchActionPerformed(e, temp);
}
});
This whole code is my Main.java.

Categories

Resources