JTable inside JOptionPane does not resize properly - java

i have a panel(BorderLayout) with some labels and tables and want to show it via JOptionPane. Somehow the result does not look as i want. (see image)
Here is the relevant code : ...
//init tablecontent
String[] obj_errColNames = {"WOC", "Error"};
String[][] obj_errTblData = new String[liFailFiles.size()][2];
n_entry = 0;
for(Entry<String,String> s : liFailFiles.entrySet()) {
obj_errTblData[n_entry][0] = s.getKey();
obj_errTblData[n_entry][1] = s.getValue();
n_entry++;
}
//add table into the panel
panel.add(new JScrollPane(tbl_err), BorderLayout.SOUTH);
tbl_err.setPreferredScrollableViewportSize(tbl_err.getPreferredSize());
tbl_err.setFillsViewportHeight(true);
As you can see in the Image the text in the Error column is not displayed full. What can i do ?

Related

Searching ArrayList using JTable and JButton

I have an ArrayList holding football matches and when the user types a date and presses "search" button, a new JTable opens showing you all matches played on that day. I have looped to get the date and compared it to the input inside the JTextField but it just gives me an empty table even if there is a record of a match played on the date the user enters. In this code below, I am just using hitting enter on JTextField to execute search because I do not know how to map JTextField to JButton. I have tried but it just prints the search Jbutton name.
public void searchMatch(ArrayList<Matches> searchMatch, String e)
{
DefaultTableModel searchModel = new DefaultTableModel();
for(int i = 0; i < searchMatch.size(); i++)
{
if(searchMatch.get(i).getM_date().equals(e))
{
System.out.println(searchMatch.get(i).getM_date());
String date = searchMatch.get(i).getM_date();
String teamName = searchMatch.get(i).getM_teamName();
String teamName2 = searchMatch.get(i).getM_teamName2();
int goalsScoredTeam1 = searchMatch.get(i).getGoalsTeam1();
int goalsScoredTeam2 = searchMatch.get(i).getGoalsTeam2();
Object[] row = {teamName, teamName2, goalsScoredTeam1, goalsScoredTeam2,date};
searchModel.addRow(row);
JTable searchTable = new JTable(searchModel);
searchTable.setFillsViewportHeight(true);
JPanel searchPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(searchTable);
searchPanel.add(scrollPane);
JFrame frame = new JFrame("Searched Matches");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
searchTable.setOpaque(true);
frame.setContentPane(searchPanel);
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
}
}
}
DefaultTableModel searchModel = new DefaultTableModel();
You TableModel has no columns to display.
Even though you add rows of data, none of the data can be displayed unless you also have defined the "column names" for the TableModel.
Your code should be something like:
String columnNames = { "Date", "Name", "..." };
DefaultTableModel searchModel = new DefaultTableModel(columnNames, 0);
Which will create an empty TableModel with just the column names. Your looping code will then add each row of data.
Note, you should also look at storing all the data in your TableModel and then just filter the TableModel. Read the section from the Swing tutorial on Sorting and Filtering for a working example.

how to combine joptionpane with combobox

