Results 1 to 4 of 4
Thread: Swing - CardLayout and JTable
- 07-17-2011, 05:37 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Swing - CardLayout and JTable
Hi,
I'm currently writing an ATM in swing and I used CardLayout to switch between the different panels.
One of the panels is a JTable that supposed to show the client history actions.
I've put the table in a scrollPane but for some reason it doesn't recognize the end of the panel as the end of table, so my scrollPane doesn't show up. I've tried changing either the size of the table or the scroll but it doesn't work.
I've included the code of my main frame and the JTable panel.
Any ideas?
(ATM Frame)
(ATM History)Java Code:public class ATMFrame extends JFrame { private static final long serialVersionUID = 1L; public ATMFrame() { this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setSize(600, 400); this.setResizable(false); JPanel center = new JPanel(new CardLayout()); DisplayHistoryPanel history = new DisplayHistoryPanel(center); center.add(history, "history"); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(center, BorderLayout.CENTER); } public static void main(String[] args) { ATMFrame atmFrame = new ATMFrame(); atmFrame.setVisible(true); } }
Java Code:public class DisplayHistoryPanel extends JPanel{ private static final long serialVersionUID = 5L; public DisplayHistoryPanel(JPanel cards) { JLabel helloLabel = new JLabel("<html><u>Hello </u></html>"); String[] tableHeaders = {"Balance ID", "Customer ID", "Action", "Amount", "Date", "Comments"}; DefaultTableModel model = new DefaultTableModel(tableHeaders, 20); JTable table = new JTable(model){ private static final long serialVersionUID = 5L; public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } }; JScrollPane scroll = new JScrollPane(table); this.setLayout(new BorderLayout()); JButton exitButton = new JButton("Exit"); this.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); northPanel.add(helloLabel); JPanel centerPanel = new JPanel(); centerPanel.add(scroll); JPanel southPanel = new JPanel(); southPanel.add(exitButton); this.add(northPanel, BorderLayout.NORTH); this.add(centerPanel, BorderLayout.CENTER); this.add(southPanel, BorderLayout.SOUTH); } }Last edited by Alex.R; 07-17-2011 at 09:56 PM. Reason: reposted the code. now complies without problems
-
And your LoginPanel? Best to simplify your program for posting here so that it is the smallest program possible that compiles and runs for us without outside dependencies (i.e., database, images, etc), and demonstrates your error, an SSCCE
-
Your updated code still won't compile or run as the LoginPanel is missing.
But your problem is with your centerPanel. Since no layout was set on it, it uses JPanel's default layout, FlowLayout, and this will allow the JScrollPane to go off screen. The solution is to have it use a BorderLayout and add the JScrollPane in the BorderLayout.CENTER position.
See my adaptation of your code below, code that will compile for me and demonstrate the problem. My major changes to your code are prefaced with a // !! comment. Comment out the part where I add a BorderLayout to your centerPanel and run it both ways to see what I mean:
Java Code:import java.awt.*; import java.awt.event.*; import java.sql.Connection; import java.sql.SQLException; import java.util.Date; import java.util.Vector; import javax.swing.*; import javax.swing.table.*; public class ATMFrame extends JFrame { private static final long serialVersionUID = 1L; public ATMFrame getInstance() { return this; } public ATMFrame() { this.setTitle("ATM"); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setSize(600, 400); // this.setResizable(false); this.setLocationRelativeTo(null); Color borderColor = new Color(150, 175, 200); Color backgroundColor = new Color(170, 212, 255); JPanel center = new JPanel(new CardLayout()); LoginPanel loginPanel = new LoginPanel(center); loginPanel.setBackground(backgroundColor); loginPanel.setWrongMassage(false); center.add(loginPanel, "Login"); // !! added center.add(new DisplayHistoryPanel(new VOCustomer(), new Vector<VOBalanceAction>(), center, null), "Display History"); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(Box.createHorizontalStrut(20), BorderLayout.EAST); this.getContentPane().add(Box.createHorizontalStrut(20), BorderLayout.WEST); this.getContentPane() .add(Box.createVerticalStrut(20), BorderLayout.NORTH); this.getContentPane() .add(Box.createVerticalStrut(20), BorderLayout.SOUTH); this.getContentPane().add(center, BorderLayout.CENTER); this.getContentPane().setBackground(borderColor); } public void setNextPanel(JPanel panel) { this.getContentPane().add(panel, BorderLayout.CENTER); } public static void main(String[] args) { ATMFrame atmFrame = new ATMFrame(); atmFrame.setVisible(true); } } class DisplayHistoryPanel extends JPanel { private static final long serialVersionUID = 5L; private final String FONT = "Arial"; private Color backgroundColor; private JPanel cards; public DisplayHistoryPanel(VOCustomer customer, Vector<VOBalanceAction> balanceActionList, JPanel cards, final Connection conn) { this.cards = cards; this.backgroundColor = new Color(170, 212, 255); JLabel helloLabel = new JLabel("<html><u>" + customer.getName() + " History" + "</u></html>"); helloLabel.setFont(new java.awt.Font(FONT, 3, 20)); String[] tableHeaders = {"Balance ID", "Customer ID", "Action", "Commission", "Date", "Comments"}; DefaultTableModel model = new DefaultTableModel(tableHeaders, 0); // !! int counter = 0; // while (counter < balanceActionList.size()) { // VOBalanceAction action = balanceActionList.get(counter); // Object[] row = {action.getBalance_id(), action.getCust_id(), // action.getAction(), Display.NIS(action.getAmount()), // Display.longToShortDate(action.getDate()), action.getComments()}; // model.addRow(row); // counter++; // } // !! added for (int i = 0; i < 30; i++) { Object[] row = new String[]{"Balance ID", "Customer ID", "Action", "Commission", "Date", "Comments"}; model.addRow(row); } JTable table = new JTable(model) { private static final long serialVersionUID = 5L; public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } // Creates shading for even rows public Component prepareRenderer(TableCellRenderer renderer, int Index_row, int Index_col) { Component comp = super.prepareRenderer(renderer, Index_row, Index_col); if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) { comp.setBackground(Color.lightGray); } else { comp.setBackground(Color.white); } return comp; } public boolean isCellEditable(int rowIndex, int colIndex) { return false; // Disallow the editing of any cell } }; JScrollPane scroll = new JScrollPane(table); JButton backButton = new JButton( new ActionListHandler("Back", this.cards)); backButton.setFont(new java.awt.Font(FONT, 1, 15)); backButton.setBackground(new Color(150, 160, 170)); JButton exitButton = new JButton("Exit"); exitButton.setFont(new java.awt.Font(FONT, 1, 15)); exitButton.setBackground(new Color(150, 160, 170)); exitButton.addActionListener(new ActionListener() { // Closes the ATM if the button "Exit" is clicked @Override public void actionPerformed(ActionEvent e) { try { if (conn != null) { conn.close(); } } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } System.exit(0); } }); this.setBackground(backgroundColor); this.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); northPanel.setBackground(backgroundColor); northPanel.add(helloLabel); JPanel centerPanel = new JPanel(); // !! added BorderLayout centerPanel.setLayout(new BorderLayout()); centerPanel.setBackground(backgroundColor); centerPanel.setBorder(BorderFactory.createRaisedBevelBorder()); centerPanel.add(scroll); JPanel southPanel = new JPanel(); southPanel.setBackground(backgroundColor); southPanel.add(backButton); southPanel.add(Box.createHorizontalStrut(50)); southPanel.add(exitButton); this.add(northPanel, BorderLayout.NORTH); this.add(centerPanel, BorderLayout.CENTER); this.add(southPanel, BorderLayout.SOUTH); } } @SuppressWarnings("serial") class LoginPanel extends JPanel { public LoginPanel(final JPanel center) { JButton next = new JButton("Next"); add(next); next.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { CardLayout cardlayout = (CardLayout) center.getLayout(); cardlayout.next(center); } }); } public void setWrongMassage(boolean b) { } } class VOCustomer { public String getName() { return "Customer Name"; } } class VOBalanceAction { public String getBalance_id() { return "balance id"; } public String getComments() { return "comments"; } public Date getDate() { return new Date(); } public double getAmount() { return 0.0; } public String getAction() { return "action"; } public String getCust_id() { return "cust id"; } } class Display { public static String NIS(double amount) { return "NIS (" + amount + ")"; } public static String longToShortDate(Date date) { return "long to short date"; } } @SuppressWarnings("serial") class ActionListHandler extends AbstractAction { public ActionListHandler(String string, JPanel cards) { } @Override public void actionPerformed(ActionEvent e) { } }
- 07-17-2011, 10:14 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
creating a Jtable using swing
By hariza in forum AWT / SwingReplies: 6Last Post: 08-25-2010, 03:14 AM -
Inside a Timer thread loop,how to refresh a JTable in swing
By neha_negi in forum Threads and SynchronizationReplies: 3Last Post: 09-04-2009, 01:45 AM -
How to add JTextArea in JTable - Swing
By phungho2000 in forum AWT / SwingReplies: 5Last Post: 12-17-2008, 09:18 PM -
Java Swing JTable Simple Doubt
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 11-26-2008, 01:46 PM -
Swing problem: JTable/TableModel
By Levish2002 in forum AWT / SwingReplies: 2Last Post: 08-24-2008, 08:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks