Im currently working on an aplication on Eclipse and what I need is to press a button and then a JFrame to open, containing values from MySQL DB on a JTable. I had recently asked a question here about an error I had in trying to show columns using absolute layout(null layout) and I was told that it would be preferable to use layouts. The thing is that i have multiple buttons and stuff on my existing form and changing the layout would hide some of the features and I couldn't find a solution.So I thought of something. Since the JTable has issues with absolute layout and Layout Managers dont help me with my multiple buttons, I could delete the JTable from my existing form(null layout) and keep the buttons. As a result, when a user presses the "Sort by Name" or "Add Console" buttons I have for mySQL app on the existing form, then a new JFrame should pop up and show the correct JTable. Is there a possible way to do that?
JTable Creation Process :
package newWindow;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class newWindow {
public static void main(String[] args) {
new newWindow();
}
public newWindow() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {}
try
{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=mySQL&password=7777");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
String[] cols = new String[columnCount];
int i;
for (i=1;i<= columnCount;i++)
{
cols[i-1] = md.getColumnName(i);
}
DefaultTableModel model = new DefaultTableModel(cols,0);
while (rs.next())
{
Object[] row = new Object[columnCount];
for (i = 1 ; i <= columnCount ; i++)
{
row[i-1] = rs.getObject(i);
}
model.addRow(row);
}
JFrame frame = new JFrame("Promitheas");
JTable table = new JTable(model);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout());
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
conn.close();
stmt.close();
rs.close();
} catch (SQLException case1) { case1.printStackTrace();
} catch (Exception case2) { case2.printStackTrace();}
}
});
}
}
Sort by Name Button :
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=mySQL&password=7777");
String[] gender = { "ASC", "DESC"};
int response = JOptionPane.showOptionDialog(null,"Ascending or Descending Order?","Make a choice...",0,JOptionPane.INFORMATION_MESSAGE,null,gender,gender[0]);
stmtsortname = conn.createStatement();
String sql = null;
if (response == JOptionPane.YES_OPTION)
sql = "ASC";
if (response == JOptionPane.NO_OPTION)
sql = "DESC";
rssortname = stmtsortname.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id ORDER BY consoles.name "+sql);
.
.
.
create an newJTable overwriting the old
.
.
} catch ...