i want my joptionpane can combine with combobox,and the combobox data is in database, how i managed that.
i've tried change but the red code always show
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String wel = sdf.format(cal1.getDate());
String NamaFile = "/report/harianMasuk.jasper";
HashMap hash = new HashMap();
String tak = JOptionPane.showOptionDialog(null,id.getSelectedIndex()-1,"Laporan Supplier",JOptionPane.QUESTION_MESSAGE);
try {
hash.put("til", wel);
hash.put("rul", tak);
runReportDefault(NamaFile, hash);
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, e);
}
Read the section from the Swing tutorial on Getting User Input From a Dialog.
It demonstrates how to display a combo box in a JOptionPane.
Not exactly sure what you are trying to accomplish but it appears to be that you want to utilize a JComboBox within a JOptionPane dialog window. This ComboBox would be filled with specific data from your database. The User is to select from this ComboBox and your application continues processing based on that selection. If this is the case then you might want to try something like this:
String selectedItem = "";
int selectedItemIndex = -1;
/* Ensure dialog never hides behind anything (use if
the keyword 'this' can not be used or there is no
object to reference as parent for the dialog). */
JFrame iframe = new JFrame();
iframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
iframe.setAlwaysOnTop(true);
// ---------------------------------------------------
int btns = JOptionPane.OK_CANCEL_OPTION;
String dialogMessage = "<html>Select the desired item from the Drop-Down "
+ "list<br>you want to work with:<br><br></html>";
String dialogTitle = "Your Fav Items";
/* Up to you to gather what you want placed into the
JComboBox that will be displayed within the JOptionPane. */
String[] comboBoxItems = {"Your", "DB", "Items", "You", "Want", "To",
"Add", "To", "ComboBox"};
BorderLayout layout = new BorderLayout();
JPanel topPanel = new JPanel(layout);
JLabel label = new JLabel(dialogMessage);
topPanel.add(label, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new BorderLayout(5, 5));
JComboBox cb = new JComboBox();
cb.setModel(new DefaultComboBoxModel<>(comboBoxItems));
cb.setSelectedIndex(-1);
centerPanel.add(cb, BorderLayout.CENTER);
topPanel.add(centerPanel);
// Ensure a selection or Cancel (or dialog close)
while (selectedItemIndex < 0) {
int res = JOptionPane.showConfirmDialog(iframe, topPanel, dialogTitle, btns);
if (res == 2) {
selectedItem = "Selection Option Was Canceled!";
break;
}
selectedItemIndex = cb.getSelectedIndex();
if (res == JOptionPane.OK_OPTION) {
if (selectedItemIndex == -1) {
JOptionPane.showMessageDialog(iframe, "<html>You <b>must</b> "
+ "select something or select <font color=red><b>Cancel</b></font>.",
"Invalid Selection...", JOptionPane.WARNING_MESSAGE);
}
else {
selectedItem = cb.getSelectedItem().toString();
}
}
iframe.dispose();
}
JOptionPane.showMessageDialog(iframe, "<html>You selected the ComboBox item:"
+ "<br><br><b><font color=blue><center>" + selectedItem + "</center>"
+ "</font></b><br></html>", "Selected Item", JOptionPane.INFORMATION_MESSAGE);
iframe.dispose();
With the above code, the Input dialog that will be displayed would look something like this:
It is up to you to find the means to fill the comboBoxItems String Array used within the code above.

Java Swing - JTable Not Showing in Scroll Pane

I created a class Cart and inside is a JTable and two ArrayLists. For some reason, my JTable is not displaying.
Here is my Cart Class:
class Cart {
ArrayList<Product> products = new ArrayList<>(); // Holds the products themselves
ArrayList<Integer> quantities = new ArrayList<>(); // Holds the quantities themselves
JTable prdTbl = new JTable(); // The GUI Product Table
DefaultTableModel prdTblModel = new DefaultTableModel(); // The Table Model
Object[] columns = {"Description","Price","Quantity","Total"}; // Column Identifiers
DecimalFormat fmt = new DecimalFormat("$#,##0.00;$-#,##0.00"); // Decimal Format for formatting USD ($#.##)
Cart() {
setTableStyle();
}
void renderTable() {
// Re-initialize the Table Model
this.prdTblModel = new DefaultTableModel();
// Set the Table Style
setTableStyle();
// Create a row from each list entry for product and quantity and add it to the Table Model
for(int i = 0; i < products.size(); i++) {
Object[] row = new Object[4];
row[0] = products.get(i).getName();
row[1] = products.get(i).getPrice();
row[2] = quantities.get(i);
row[3] = fmt.format(products.get(i).getPrice() * quantities.get(i));
this.prdTblModel.addRow(row);
}
this.prdTbl.setModel(this.prdTblModel);
}
void setTableStyle() {
this.prdTblModel.setColumnIdentifiers(columns);
this.prdTbl.setModel(this.prdTblModel);
this.prdTbl.setBackground(Color.white);
this.prdTbl.setForeground(Color.black);
Font font = new Font("Tahoma",1,22);
this.prdTbl.setFont(font);
this.prdTbl.setRowHeight(30);
}
JTable getTable() {
renderTable(); // Render Table
return this.prdTbl;
}
}
Note: some methods have been removed such as addProduct() and removeProduct(), as I feel they aren't necessary. If you need to see them, please ask.
Here is my initialize() method for the Swing Application Window:
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 590, 425);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Cart cart = new Cart();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, "cell 0 0,grow");
JPanel cartPanel = new JPanel();
tabbedPane.addTab("Cart", null, cartPanel, null);
cartPanel.setLayout(new MigLayout("", "[grow]", "[][grow]"));
JScrollPane scrollPane = new JScrollPane();
cartPanel.add(scrollPane, "cell 0 1,grow");
table = new JTable();
scrollPane.setViewportView(table);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] item = {"Macadamia", "Hazelnut", "Almond", "Peanut", "Walnut", "Pistachio", "Pecan", "Brazil"};
Double[] price = {2.00, 1.90, 1.31, 0.85, 1.12, 1.53, 1.25, 1.75};
int choice = (int) (Math.random() * item.length);
Product p = new Product(item[choice], price[choice]);
cart.addProduct(p);
table = cart.getTable();
}
});
cartPanel.add(btnAdd, "flowx,cell 0 0");
JButton btnRemove = new JButton("Remove");
cartPanel.add(btnRemove, "cell 0 0");
JButton btnClear = new JButton("Clear");
cartPanel.add(btnClear, "cell 0 0");
}
I'm not sure if I'm missing something here? It has worked fine like this in the past? I've also tried printing out values at table = cart.getTable();, and it seems to be receiving the values fine, so it leads me to believe it has something to do with the Swing initialize() rather than my Cart class, but just in case I posted the Cart class as well.
Are you sure you're adding the right table? Your code shows:
table = new JTable();
scrollPane.setViewportView(table);
I cannot see where's table declared, further more table contains nothing, no rows no columns, while inside actionListener you initialize table with a new instance:
table = cart.getTable();
but the scrollPane holds another instance of JTable.
It looks like you never associate your cart with your cartPanel; I think your problem is here:
JPanel cartPanel = new JPanel();
you make the new panel but never hook your cart to it. Looks good otherwise.
Good luck!

Remove or Hide JScrollPane bug

I have this program that has a button where it can bring data from database. The thing is it has to check the database for data. If there is data, then it displays it in a form of a Table. If not, shows a message (in JLabel) that no data exists. So far, so good. The issue is, after viewing the table, and then remove all data from database, it shows there is no data that exists. But, the table is still there. I can still see it, And it refuses to be removed. Why?
Here's Code:
else if(ae.getSource()==Test)
{
try
{
JTable table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
String query="SELECT * FROM Students ORDER BY StdID";
rs=st.executeQuery(query);
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addColumn("No.");
model.addColumn("StdID");
model.addColumn("StdName");
model.addColumn("StdMajor");
model.addColumn("StdGender");
model.addColumn("StdAge");
model.addColumn("StdEmail");
model.addColumn("StdPhone");
model.addColumn("RegisteredBy");
model.addColumn("UpdatedBy");
int result=0;
int stdid;
String stdname;
String stdmajor;
String stdgender;
int stdage;
String stdemail;
String stdphone;
String registeredby;
String updatedby;
while(rs.next())
{
result = result + 1;
stdid = rs.getInt("StdID");
stdname = rs.getString("StdName"); stdname = stdname.trim();
stdmajor = rs.getString("StdMajor"); stdmajor = stdmajor.trim();
stdgender = rs.getString("StdGender"); stdgender = stdgender.trim();
stdage = rs.getInt("StdAge");
stdemail = rs.getString("StdEmail"); stdemail = stdemail.trim();
stdphone = rs.getString("StdPhone"); stdphone = stdphone.trim();
registeredby = rs.getString("RegisteredBy"); registeredby = registeredby.trim();
updatedby = rs.getString("UpdatedBy");
if (!rs.wasNull())
{ updatedby = updatedby.trim(); }
else
{ updatedby = ""; }
DisplayStatus.setText(stdname);
model.addRow(new Object[] { result, stdid, stdname, stdmajor, stdgender, stdage, stdemail, stdphone, registeredby, updatedby });
}
if(result>0)
{
DisplayStatus.setText("");
JPanel LeftGap = new JPanel();
LeftGap.setBackground(Color.WHITE);
LeftGap.setPreferredSize(new Dimension(20, 0));
JPanel RightGap = new JPanel();
RightGap.setBackground(Color.WHITE);
RightGap.setPreferredSize(new Dimension(20, 0));
JPanel BottomGap = new JPanel();
BottomGap.setBackground(Color.WHITE);
BottomGap.setPreferredSize(new Dimension(0, 20));
table.getColumnModel().getColumn(0).setPreferredWidth(10); // No. Column
table.getColumnModel().getColumn(1).setPreferredWidth(35); // StdID Column
table.getColumnModel().getColumn(3).setPreferredWidth(30); // StdMajor Column
table.getColumnModel().getColumn(4).setPreferredWidth(30); // StdGender Column
table.getColumnModel().getColumn(5).setPreferredWidth(10); // StdAge Column
table.getColumnModel().getColumn(6).setPreferredWidth(130); // StdEmail Column
table.getColumnModel().getColumn(7).setPreferredWidth(50); // StdPhone Column
DisplayTab.add(LeftGap, BorderLayout.LINE_START);
DisplayTab.add(RightGap, BorderLayout.LINE_END);
DisplayTab.add(BottomGap, BorderLayout.PAGE_END);
DisplayTab.add(scrollPane, BorderLayout.CENTER);
}
if(result <= 0)
{
//DisplayTab.remove(scrollPane);
scrollPane.setVisible(false);
DisplayStatus.setText("<html> <body bgcolor=C6C4C4> <font color=red style=font-size:35> There is no data in the database. </font> </body> </html>");
DisplayStatus.setHorizontalAlignment(JLabel.CENTER);
DisplayStatus.setVerticalAlignment(JLabel.CENTER);
DisplayTab.revalidate();
DisplayTab.repaint();
}
}
catch(Exception e)
{ e.printStackTrace(); }
}
UPDATE #1 : I added full event code.
First of all, variable names should NOT start with an upper case character. It messes up the code highlighting in the forum. Follow the Java Conventions.
JScrollPane scrollPane = new JScrollPane(table);
Don't create a new JScrollPane. Add the scrollpane to the frame when you create the GUI.
Then when you want to change the component displayed in the scrollpane you just use:
scrollPane.setViewportView( theComponent );
All i know is that having remove before setVisible will make setvisible not work.
if(result <= 0)
{
//DisplayTab.remove(scrollPane); not needed
scrollPane.setVisible(false);
DisplayStatus.setText("<html> <body bgcolor=C6C4C4> <font color=red style=font-size:35> There are no data in the database. </font> </body> </html>");
DisplayStatus.setHorizontalAlignment(JLabel.CENTER);
DisplayStatus.setVerticalAlignment(JLabel.CENTER);
DisplayTab.revalidate();
DisplayTab.repaint();
}

Re-Using JTableHeader

The code below displays 2 JTables.
As they both will have exactly the same headers I wanted, for the sake of efficiency, to reuse the header from the first table.
However running the code results in the header appearing in the second table but not in the table it came from originally.
I am less interested in work-arounds, but - for the sake of learning and understanding - more interested in finding out why the header does not appear in the first table.
Here is the code:
public class HeaderTest1 {
public void doTheTest() {
JFrame testFrame = new JFrame("Header Test");
JPanel pane = new JPanel();
Container theContentPane = testFrame.getContentPane();
BoxLayout box = new BoxLayout(pane, BoxLayout.Y_AXIS);
pane.setLayout(box);
theContentPane.add(pane);
String theData[][]
= {
{"One", "two", "3"},
{"four", "5", "six"},
{"7", "8", "9.0"},
{"£10.00", "11", "twelve"}
};
String columnNames[] = {"Column 1", "Column 2", "Column 3"};
JTable firstTable = new JTable(theData, columnNames);
JScrollPane thisScrollPane = new JScrollPane(firstTable);
JTableHeader thisTableHeader = firstTable.getTableHeader();
pane.add(thisScrollPane);
buildTheSecondTable(thisTableHeader, firstTable, columnNames, pane);
testFrame.pack();
testFrame.setVisible(true);
}
private void buildTheSecondTable(JTableHeader headerFromTheFirstTable,
JTable firstTable, String[] columnNames, JPanel pane) {
JTable secondTable = new JTable();
int columnCount = columnNames.length;
JScrollPane thisScrollPane = new JScrollPane(secondTable);
secondTable.setTableHeader(headerFromTheFirstTable);
Object[][] emptyData = new Object[1][columnCount];
for (int n = 0; n < columnCount; n++) {
emptyData[0][n] = "";
}
DefaultTableModel thisTableModel = new DefaultTableModel();
thisTableModel.setDataVector(emptyData, columnNames);
secondTable.setModel(thisTableModel);
secondTable.setLayout(firstTable.getLayout());
secondTable.setCellEditor(firstTable.getCellEditor());
pane.add(thisScrollPane);
}
public static void main(String[] args) throws SQLException, ParseException {
HeaderTest thisTest = new HeaderTest();
thisTest.doTheTest();
}
Any advice would be appreciated
A Swing component can only have a single parent so you can't share the table header component.
You can however share the Array of column names:
JTable firstTable = new JTable(theData, columnNames);
In your buildTheSecondTable method you have access to the array of column names so just use:
//DefaultTableModel thisTableModel = new DefaultTableModel();
DefaultTableModel thisTableModel = new DefaultTableModel(columnNames);
Then you can add data to the model and the model to the table.
Then reorder the code to create the JScrollPane after you add the model to the table.
Also, get rid of the table.setLayout() code. You would never use a layout manager on a table. You don't add components to the table. The table renders the data itself without using real components.

Categories

Resources